Date Formatting Without System Calls

Rob Cozens rcozens at pon.net
Sun Mar 17 12:44:01 EST 2002


Hi All,

For several years I've used a HyperTalk handler to determine the 
current Mac OS date panel settings without the aid of an external 
function.  Thanks to the addition of the system date function (& I do 
mean THANKS, Scott), it is now possible to do the same in Transcript:

function systemDateFormat
   put "1999,1,9,0,0,0,0" into testDate
   convert testDate to system date
   put 2 into characterNumber
   repeat while char characterNumber of testDate is in "1,9"
     add 1 to characterNumber
   end repeat
   put char characterNumber of testDate into dateSeparator
   put empty into dateFormat
   put the itemDelimiter into originalDelim
   set the itemDelimiter to dateSeparator
   repeat with x = 1 to 3
     get item x of testDate
     if it > 9 then
       if it > 99 then put "y" after dateFormat
       put "y"&return after dateFormat
     else if it > 1 then
       if length(it) > 1 then put "d" after dateFormat
       put "d"&return after dateFormat
     else
       if length(it) > 1 then put "m" after dateFormat
       put "m"&return after dateFormat
     end if
   end repeat
   set the itemDelimiter to originalDelim
   return dateFormat&dateSeparator
end systemDateFormat

Discussions regarding the calendar prompted me to realize one can use 
the long system date in a similar manner to obtain month & day names:

function systemMonth monthNumber
   if monthNumber < 1 or monthNumber > 12 then return empty --no 
support for the Hebrew calendar
   put "1999,"&monthNumber&",0,0,0,0,0" into testDate
   convert testDate to long system date
   get word 2 of testDate
   if it is a number then return word 3 of testDate else return it -- 
weak logic??
end systemMonth

function systemDay dayOfWeek -- 0=Sun thru 6=Sat
   if dayOfWeek < 0 or dayOfWeek > 6 then return empty
   put "2002,3,17,0,0,0,0" into testDate -- a Sunday
   convert testDate to seconds
   add dayOfWeek * 86400 to testDate -- 86400 = 60*60*24
   convert testDate to long system date
   get word 1 of testDate
   delete char offset(",",it) of it -- can day name have char other 
than "," after it?
   return it
end systemDay

One of my first "get acquainted with Revolution" projects will be to 
convert my HyperTalk & Xstuff libraries to Transcript.  Once done, I 
will make same available for anyone who can find a use for them, and 
these and other date/time functions will be included.  I'm posting 
these now because the subject arose, and to offer them for comment in 
case someone can see a better way.  For example, the systemDate 
function requires decyphering the returned value to determine date 
element order, leading zeroes, full century, and/or date separator.

-- 
Rob Cozens, CCW

"Where but America can the person who lost the popular vote become 
President without a coup?"



More information about the use-livecode mailing list