UTC Time

Just starting out? Need help? Post your questions and find answers here.
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

UTC Time

Post by idle »

I need to get UTC time, is this right for you? Seems to be right for me

Code: Select all

Structure tm
   tm_sec.l;   // seconds after the minute - [0, 60] including leap second
   tm_min.l;   // minutes after the hour - [0, 59]
   tm_hour.l;  // hours since midnight - [0, 23]
   tm_mday.l;  // day of the month - [1, 31]
   tm_mon.l;   // months since January - [0, 11]
   tm_year.l;  // years since 1900
   tm_wday.l;  // days since Sunday - [0, 6]
   tm_yday.l;  // days since January 1 - [0, 365]
   tm_isdst.l; // daylight savings time flag
 EndStructure 


ImportC "" 
   time(*tm)
   gmtime(t.i) 
 EndImport  
 
 Procedure.s UTC()
   Protected time.tm,*time.tm,date.s,day.s  
   time(@time)
   *time = gmtime(@time) 
      
   Select *time\tm_wday 
     Case 0 
       day = "Sunday"
     Case 1 
       day = "Monday"
     Case 2 
       day = "Tuesday" 
     Case 3 
       day = "Wednesday"
     Case 4 
       day = "Thursday" 
     Case 5 
       day = "Friday" 
     Case 6 
       day = "Saturday" 
   EndSelect     
   
      
   date + Day + ", " + RSet(Str(*time\tm_mday),2,"0") + " " + RSet(Str(1 + *time\tm_mon),2,"0") + " " + Str(1900 + *time\tm_year) + " " + RSet(Str(*time\tm_hour),2,"0") + ":" + RSet(Str(*time\tm_min),2,"0") + ":" + RSet(Str(*time\tm_sec),2,"0") + " UTC" 
     
    ProcedureReturn date 
  EndProcedure  

Debug UTC()
Windows 11, Manjaro, Raspberry Pi OS
Image
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4946
Joined: Sun Apr 12, 2009 6:27 am

Re: UTC Time

Post by RASHAD »

Hi idle
KSA : Yes it is OK
Egypt my love
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 »

I have utc in my computer-bios, so your code shows the false value :)

Image
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
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: UTC Time

Post by wilbert »

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))
Windows (x64)
Raspberry Pi OS (Arm64)
ZX80
Enthusiast
Enthusiast
Posts: 361
Joined: Mon Dec 12, 2016 1:37 pm

Re: UTC Time

Post by ZX80 »

wilbert
yes. you are right.
I wanted to write this:

Code: Select all

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

Debug "Time zone: "+ Str((Date() - time()) / 3600)
to get timezone info.
and full code:

Code: Select all

Structure tm
  tm_sec.l;   // seconds after the minute - [0, 60] including leap second
  tm_min.l;   // minutes after the hour - [0, 59]
  tm_hour.l;  // hours since midnight - [0, 23]
  tm_mday.l;  // day of the month - [1, 31]
  tm_mon.l;   // months since January - [0, 11]
  tm_year.l;  // years since 1900
  tm_wday.l;  // days since Sunday - [0, 6]
  tm_yday.l;  // days since January 1 - [0, 365]
  tm_isdst.l; // daylight savings time flag
EndStructure


ImportC ""
  time(*tm = #Null)
  gmtime(t.i)
EndImport


Procedure.s GetDay(value)
Protected day.s
  Select value
    Case 0
      day = "Sunday"
    Case 1
      day = "Monday"
    Case 2
      day = "Tuesday" 
    Case 3 
      day = "Wednesday"
    Case 4 
      day = "Thursday" 
    Case 5 
      day = "Friday" 
    Case 6 
      day = "Saturday" 
    EndSelect
  ProcedureReturn day
EndProcedure

Procedure.s UTC()
  Protected time.tm,*time.tm,date.s
  time(@time)
  *time = gmtime(@time)
  date + GetDay(*time\tm_wday) + ", " + RSet(Str(*time\tm_mday),2,"0") + " " + RSet(Str(1 + *time\tm_mon),2,"0") + " " + Str(1900 + *time\tm_year) + " " + RSet(Str(*time\tm_hour),2,"0") + ":" + RSet(Str(*time\tm_min),2,"0") + ":" + RSet(Str(*time\tm_sec),2,"0") + " UTC"
  ProcedureReturn date
EndProcedure


timezone = (Date() - time()) / 3600
If timezone = 0
  LocalTime.SYSTEMTIME
  GetLocalTime_(LocalTime)
  utc.s = GetDay(LocalTime\wDayOfWeek) + ", " + RSet(Str(LocalTime\wDay),2,"0") + " " + RSet(Str(LocalTime\wMonth),2,"0") + " " + Str(LocalTime\wYear) + " " + RSet(Str(LocalTime\wHour),2,"0") + ":" + RSet(Str(LocalTime\wMinute),2,"0") + ":" + RSet(Str(LocalTime\wSecond),2,"0") + " utc"
  Debug utc
Else
  Debug UTC()
EndIf
User avatar
mk-soft
Always Here
Always Here
Posts: 6204
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: UTC Time

Post by mk-soft »

This work with Windows, Linux and MacOS...

But difftime no right result <- fixed

Update - Thanks Wilbert
Update 2

Code: Select all

Structure tm
  tm_sec.l;   // seconds after the minute - [0, 60] including leap second
  tm_min.l;   // minutes after the hour - [0, 59]
  tm_hour.l;  // hours since midnight - [0, 23]
  tm_mday.l;  // day of the month - [1, 31]
  tm_mon.l;   // months since January - [0, 11]
  tm_year.l;  // years since 1900
  tm_wday.l;  // days since Sunday - [0, 6]
  tm_yday.l;  // days since January 1 - [0, 365]
  tm_isdst.l; // daylight savings time flag
EndStructure


ImportC ""
  time(time_t = #Null)
  localtime(*tm = #Null)
  gmtime(time_t.i)
  mktime(*tm)
  difftime.d(*tm1, *tm2)
EndImport


Procedure.s GetDay(value)
Protected day.s
  Select value
    Case 0
      day = "Sunday"
    Case 1
      day = "Monday"
    Case 2
      day = "Tuesday" 
    Case 3 
      day = "Wednesday"
    Case 4 
      day = "Thursday" 
    Case 5 
      day = "Friday" 
    Case 6 
      day = "Saturday" 
    EndSelect
  ProcedureReturn day
EndProcedure

Procedure.s UTC_String()
  Protected now.i,*time.tm,date.s
  time(@now)
  *time = gmtime(@now)
  date + GetDay(*time\tm_wday) + ", " + RSet(Str(*time\tm_mday),2,"0") + "." + RSet(Str(1 + *time\tm_mon),2,"0") + "." + Str(1900 + *time\tm_year) + " " + RSet(Str(*time\tm_hour),2,"0") + ":" + RSet(Str(*time\tm_min),2,"0") + ":" + RSet(Str(*time\tm_sec),2,"0") + " UTC"
  ProcedureReturn date
EndProcedure

Procedure.s LocalTime_String()
  Protected now.i,*time.tm,date.s
  time(@now)
  *time = localtime(@now)
  date + GetDay(*time\tm_wday) + ", " + RSet(Str(*time\tm_mday),2,"0") + "." + RSet(Str(1 + *time\tm_mon),2,"0") + "." + Str(1900 + *time\tm_year) + " " + RSet(Str(*time\tm_hour),2,"0") + ":" + RSet(Str(*time\tm_min),2,"0") + ":" + RSet(Str(*time\tm_sec),2,"0") + " LOCAL"
  ProcedureReturn date
EndProcedure

Procedure.d LocalToUTC()
  Protected *time.tm, now.i, now_local.i, isdst.i, r1.d
  time(@now)
  *time = localtime(@now)
  isdst = *time\tm_isdst
  *time = gmtime(@now)
  *time\tm_isdst = isdst
  now_local = mktime(*time)
  r1 = difftime(now, now_local) 
  ProcedureReturn r1
EndProcedure

Debug UTC_String()
Debug LocalTime_String()
Debug LocalToUTC()
Last edited by mk-soft on Mon Jul 09, 2018 7:14 pm, edited 2 times in total.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: UTC Time

Post by wilbert »

mk-soft wrote:But difftime no right result
double difftime (time_t end, time_t beginning);
end and beginning need to be time_t .
localtime and gmtime return a tm structure so you are passing the wrong things to the difftime function.

Maybe use something like this

Code: Select all

ImportC ""
  difftime.d(time1, time0)
  gmtime(*timep)
  time(*t)
  timelocal(*tm)
EndImport

time(@now)
*gmtime = gmtime(@now)
now_local = timelocal(*gmtime)

Debug difftime(now_local, now)
Debug now_local - now
Edit:
I see timelocal is not available on Windows :?
Windows (x64)
Raspberry Pi OS (Arm64)
User avatar
mk-soft
Always Here
Always Here
Posts: 6204
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: UTC Time

Post by mk-soft »

Ok...

Thanks wilbert :wink:
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
ZX80
Enthusiast
Enthusiast
Posts: 361
Joined: Mon Dec 12, 2016 1:37 pm

Re: UTC Time

Post by ZX80 »

mk-soft
This work with Windows, Linux and MacOS...

But difftime no right result
okay. i have another way to get difference between local and utc time. i wrote it many years ago and i use this method to get timezone infomation.
works fine for me. please check it:

Code: Select all

  TZ.s=""
  GetTimeZoneInformation_(tzi.TIME_ZONE_INFORMATION)
  tmp = -1*tzi\Bias
  TZH = tmp/60
  If TZH=0
    TZ = " UTC/GMT"
  Else
    TZM = tmp-(TZH*60)
    TZ=Str(TZH)
    If FindString(TZ,"-")
      TZ = Right(TZ, (Len(TZ)-1))
      If TZM<>0
        TZ = " UTC/GMT - "+TZ+":"+Right(Str(TZM),2)
      Else
        TZ = " UTC/GMT - "+TZ
      EndIf
    Else
      If TZM=0
        TZ = " UTC/GMT + "+TZ
      Else
        TZ = " UTC/GMT + "+TZ+":"+Str(TZM)
      EndIf
    EndIf
  EndIf
  
  Debug "TimeZone is " + TZ
For Windows ONLY!
User avatar
mk-soft
Always Here
Always Here
Posts: 6204
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: UTC Time

Post by mk-soft »

I've found a solution.
Get the value for tm_isdst from localetime before and take it over.

Tested and work with Windows, Linux and MacOS :wink:

Code: Select all

Structure tm
  tm_sec.l;   // seconds after the minute - [0, 60] including leap second
  tm_min.l;   // minutes after the hour - [0, 59]
  tm_hour.l;  // hours since midnight - [0, 23]
  tm_mday.l;  // day of the month - [1, 31]
  tm_mon.l;   // months since January - [0, 11]
  tm_year.l;  // years since 1900
  tm_wday.l;  // days since Sunday - [0, 6]
  tm_yday.l;  // days since January 1 - [0, 365]
  tm_isdst.l; // daylight savings time flag
EndStructure

ImportC ""
  time(time_t = #Null)
  localtime(*tm = #Null)
  gmtime(time_t.i)
  mktime(*tm)
  difftime.d(*tm1, *tm2)
EndImport

Procedure.d LocalToUTC()
  Protected *time.tm, now.i, now_local.i, isdst.i, r1.d
  time(@now)
  *time = localtime(@now)
  isdst = *time\tm_isdst
  *time = gmtime(@now)
  *time\tm_isdst = isdst
  now_local = mktime(*time)
  r1 = difftime(now, now_local) 
  ProcedureReturn r1
EndProcedure

Debug LocalToUTC()
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: UTC Time

Post by idle »

ts-soft wrote:I have utc in my computer-bios, so your code shows the false value :)

Image
If I change my system time to display UTC , I still get the correct value :?
Windows 11, Manjaro, Raspberry Pi OS
Image
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: UTC Time

Post by idle »

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))
must admit I'm a little more confused than I was before since time(0) in FormatDate returns UTC?
can that be assumed to be right? or is formatdate doing the conversion?

and thanks mk-soft for localTime_String() and LocalToUTC()
Windows 11, Manjaro, Raspberry Pi OS
Image
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: UTC Time

Post by wilbert »

idle wrote:must admit I'm a little more confused than I was before since time(0) in FormatDate returns UTC?
can that be assumed to be right? or is formatdate doing the conversion?
Yes, it can be assumed right and no, formatdate doesn't do the conversion.
It's just that time() returns a utc timestamp and Date() returns a local timestamp.
Windows (x64)
Raspberry Pi OS (Arm64)
Little John
Addict
Addict
Posts: 4777
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: UTC Time

Post by Little John »

Just a little addition (with many thanks again to wilbert for this information!):

Code: Select all

EnableExplicit

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

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

Macro UTC()
   ; out: current UTC in PureBasic format (seconds since 1970-01-01)
   time()
EndMacro
; -------------------------------------------------------------------

Define.i local, universal

local = Date()
universal = UTC()

Debug (local - universal) / 3600   ; -> 2  (CEST = UTC + 2 hours)
Last edited by Little John on Tue Jul 10, 2018 6:16 am, edited 1 time in total.
User avatar
idle
Always Here
Always Here
Posts: 5836
Joined: Fri Sep 21, 2007 5:52 am
Location: New Zealand

Re: UTC Time

Post by idle »

nice and easy
Windows 11, Manjaro, Raspberry Pi OS
Image
Post Reply