Page 1 of 2
UTC Time
Posted: Mon Jul 09, 2018 10:22 am
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()
Re: UTC Time
Posted: Mon Jul 09, 2018 12:13 pm
by RASHAD
Hi idle
KSA : Yes it is OK
Re: UTC Time
Posted: Mon Jul 09, 2018 12:44 pm
by ts-soft
I have utc in my computer-bios, so your code shows the false value

Re: UTC Time
Posted: Mon Jul 09, 2018 2:57 pm
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))
Re: UTC Time
Posted: Mon Jul 09, 2018 3:40 pm
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
Re: UTC Time
Posted: Mon Jul 09, 2018 5:01 pm
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()
Re: UTC Time
Posted: Mon Jul 09, 2018 5:33 pm
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
Re: UTC Time
Posted: Mon Jul 09, 2018 6:18 pm
by mk-soft
Ok...
Thanks wilbert

Re: UTC Time
Posted: Mon Jul 09, 2018 6:28 pm
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!
Re: UTC Time
Posted: Mon Jul 09, 2018 7:21 pm
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
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()
Re: UTC Time
Posted: Mon Jul 09, 2018 10:17 pm
by idle
ts-soft wrote:I have utc in my computer-bios, so your code shows the false value

If I change my system time to display UTC , I still get the correct value

Re: UTC Time
Posted: Mon Jul 09, 2018 11:47 pm
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()
Re: UTC Time
Posted: Tue Jul 10, 2018 5:59 am
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.
Re: UTC Time
Posted: Tue Jul 10, 2018 6:10 am
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)
Re: UTC Time
Posted: Tue Jul 10, 2018 6:15 am
by idle
nice and easy