DateTime functions

From DataFlex Wiki
Jump to navigationJump to search

ISO8601TimeStamp

Here's a function I use for creating a timestamp, this uses the ISO8601 format as it has advantages such as that it automatically sorts correctly when sorted with ASCII ordering.

This works well even with databases - such as the embedded database - that do not offer native date time formats.

  
  //
  // Returns an ISO8601 timestamp this is used by for example SQLite in date time columns and works
  // wonderfully well within the dataflex database too.
  // You'll need an ascii column of 24 characters
  // the format is:
  // YYYY-MM-DDTHH:MM:SS.SSS
  // This is assuming you do not need timezone information.
  // See also:
  // http://www.sqlite.org/lang_datefunc.html
  //
  Function ISO8601TimeStamp Returns String
    String    sYear
    String    sMonth
    String    sDay
    String    sHour
    String    sMin
    String    sSec
    String    smilSec
    String    sTimeStamp
    DateTime  dtNow
    
    Move (CurrentDateTime())    to dtNow
    Move (DateGetYear(dtNow))        to sYear
    Move (DateGetMonth(dtNow))       to sMonth
    Move (DateGetDay(dtNow))         to sDay
    Move (DateGetHour(dtNow))        to sHour
    Move (DateGetMinute(dtNow))      to sMin
    Move (DateGetSecond(dtNow))      to sSec
    Move (DateGetMillisecond(dtNow)) to smilSec
    
    If (Length(sMonth)=1)  Move ("0"+sMonth)  to sMonth
    If (Length(sDay)=1)    Move ("0"+sDay)    to sDay
    If (Length(sHour)=1)   Move ("0"+sHour)   to sHour
    If (Length(sMin)=1)    Move ("0"+sMin)    to sMin
    If (Length(sSec)=1)    Move ("0"+sSec)    to sSec
    If (Length(smilSec)=1) Move ("0"+smilSec) to smilSec
    If (Length(smilSec)=2) Move ("0"+smilSec) to smilSec
    
    Move (SFormat("%1-%2-%3T%4:%5:%6.%7",sYear,sMonth,sDay,sHour,sMin,sSec,smilSec)) to sTimeStamp
    
    Function_Return sTimeStamp
  End_Function
  

XML DateTime functions

If you have a SOAP service and you need to convert from / to datetime formats then these functions might come in handy.

    
    //
    //  Gets the data and time for the datetime string as passed by
    //  XML. Automatic casting strips of the time part and we do
    //  want the time
    //
    //  Returns:
    //      dtDateTime with the date and time as passed in xml
    //
    Function StringToDateTime String sDateTime Returns DateTime
        String sDate
        String sYear sMonth sDay
        String sHours sMinutes sSeconds
        String sMilliSeconds
        String sTimeString
        Integer iPos
        DateTime dtTime
        
        Move "" to sTimeString
        Move (Pos("T",sDateTime)) to iPos
        If (iPos>0) Begin
          Move (Left(sDateTime,iPos-1)) to sDate
          Move (Left(sDate,4))  to sYear
          Move (Mid(sDate,2,6)) to sMonth
          Move (Right(sDate,2)) to sDay
          //Move sDate to dtTime // does not work, because US has no military date format
          Move (Replace(sDate+"T",sDateTime,"")) to sTimeString
          //  Extract parts
          Move (Mid(sTimeString, 2, 1))  to sHours
          Move (Mid(sTimeString, 2, 4))  to sMinutes
          Move (Mid(sTimeString, 2, 7))  to sSeconds
          Move (Mid(sTimeString, 3, 10)) to sMilliSeconds
        End
        
        //  Set to DateTime
        Move (DateSetYear(dtTime,      Integer(sYear)))    to dtTime
        Move (DateSetMonth(dtTime,     Integer(sMonth)))   to dtTime
        Move (DateSetDay(dtTime,       Integer(sDay)))     to dtTime
        Move (DateSetHour(dtTime,      Integer(sHours)))   to dtTime
        Move (DateSetMinute(dtTime,    Integer(sMinutes))) to dtTime
        Move (DateSetSecond(dtTime,    Integer(sSeconds))) to dtTime
        Move (DateSetMilliSecond(dtTime, Integer(sMilliSeconds))) to dtTime
        
        Function_Return dtTime
    End_Function

    
    //
    //  Gets the datetime as passed by XML.
    //  Automatic casting strips of the Time part and we do
    //  want the time
    //
    //  Returns:
    //      DateTime string with the date and time as passed in xml
    //
    Function DateTimeToString DateTime dtTime Returns String
        String sYear sMonth sDay
        String sHours sMinutes sSeconds
        String sMilliSeconds
        String sTimeString
        
        Move "" to sTimeString
        
        Move (DateGetYear(dtTime))        to sYear
        Move (DateGetMonth(dtTime))       to sMonth
        If (Integer(sMonth)<10) Move ("0"+sMonth) to sMonth
        Move (DateGetDay(dtTime))         to sDay
        If (Integer(sDay)<10)   Move ("0"+sDay) to sDay
        Move (DateGetHour(dtTime))        to sHours
        If (Integer(sHours)<10) Move ("0"+sHours) to sHours
        Move (DateGetMinute(dtTime))      to sMinutes
        If (Integer(sMinutes)<10) Move ("0"+sMinutes) to sMinutes
        Move (DateGetSecond(dtTime))      to sSeconds
        If (Integer(sSeconds)<10) Move ("0"+sSeconds) to sSeconds
        Move (DateGetMilliSecond(dtTime)) to sMilliSeconds
        Move (sYear+"-"+sMonth+"-"+sDay+"T"+sHours+":"+sMinutes+":"+sSeconds+"."+sMilliSeconds) to sTimeString
        Function_Return sTimeString
    End_Function



External links

See also: