UTC Time

Just starting out? Need help? Post your questions and find answers here.
Marc56us
Addict
Addict
Posts: 1479
Joined: Sat Feb 08, 2014 3:26 pm

Re: UTC Time

Post by Marc56us »

Is there a reason to use a macro and variables?
We can then simplify a little more

Code: Select all

EnableExplicit

; -------------------------------------------------------------------
; cross-platform
; [after wilbert, <http://www.purebasic.fr/english/viewtopic.php?f=13&t=68554>]

ImportC ""
   time(*tm=#Null)
EndImport

Debug ( Date() - time() ) / 3600
:wink:
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Re: UTC Time

Post by ts-soft »

idle wrote:If I change my system time to display UTC , I still get the correct value :?
My Bad, i have used linux, sorry
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
idle
Always Here
Always Here
Posts: 5097
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: UTC Time

Post by idle »

ts-soft wrote:
idle wrote:If I change my system time to display UTC , I still get the correct value :?
My Bad, i have used linux, sorry
:lol: kind of looked like windows to me, but you still didn't say how the time was wrong.
maybe I'll just go along with Douglas Adams “Time is an illusion. Lunchtime doubly so.”
Windows 11, Manjaro, Raspberry Pi OS
Image
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: UTC Time

Post by Little John »

Marc56us wrote:Is there a reason to use a macro and variables?
There is no technical reason to use a macro and variables.

However, using the macro has the advantage that its name is self-explanatory -- in contrast to time(). And it was not my intention anyway to post code as simple as possible.

The part with the variables were not meant for copy-and-paste for general use, but only for demonstration purposes. Using a variable named 'local' stresses the point that this is the local time.
Marc56us wrote:We can then simplify a little more
Your code can be simplified even more. Since it doesn't contain any variables, EnableExplicit is not necessary. ;-)
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: UTC Time

Post by Little John »

With PB 5.62 and 5.70 beta 1 on Windows 10 version 1803, the following code shows always 2 here (where we currently have CEST), regardless of the value of RealTimeIsUniversal in the registry. If the computer clock has been adjusted so that Date() yields UTC, then of course time() does not yield UTC anymore.

Code: Select all

ImportC ""
   time(*tm=#Null)
EndImport

Debug (Date() - time()) / 3600
User avatar
idle
Always Here
Always Here
Posts: 5097
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: UTC Time

Post by idle »

Little John wrote:With PB 5.62 and 5.70 beta 1 on Windows 10 version 1803, the following code shows always 2 here (where we currently have CEST), regardless of the value of RealTimeIsUniversal in the registry. If the computer clock has been adjusted so that Date() yields UTC, then of course time() does not yield UTC anymore.

Code: Select all

ImportC ""
   time(*tm=#Null)
EndImport

Debug (Date() - time()) / 3600
Can you tell me how it compares to my OP? That's what I mean't to ask but didn't state it clearly.
There's a DST field and I don't know if I need to account for it or not.
It seemed to work when I changed my PC date to a time with DST in effect but I wanted confirmation
Windows 11, Manjaro, Raspberry Pi OS
Image
Little John
Addict
Addict
Posts: 4527
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: UTC Time

Post by Little John »

Hello idle,

I am sorry if I didn't understand you properly.

The code in your first post of this thread works as expected on my German Windows 10 system. I.e. the UTC() function yields 1 hour less than PB's Date() function when DST is not in effect, and 2 hours less when DST is in effect. This is always the case, regardless whether the value of RealTimeIsUniversal in the registry is 0 or 1.

This means, analogously to what I wrote in my previous post, that your function UTC() actually yields UTC, as long as the clock setting of the regarding computer is normal, so that PB's Date() function yields the local time (with or without DST in effect).
User avatar
idle
Always Here
Always Here
Posts: 5097
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: UTC Time

Post by idle »

Hi Little John,
Thanks, I thought it was correct but I was confused when it was pointed out that time() returns UTC
I had misunderstood what gmtime() does and had thought it was a required step to convert the time to UTC
Windows 11, Manjaro, Raspberry Pi OS
Image
timwil1963
New User
New User
Posts: 7
Joined: Sat Dec 28, 2019 4:26 pm

Re: UTC Time

Post by timwil1963 »

wilbert wrote:It shows the correct UTC time here (MacOS).
You could also use the PB functions to format.

Code: Select all

ImportC "" 
  time(*tm)
EndImport

Debug FormatDate("%dd %mm %yyyy %hh:%ii:%ss UTC", time(#Null))

Thank you for this - just what I was looking for
Post Reply