(De)Serialize the SystemDate
From Director Online Wiki
Saving the date to a text file and reading it back in as a date object may seem complicated, unless you realize you can do this:
dateObj = the systemDate -- get today's date from the computer put dateObj -- date( 2009, 4, 18 ) dateInt = integer(dateObj) -- convert to an integer put dateInt -- 20090418 -- at this point it's serialized and you can save it -- to a text file or to a list with other data. You can -- also convert it to a string: dateStr = string(dateInt) -- to get a date object back we just reverse the process dateObj = date(dateInt) -- or if it was a string, then: dateObj = date(integer(dateStr))
However, there's one more property available in the date object - seconds. It's not converted when converting the date object to an integer because the integer would always be larger than the maxInteger. But, if you want to maintain the seconds of the date object to calculate the time from it, then you can use a set of handlers like these:
on serializeDate(dateObj) if ilk(dateObj) <> #date then return VOID dateStr = string(integer(dateObj)) & string(dateObj.seconds) return dateStr end -- from the message window: dateStr = serializeDate(the systemDate) -- "2009041837304" on deserializeDate(dateString) if not stringP(dateString) then return VOID if dateString.length < 8 then return VOID dateObj = date(integer(dateString.char[1..8])) if dateString.length > 8 then dateObj.seconds = integer(dateString.char[9..dateString.length]) end if return dateObj end -- from the message window: dateObj = deserializeDate("2009041837304") put dateObj -- date( 2009, 4, 18 ) put dateObj.seconds -- 37304
- Note: A great use for (de)serializing the date object with seconds is as a unique ID that can double as a time stamp. Remember, that it's only good down to the second, which might not be enough resolution between serializeDate() handler calls if they're less than a second apart.