date libraries/routines for LCG

Bob Sneidar bobsneidar at iotecdigital.com
Wed May 9 12:56:42 EDT 2018


Yes, I contributed to the Master Library a couple of date and time functions which return formats which LC does not provide. One is sql date which returns the date in the format yyyy-mm-dd, The second is julian date which uses Jacques method (I understand there is not simply one Julian standard!) 

For time I have sql time (hh:mm:ss) and military, returning the time in 24 hour format. 

So for instance you can convert to sql datetime format by using formatDate(the date, "sql date) && formatTime(the time, "sql time"). 

I'm not sure if the Master Library made some improvement on the functions I submitted, but here is what I have:

function formatDate theDate, theFormat
   /*
   Accepts any valid date for the first parameter. If not a valid date, it simply returns
   what was passed. Second parameter can be any of the following:
   sql date: date in the yyyy-mm-dd format
   short date, abbreviated date, internet date, long date: LC versions of the same
   julian date: Julian number based on (I believe) Jacques formula)
   */
   
   put the itemdelimiter into theOldDelim
   set the itemdelimiter to "-"
   
   if the length of item 1 of theDate = 4 and \
         the number of items of theDate = 3 and \
         item 1 of theDate is a number and \
         item 2 of theDate is a number and \
         item 3 of theDate is a number then
      put item 2 of theDate & "/" & \
            item 3 of theDate & "/" & \
            item 1 of theDate into theDate
   end if
   
   convert theDate to dateitems
   set the itemdelimiter to theOldDelim
   
   switch theFormat
      case "sql date"
         put item 1 of theDate & "-" & \
               format("%02d",item 2 of theDate) & "-" & \
               format("%02d",item 3 of theDate) into theDate
         break
      case "short date"
         convert theDate from dateitems to short date
         break
      case "abbreviated date"
         convert theDate from dateitems to abbreviated date
         break
      case "abbr date"
         convert theDate from dateitems to abbreviated date
         break
      case "internet date"
         convert theDate from dateitems to internet date
         break
      case "long date"
         convert theDate from dateitems to long date
         break
      case "julian date"
         put the date into theDate
           convert theDate to dateItems
           if  ((item 2 of theDate = 1) or (item 2 of theDate = 2)) then
                 put 1 into theDay
           else
                 put 0 into theDay
           end if
           put item 1 of theDate + 4800 - theDay into theYear
           put item 2 of theDate + (12 * theDay) - 3 into theMonth
           put item 3 of theDate + \
                       ((153 * theMonth + 2) div 5) + \
                       (365 * theYear) + \
                       (theYear div 4) - \
                       (theYear div 100) + \
                       (theYear div 400) - \
                       32045 into theDate
         break
   end switch
   
   return theDate 
end formatDate

function formatTime theTime, theFormat
   /*
   accepts any valid time and returns the form of the time specified in the second parameter.
   The valid formats are:
   sql time: hh:mm:ss (Note: combining sql date from the formatDate() function with the
   sql time will produce a valid SQL date time type). 
   short time: LC short time format
   abbreviated time: LC abbr time format (same as short time)
   long time: LC long time format
   seconds: the number of seconds since the prior midnight
   military: the military time 00:00 - 23:59
   */
   if theTime is empty then return empty
   
   set the numberformat to "00"
   switch theFormat
      case "sql time"
         convert theTime to dateitems
         put (item 4 of theTime +0) & ":" & \
               (item 5 of theTime +0) & ":" & \
               (item 6 of theTime +0) into theTime
         break
      case "short time"
         convert theTime to short time
         break
      case "abbreviated time"
         convert theTime to abbreviated time
         break
      case "long time"
         convert theTime to long time
         break
      case "seconds"
         convert theTime to seconds
         break
      case "military"
         set the itemdelimiter to ":"
         
         if theTime contains "PM" then
            add 12 to item 1 of theTime
         end if
         
         put word 1 of item 2 of theTime into item 2 of theTime
         break
   end switch
   
   return theTime
end formatTime


> On May 8, 2018, at 18:14 , Mike Doub via use-livecode <use-livecode at lists.runrev.com> wrote:
> 
> Have you checked the Master Library?
> 
> https://www.dropbox.com/s/3wpwn3hfbmpl7sk/MasterLibrary.livecode?dl=0





More information about the use-livecode mailing list