Loading ...
Sorry, an error occurred while loading the content.

610RE: [json] Dates do not automatically decode

Expand Messages
  • Greg Patnude
    Dec 5, 2006
    • 0 Attachment
      You can use my JavaScript utility object to return a data, time, or
      timestamp --

      // VC-DATE
      /*

      PROJECT: JDM (Java Dynamic Machine)
      PROGRAMMER: G. Patnude (gpatnude@...)
      FILE: vc-date/vc-date.js
      PURPOSE: DATE & TIME MANIPULATION ROUTINES...

      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
      * * * * * * * * * * * * * * * *
      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
      * * * * * * * * * * * * * * * *

      COPYRIGHT NOTICE: Copyright 2006 - iDynaTECH, inc.

      This software is distributed under the JSON license and may be
      re-distributed free of charge provided that
      appropriate credit is given to Gregory P. Patnude and iDynaTECH, inc.

      CONTACT INFO:

      Mr. Gregory P. Patnude
      Vice President, Innovations & Applications Group

      Interactive Dynamic Technologies, Incorporated
      dba: iDynaTECH, inc.
      120 North Pine Street
      Suite #162
      Spokane, WA. 99202
      (509) 343 - 3104

      WEB SITE: http://www.idynatech.com
      E-MAIL: gpatnude@...

      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
      * * * * * * * * * * * * * * * *
      * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
      * * * * * * * * * * * * * * * *

      */

      var JSDATE = {

      // CLASS VARIABLES...
      version : "0.000a",
      data: new Object(),
      months: Array('January', 'February', 'March', 'April', 'May',
      'June', 'July', 'August', 'September', 'October', 'November', 'December'),
      days : Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday',
      'Friday', 'Saturday'),

      // CLASS FUNCTIONS * CLASS FUNCTIONS * CLASS FUNCTIONS * CLASS FUNCTIONS *
      CLASS FUNCTIONS * CLASS FUNCTIONS * CLASS FUNCTIONS *
      // CLASS FUNCTIONS * CLASS FUNCTIONS * CLASS FUNCTIONS * CLASS FUNCTIONS *
      CLASS FUNCTIONS * CLASS FUNCTIONS * CLASS FUNCTIONS *

      // CONSTRUCTOR...
      init: function init (data) {

      this.data = data;
      return;

      },

      // test: function test(str) {
      test: function test(str) {

      // USE REGULAR EXPRESSIONS TO TEST THE CURRENT FORMAT OF THE
      SUPPLIED ARGUMENT...
      var tests = [

      ["TIMESTAMP", new
      RegExp(/\d{4}\-\d{2}\-\d{2}\W\d{2}\:\d{2}\:\d{2}\.\d/ig)],
      ["DATE", new RegExp(/\d{4}\-\d{2}\-\d{2}/ig)],
      ["TIME", new RegExp(/\d{2}\:\d{2}\:\d{2}\.\d/ig)],

      ];

      // WHILE LOOPING THROUGH EACH TEST...
      for (var t in tests) {

      // PERFORM THE TEST AGAINST THE SUPPLIED ARGUMENT
      AND RETURN IT PROPERLY FORMATTED...
      var branch = (tests[t][1].test(str)) ? tests[t][0] :
      null;

      // NOW --> TO PROPERLY FORMAT THE ARGUMENT...
      if (branch) {

      // EVAL AND FORCE A CALL TO THE FORMATTER
      FOR THIS TYPE...
      eval("dx = this." + branch + "(str,
      tests[t][1])");
      return dx;

      }

      }

      },

      // CONVERTS A DATE TO AN OBJECT...
      DATE: function DATE(d, test) {

      var ds = d.split("-");
      var dt = new Date(ds[0], (ds[1] - 1), ds[2]);
      dt.type = 'DATE';

      dt.year = parseInt(ds[0]);
      dt.month = parseInt(ds[1]);
      dt.day = parseInt(ds[2]);

      return dt;

      },

      // CONVERTS A TIME TO AN OBJECT...
      TIME: function TIME(t, test) {

      var ts = t.split(":");
      var msec = ts[2].split(".");
      var tt = new Date(0, 0, 0, ts[0], ts[1], msec[0], msec[1]);

      tt.type = 'TIME';

      tt.hour = parseInt(ts[0]);
      tt.minute = parseInt(ts[1]);
      tt.sec = parseInt(msec[0]);
      tt.msec = parseInt(msec[1]);

      return tt;

      },

      // CONVERTS A TIMESTAMP TO AN OBJECT...
      TIMESTAMP: function TIMESTAMP(ts, test) {

      // UTIL.trace("TIMESTAMP() called with: " + ts + ", " + test
      + ", " + r + ", " + c);
      // var tso = new Object();
      var tx = ts.split(new RegExp(/\ /gi));
      var ds = tx[0].split("-");
      var ts = tx[1].split(":");
      var msec = ts[2].split(".");
      var tso = new Date(ds[0], (ds[1] - 1), ds[2], ts[0], ts[1],
      msec[0], msec[1]);
      tso.type = 'TIMESTAMP';
      return tso;

      }

      // EOF

      };



      -----Original Message-----
      From: json@yahoogroups.com [mailto:json@yahoogroups.com] On Behalf Of Peter
      Michaux
      Sent: Monday, December 04, 2006 5:30 PM
      To: json@yahoogroups.com
      Subject: [json] Dates do not automatically decode

      Hi,

      Is there a strategy for decoding dates that could or should be in
      json.js? It seems incomplete that it can encode dates but doesn't
      automatically decode them into Date objects as well. It is nice when
      life is a two way street. Is it too restrictive to require that any
      string with the regexp is turned into a date object?

      \d{4}-\d{2}-\d{2}T\d{2}\:\d{2}:\d{2}

      I know this picks invalid dates. Perhaps some more inspection to
      insure it is a date. If it is not a real date then it should be left
      as a string? This seems like an expensive process and might be better
      to allow people to just loose out if the have a json string with an
      invalid date.

      I made some experiements with YAML in Ruby and when a date is loaded
      it seems to stay as a string as does the JSON code. It seems confusing
      that this is ok that the conversions are not perfectly reversable.

      Any thoughts on this?

      Thanks,
      Peter
    • Show all 9 messages in this topic