Getting Current UTC
#1
Have waded back thru the previous forum posts but at a loss to get an answer.
Sorry if it's been asked before but all I need is the numerical string for the Current UTC timestamp.
How?
Thanks.
Reply
#2
Do you mean getting it from the NOWUTC in PowerTime? TimeStringFull?
Or how to get it from the web?
Reply
#3
(05-30-2025, 11:32 AM)Kurt Kuzba Wrote: Do you mean getting it from the NOWUTC in PowerTime? TimeStringFull?
Or how to get it from the web?
Thanks Kurt, not web.  Just need the UTC as a string to log events against and to keep a note of elapsed time. Trying to keep it simple!
Reply
#4
(05-30-2025, 06:22 AM)Owen_English Wrote: Sorry if it's been asked before but all I need is the numerical string for the Current UTC timestamp.
How?

You might want to have a look at the PowerTime Object.
„Let the machine do the dirty work.“
The Elements of Programming Style, Brian W. Kernighan, P. J. Plauger 1978
Reply
#5
Yes, am looking at that, seems as tho needs 20-30 lines of code to get the UTC 'now' string and just wondered if there was a simpler way of doing it.
Reply
#6
Do you have PBWin 10.0x for PowerTime?

" Just need the UTC as a string to log events against and to keep a note of elapsed time." To do arithmetic in time (like elapsed) get/keep the stamp in PowerTime (AKA QUAD or FileTime), then convert to text number for display or print.

For older PB versions the API functions are not hard to do.

Cheers,

we cross posted on  last.

30 lines? Where is that?
Looks closer to 4 lines at a quick glance:
NOW
TIMEDIFF
DATESTRING
TIMESTRINGFULL
Reply
#7
As simple as a call to GetSystemTime then format the response. But why here instead of PowerBASIC forums?

Code:
FUNCTION fn_GetCurrentTimeISO8601UTC() AS STRING
   LOCAL v_udtST AS SYSTEMTIME
   LOCAL v_strTimpstamp AS STRING
   GETSYSTEMTIME v_udtST
   FUNCTION = fn_SystemTimeToISO8601(v_udtST)
END FUNCTION

'----------------------------------------------------------------------------(')

FUNCTION fn_SystemTimeToISO8601(BYREF v_udtST AS SYSTEMTIME) AS STRING
   FUNCTION = _
      FORMAT$(v_udtST.wYear, "0000") & _
      FORMAT$(v_udtST.wMonth, "00") & _
      FORMAT$(v_udtST.wDay, "00") & _
      "T" & _ _
      FORMAT$(v_udtST.wHour, "00") & _
      FORMAT$(v_udtST.wMinute, "00") & _
      FORMAT$(v_udtST.wSecond, "00") & _
      "Z"
END FUNCTION
Reply
#8
(05-30-2025, 11:45 AM)Owen_English Wrote:
(05-30-2025, 11:32 AM)Kurt Kuzba Wrote: Do you mean getting it from the NOWUTC in PowerTime? TimeStringFull?
Or how to get it from the web?
Thanks Kurt, not web.  Just need the UTC as a string to log events against and to keep a note of elapsed time. Trying to keep it simple!

Resurrecting this thread after browsing the forum.

To keep track of elapsed time it is probably better to use what is commonly called a Posix/Unix/Epoch Timestamp (as a string if necessary). That allow easy log sorting and elapsed time calculations . It is the number of seconds (optionally including milliseconds0) since  January 1, 1970, at 00:00:00.
Compare to doing arithmetic on an ISO DateTime string Sad'
Code:
' Current Unix Epoch Time (seconds since midnight January 1, 1970 UTC) and ISOTime
#COMPILE EXE
#DIM ALL
FUNCTION PBMAIN () AS LONG
    ? STR$(UnixTimeStamp) & $LF & STR$(UnixTimeStampD) & $LF & ISODateTime & $LF & ISODateTimeD
END FUNCTION

FUNCTION UnixTimeStamp() AS LONG ' Integer stamp
    LOCAL tmpTime AS IPOWERTIME
    tmpTime = CLASS "PowerTime"
    tmptime.nowUtc
    FUNCTION = tmpTime.filetime/10000000 - 11644473600
END FUNCTION

FUNCTION UnixTimeStampD AS DOUBLE ' With milliseconds
    LOCAL NowTime AS IPOWERTIME
    NowTime = CLASS "PowerTime"
    NowTime.NowUTC ' get current Time in UTC
    FUNCTION = NowTime.FileTime /10000000 - 11644473600
END FUNCTION

FUNCTION ISODateTime() AS STRING 'To second
  LOCAL tmpTime AS IPOWERTIME
    tmpTime = CLASS "PowerTime"
    tmptime.NowUTC
    FUNCTION = 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"))
END FUNCTION
FUNCTION ISODateTimeD() AS STRING 'Decimal - incl msecs
  LOCAL tmpTime AS IPOWERTIME
    tmpTime = CLASS "PowerTime"
    tmptime.NowUTC
    FUNCTION = 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"),".",FORMAT$(tmpTime.mSecond,"000"))
END FUNCTION
'
Reply
#9
> probably better to use (...) the number of seconds (optionally including milliseconds0) since  January 1, 1970, at 00:00:00.

Horses for courses. I work with data going back to 1960, so I prefer FILETIME: the number of 100-nanosecond units since the start of January 1, 1601.  PowerTime is based on FILETIME. But if I was a cosmologist working on the age of the universe, even that would not have enough range, and I wouldn't need nanosecond precision.

For the OP's needs, the onlly reason I'd prefer FILETIME is that it is native to the compiler, and Windows uses FILETIME under the hood for everything.
Reply
#10
(08-31-2025, 08:19 AM)Eric Pearson Wrote: > probably better to use (...) the number of seconds (optionally including milliseconds0) since  January 1, 1970, at 00:00:00.

Horses for courses. I work with data going back to 1960, so I prefer FILETIME: the number of 100-nanosecond units since the start of January 1, 1601.  PowerTime is based on FILETIME. But if I was a cosmologist working on the age of the universe, even that would not have enough range, and I wouldn't need nanosecond precision.

For the OP's needs, the onlly reason I'd prefer FILETIME is that it is native to the compiler, and Windows uses FILETIME under the hood for everything.


Yep, horses for courses. Personally, I like Unix Time for interoperability but there is nothing wrong with Filetime if you need a broader range.  But I'd divide it by 10^7 and round as required for ease of use 

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


Like Google says:

Unix time is used in Unix-like operating systems (Linux, macOS, Android), most programming languages (C, Java, Python, JavaScript), and various web platforms and applications for internal timestamps and data exchange. It provides a simple, universal way to represent time as a count of seconds since January 1, 1970, at 00:00:00 Coordinated Universal Time (UTC). 

Where Unix time is used:
  • Operating Systems:
    Foundational to Unix and Unix-like systems, including Linux, macOS, and mobile operating systems like Android and iOS. 

[*]Programming Languages:
Widely adopted in most modern programming languages for handling dates and times, including Java, Python, JavaScript, C/C++, PHP, and Perl. 

Web Development:
Used for timestamps on web platforms and in data formats like JSON, often in milliseconds rather than seconds. 

APIs and Data Services:
Common in APIs for industrial equipment, such as those using eGauge, BACnet, or Modbus, to report timestamps and time ranges in data. 

Internal System Timekeeping:
Systems use it for internal logging, scheduling tasks, and other functions that require a consistent, universal time representation. 

Why Unix time is used:
  • Simplicity:
    It's a straightforward way to store and manipulate time as a single integer, making it easy to compare different points in time and perform calculations. 

Universality:
By using UTC as its reference point, it provides a common, time-zone-independent standard across different systems and geographical locations. 



[*]Interoperability:
Its widespread adoption in various systems and languages ensures that data can be shared and understood across different computing environments
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)