Getting Current UTC
#11
Tidied up functions for UNIX TimeStamp, FileTime seconds and current UTC ISO DateTime as strings, all with optional fractional seconds (up to msecs)

'
Code:
' Current Unix Time, FileTime seconds and ISO UTC DateTime
' to a maximum of 3 decimal places!!!
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN () AS LONG
    LOCAL s AS STRING
    ? "UnixTime:" & $LF & UnixTimeNowStr & $LF & UnixTimeNowStr(1) & $LF & $LF _
      & "Filetime: " & $LF & FileTimeNowStr & $LF & FileTimeNowStr(2) & $LF & $LF _
      & "ISO DateTime: " & $LF & ISONowUTCStr & $LF & ISONowUTCStr(3) & $LF,, "UTC TimeStamps"
END FUNCTION

FUNCTION UnixTimeNowStr((OPT BYVAL decplaces AS LONG) AS STRING
    LOCAL tmpTime AS IPOWERTIME, fmt AS STRING
    fmt = "0"
    IF decplaces > 3 THEN decplaces = 3
    IF decplaces > 0 THEN fmt &= "." & STRING$(decplaces,"0")
    tmpTime = CLASS "PowerTime"
    tmptime.nowUtc
    FUNCTION = FORMAT$(tmpTime.filetime/10000000 - 11644473600,fMt)
END FUNCTION

FUNCTION FileTimeNowStr(OPT BYVAL decplaces AS LONG) AS STRING
    LOCAL tmpTime AS IPOWERTIME, fmt AS STRING
    tmpTime = CLASS "PowerTime"
    tmptime.NowUTC
    fmt = "0"
    IF decplaces > 3 THEN decplaces = 3
    IF decplaces > 0 THEN fmt &= "." & STRING$(decplaces,"0")
    FUNCTION = FORMAT$(tmpTime.Filetime /10^7,fmt)
END FUNCTION

FUNCTION ISONowUTCStr(OPT BYVAL decplaces AS LONG) AS STRING
  LOCAL tmpTime AS IPOWERTIME, s AS STRING
    tmpTime = CLASS "PowerTime"
    tmptime.NowUTC
    s = CHR$(FORMAT$(tmpTime.Year,"0000"),FORMAT$(tmpTime.Month,"00"),FORMAT$(tmpTime.Day,"00"),"T", _
              FORMAT$(tmpTime.Hour,"00"),FORMAT$(tmpTime.Minute,"00"),FORMAT$(tmpTime.Second,"00"))
    IF decplaces > 3 THEN decplaces = 3
    IF decplaces > 0 THEN s &= "." & FORMAT$(CINT(tmpTime.msecond/10^(3-decplaces)))
    FUNCTION = s & "Z"
END FUNCTION
'
Reply
#12
Was thinking along similar lines as Eric; we're in a Windows environment. But found post 10 compelling. Updating to OPT parameters is logical too.

Suppose the 'ux timestamp is kept in binary for sorting and duration, then string form is needed later? Back to Powertime/filetime then Powertime methods/filetimetosystemtime?

(Only a bit, but some of the opening is butter because I don't need it, so am too lazy to do itmyself!  Big Grin ))
Reply


Forum Jump:


Users browsing this thread: Pierre Bellisle, 1 Guest(s)