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
'
--
New PowerBASIC User Community Forum:
https://pbusers.org/forum
Reply


Messages In This Thread
Getting Current UTC - by Owen_English - 30.05.2025, 07:22 AM
RE: Getting Current UTC - by Kurt Kuzba - 30.05.2025, 12:32 PM
RE: Getting Current UTC - by Owen_English - 30.05.2025, 12:45 PM
RE: Getting Current UTC - by Stuart McLachlan - 31.08.2025, 04:06 AM
RE: Getting Current UTC - by Albert Richheimer - 30.05.2025, 12:54 PM
RE: Getting Current UTC - by Owen_English - 30.05.2025, 12:58 PM
RE: Getting Current UTC - by Dale Yarker - 30.05.2025, 01:09 PM
RE: Getting Current UTC - by George Bleck - 30.05.2025, 11:29 PM
RE: Getting Current UTC - by Eric Pearson - 31.08.2025, 09:19 AM
RE: Getting Current UTC - by Stuart McLachlan - 31.08.2025, 10:04 AM
RE: Getting Current UTC - by Stuart McLachlan - 01.09.2025, 07:06 AM
RE: Getting Current UTC - by Dale Yarker - 01.09.2025, 09:06 AM
RE: Getting Current UTC - by Dale Yarker - 01.09.2025, 11:02 AM
RE: Getting Current UTC - by Stuart McLachlan - 01.09.2025, 11:37 AM
RE: Getting Current UTC - by Stuart McLachlan - 03.09.2025, 07:05 AM

Forum Jump:


Users browsing this thread: 1 Guest(s)