TimeZoneOffset()

Share your advanced PureBasic knowledge/code with the community.
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

TimeZoneOffset()

Post by Rescator »

Code: Select all

EnableExplicit

Procedure.l TimeZoneOffset()
 Protected result.l,mode.l,tz.TIME_ZONE_INFORMATION
 mode=GetTimeZoneInformation_(tz)
 If mode=#TIME_ZONE_ID_STANDARD
  result-tz\bias-tz\StandardBias
 ElseIf mode=#TIME_ZONE_ID_DAYLIGHT
  result-tz\bias-tz\DaylightBias
 Else
  result-tz\Bias
 EndIf
 ProcedureReturn result*60
EndProcedure

Define tz.l,utc$,local$

tz=TimeZoneOffset()

local$=FormatDate("%yyyy-%mm-%dd %hh:%ii:%ss",Date())
utc$=FormatDate("%yyyy-%mm-%dd %hh:%ii:%ss UTC",Date()-tz)

Debug "Offset="+Str(TimeZoneOffset())+" (relative to UTC in seconds)"
Debug local$
Debug utc$
This is actually a slightly improved + example version of: http://www.purebasic.fr/english/viewtopic.php?t=23172
Last edited by Rescator on Sun Nov 16, 2014 1:19 pm, edited 2 times in total.
ABBKlaus
Addict
Addict
Posts: 1143
Joined: Sat Apr 10, 2004 1:20 pm
Location: Germany

Post by ABBKlaus »

Thanks for that, i just needed it in PurePDF :wink:
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Re: TimeZoneOffset()

Post by Rescator »

Fixed the example, I tested it an found it added rather than subtracted, no clue why; anyway, it's fixed now.
No changes was made to TimeZoneOffset()
User avatar
VB6_to_PBx
Enthusiast
Enthusiast
Posts: 627
Joined: Mon May 09, 2011 9:36 am

Re: TimeZoneOffset()

Post by VB6_to_PBx »

is there an easier method to get "normal Time" with AM and PM
than my Code below ???

If Val(FormatDate("%hh",Date())) => 13
local$ = Str(Val(FormatDate("%hh",Date())) - 12) + ":" + FormatDate("%ii",Date()) + ":" + FormatDate("%ss",Date()) + " PM"
Else
local$ = Str(Val(FormatDate("%hh",Date()))) + ":" + FormatDate("%ii",Date()) + ":" + FormatDate("%ss",Date()) + " AM"
EndIf



Code: Select all

EnableExplicit

Procedure.l TimeZoneOffset()

    Protected result.l,mode.l,tz.TIME_ZONE_INFORMATION

    mode = GetTimeZoneInformation_(tz)

    If mode = #TIME_ZONE_ID_STANDARD
         result - tz\bias-tz\StandardBias
    ElseIf mode = #TIME_ZONE_ID_DAYLIGHT
         result - tz\bias-tz\DaylightBias
    Else
         result - tz\Bias
    EndIf

    ProcedureReturn result * 60

EndProcedure

Define tz.l,utc$,local$

tz = TimeZoneOffset()

local$ = FormatDate("%yyyy-%mm-%dd %hh:%ii:%ss",Date())
Debug local$

If Val(FormatDate("%hh",Date())) => 13
    local$ = Str(Val(FormatDate("%hh",Date())) - 12) + ":" + FormatDate("%ii",Date()) + ":" + FormatDate("%ss",Date()) + " PM"
Else
    local$ = Str(Val(FormatDate("%hh",Date()))) + ":" + FormatDate("%ii",Date()) + ":" + FormatDate("%ss",Date()) + " AM"
EndIf

utc$ = FormatDate("%yyyy-%mm-%dd %hh:%ii:%ss UTC",Date()-tz)

Debug "Offset=" + Str(TimeZoneOffset()) + " (relative to UTC in seconds)"
Debug local$
Debug utc$

 
PureBasic .... making tiny electrons do what you want !

"With every mistake we must surely be learning" - George Harrison
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: TimeZoneOffset()

Post by Thunder93 »

What you think VB6_to_PBx about the following code?

Code: Select all

Procedure.l TimeZoneOffset()
  lpUniversalTime.SYSTEMTIME
  
  If GetSystemTime_(lpUniversalTime)
    TimeZoneOffset = Date(lpUniversalTime\wYear, lpUniversalTime\wMonth, lpUniversalTime\wDay, lpUniversalTime\wHour, lpUniversalTime\wMinute, lpUniversalTime\wSecond)
    ProcedureReturn Date()-TimeZoneOffset
  EndIf  
EndProcedure

Define tz.l = TimeZoneOffset(), tDate = Date(), utc$, local$

local$ = FormatDate("%yyyy.%mm.%dd %hh:%ii:%ss",  tDate)  
utc$ = FormatDate("%yyyy.%mm.%dd %hh:%ii:%ss UTC",  tDate-tz)


Debug "Offset="+Str(tz)+" (relative to UTC in seconds)"
Debug local$
Debug utc$

;;;;;
Procedure.s  Normal_Time()
  tDate = Date()
  local$ = FormatDate("%yyyy.%mm.%dd %hh:%ii:%ss",  tDate) 
  If Hour(tDate) > 11
    local$ + " PM"
  Else
    local$ + " AM"
  EndIf
  ProcedureReturn local$
EndProcedure

Debug Normal_Time()
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
User avatar
VB6_to_PBx
Enthusiast
Enthusiast
Posts: 627
Joined: Mon May 09, 2011 9:36 am

Re: TimeZoneOffset()

Post by VB6_to_PBx »

Thunder93 wrote:What you think VB6_to_PBx about the following code?

Code: Select all

Procedure.l TimeZoneOffset()
  lpUniversalTime.SYSTEMTIME
  
  If GetSystemTime_(lpUniversalTime)
    TimeZoneOffset = Date(lpUniversalTime\wYear, lpUniversalTime\wMonth, lpUniversalTime\wDay, lpUniversalTime\wHour, lpUniversalTime\wMinute, lpUniversalTime\wSecond)
    ProcedureReturn Date()-TimeZoneOffset
  EndIf  
EndProcedure

Define tz.l = TimeZoneOffset(), tDate = Date(), utc$, local$

local$ = FormatDate("%yyyy.%mm.%dd %hh:%ii:%ss",  tDate)  
utc$ = FormatDate("%yyyy.%mm.%dd %hh:%ii:%ss UTC",  tDate-tz)


Debug "Offset="+Str(tz)+" (relative to UTC in seconds)"
Debug local$
Debug utc$

;;;;;
Procedure.s  Normal_Time()
  tDate = Date()
  local$ = FormatDate("%yyyy.%mm.%dd %hh:%ii:%ss",  tDate) 
  If Hour(tDate) > 11
    local$ + " PM"
  Else
    local$ + " AM"
  EndIf
  ProcedureReturn local$
EndProcedure

Debug Normal_Time()
it seems to only give "AM and PM" correctly , but it still displays 24 Hour or Military Time

i changed this Code up slightly to :

Debug ""
Debug "Normal Clock Time = " + local$
Debug ""


If Val(FormatDate("%hh",Date())) = 0
local$ = Str(Val(FormatDate("%hh",Date())) + 12) + ":" + FormatDate("%ii",Date()) + ":" + FormatDate("%ss",Date()) + " AM"
EndIf


and added this :
Debug "Hour Time Offset = " + Str(TimeZoneOffset()/3600) + " Hours"


the Code below works correctly , but i thought PureBasic would have a built-in shorter Format Time ???

Code: Select all

EnableExplicit

Procedure.l TimeZoneOffset()

    Protected result.l,mode.l,tz.TIME_ZONE_INFORMATION

    mode = GetTimeZoneInformation_(tz)

    If mode = #TIME_ZONE_ID_STANDARD
         result - tz\bias-tz\StandardBias
    ElseIf mode = #TIME_ZONE_ID_DAYLIGHT
         result - tz\bias-tz\DaylightBias
    Else
         result - tz\Bias
    EndIf

    ProcedureReturn result * 60

EndProcedure

Define tz.l,utc$,local$

tz = TimeZoneOffset()

local$ = FormatDate("%yyyy-%mm-%dd %hh:%ii:%ss",Date())
Debug local$

If Val(FormatDate("%hh",Date())) => 13
    local$ = Str(Val(FormatDate("%hh",Date())) - 12) + ":" + FormatDate("%ii:%ss",Date()) + " PM"
Else
    local$ = Str(Val(FormatDate("%hh",Date()))) + ":" + FormatDate("%ii:%ss",Date()) + " AM"
EndIf

If Val(FormatDate("%hh",Date())) = 0
    local$ = Str(Val(FormatDate("%hh",Date())) + 12) + ":" + FormatDate("%ii:%ss",Date()) + " AM"
EndIf



Debug ""
Debug "Normal Clock Time = " + local$
Debug ""

utc$ = FormatDate("%yyyy-%mm-%dd %hh:%ii:%ss UTC",Date()-tz)
Debug "Offset=" + Str(TimeZoneOffset()) + " (relative to UTC in seconds)"
Debug utc$
Debug ""
Debug "Hour Time Offset = " + Str(TimeZoneOffset()/3600) + " Hours"

Last edited by VB6_to_PBx on Mon Nov 17, 2014 11:04 am, edited 1 time in total.
 
PureBasic .... making tiny electrons do what you want !

"With every mistake we must surely be learning" - George Harrison
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: TimeZoneOffset()

Post by Thunder93 »

Now I compute. .. Your versions shows 0 hour when it should be showing 12AM or 12PM.

I've made some changes to my Normal_Time() procedure.

Code: Select all

Procedure.s  Normal_Time()  
  tDate = Date()
  Hour.b = Hour(tDate)
  
  If Hour < 12 : meridiem$ = "A.M."
  ElseIf Hour > 11 : meridiem$ = "P.M." 
  EndIf  
  
  If Hour % 24 = 0 : Hour = 12 : EndIf   
  If Hour >= 13 : Hour-12 : EndIf
  
  ProcedureReturn FormatDate("%yyyy.%mm.%dd ",  tDate) + Hour + FormatDate(":%ii:%ss "+meridiem$,  tDate)
EndProcedure

Debug Normal_Time()
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
User avatar
VB6_to_PBx
Enthusiast
Enthusiast
Posts: 627
Joined: Mon May 09, 2011 9:36 am

Re: TimeZoneOffset()

Post by VB6_to_PBx »

Thunder93 wrote:Now I compute. .. Your versions shows 0 hour when it should be showing 12AM or 12PM.

I've made some changes to my Normal_Time() procedure.

Code: Select all

Procedure.s  Normal_Time()  
  tDate = Date()
  Hour.b = Hour(tDate)
  
  If Hour < 12 : meridiem$ = "A.M."
  ElseIf Hour > 11 : meridiem$ = "P.M." 
  EndIf  
  
  If Hour % 24 = 0 : Hour = 12 : EndIf   
  If Hour >= 13 : Hour-12 : EndIf
  
  ProcedureReturn FormatDate("%yyyy.%mm.%dd ",  tDate) + Hour + FormatDate(":%ii:%ss "+meridiem$,  tDate)
EndProcedure

Debug Normal_Time()

Thunder93 ,
Thanks for your new Code !
 
PureBasic .... making tiny electrons do what you want !

"With every mistake we must surely be learning" - George Harrison
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: TimeZoneOffset()

Post by Thunder93 »

Your welcome. :)
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
User avatar
VB6_to_PBx
Enthusiast
Enthusiast
Posts: 627
Joined: Mon May 09, 2011 9:36 am

Re: TimeZoneOffset()

Post by VB6_to_PBx »

Rescator ,

Thank you for Posting your Code , much appreciated !


and

Thunder93 ,

this would also work :

Code: Select all

Select Val(FormatDate("%hh",Date()))
Case 0 : local$ = Str(Val(FormatDate("%hh",Date())) + 12) + ":" + FormatDate("%ii:%ss",Date()) + " AM"
Case 1 To 11 : local$ = Str(Val(FormatDate("%hh",Date()))) + ":" + FormatDate("%ii:%ss",Date()) + " AM"
Case 12 :  local$ = Str(Val(FormatDate("%hh",Date()))) + ":" + FormatDate("%ii:%ss",Date()) + " PM"
Case 13 To 24 : local$ = Str(Val(FormatDate("%hh",Date())) - 12) + ":" + FormatDate("%ii:%ss",Date()) + " PM"
EndSelect

Debug ""
Debug "Normal Clock Time = " + local$
Debug ""
 
PureBasic .... making tiny electrons do what you want !

"With every mistake we must surely be learning" - George Harrison
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: TimeZoneOffset()

Post by Thunder93 »

I like the simplicity and convenience of mine. :twisted:
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
User avatar
Rescator
Addict
Addict
Posts: 1769
Joined: Sat Feb 19, 2005 5:05 pm
Location: Norway

Re: TimeZoneOffset()

Post by Rescator »

Please be aware when handling timezones that some parts of the world have half hour timezone offsets, this is why the OS uses minutes instead of hours for timezone offsets.
http://www.timeanddate.com/time/time-zo ... sting.html

Something like

Code: Select all

Debug "Hour Time Offset = " + Str(TimeZoneOffset()/3600) + " Hours"
is wrong as it would cause a half hour timezone to get rounded a half hour wrong.
A quick fix would be

Code: Select all

Debug "Hour Time Offset = " + StrF(TimeZoneOffset()/3600.0,1) + " Hours"
(for display purposes only, and you may also need to use StrD() instead of StrF() due to 32bit not being that good with precision of integer numbers larger than 32767 if I recall correctly).

Also, if you want UTC but formatted then you could do the following:

Code: Select all

EnableExplicit

Procedure.s CustomFormatDateUTC()
	Protected st.SYSTEMTIME,weekday$,month$,ampm$
	GetSystemTime_(st)
	Select st\wDayOfWeek
		Case 0
			weekday$="Sunday"
		Case 1
			weekday$="Monday"
		Case 2
			weekday$="Tuesday"
		Case 3
			weekday$="Wednesday"
		Case 4
			weekday$="Thursday"
		Case 5
			weekday$="Friday"
		Case 6
			weekday$="Saturday"
	Endselect
	Select st\wMonth
		Case 1
			month$="January"
		Case 2
			month$="Februrary"
		Case 3
			month$="Mars"
		Case 4
			month$="Aril"
		Case 5
			month$="May"
		Case 6
			month$="June"
		Case 7
			month$="July"
		Case 8
			month$="August"
		Case 9
			month$="September"
		Case 10
			month$="October"
		Case 11
			month$="November"
		Case 12
			month$="December"
	Endselect
	Select st\wHour
		Case 0 To 11
			ampm$="AM"
		Default
			ampm$="PM"
	EndSelect
	ProcedureReturn Str(st\wDay)+" "+month$+" "+Str(st\wYear)+", "+weekday$+" "+Str(st\wHour)+":"+RSet(Str(st\wMinute),2,"0")+" "+ampm$+"."
EndProcedure

Debug CustomFormatDateUTC()
No need to get the TimeZone as GetSystemTime_() gets you the UTC date and time (unaffected by timezones).
The code in the first post has one weakness in that the examples use FormatDate() and I'm just guessing here but I think FormatDate() is affected by timezone changes so if you want UTC then you should get the UTC date and time and avoid applying a timezone correction to get UTC.

If you need to use something like Date() or FormatDate() then you might need/want to do it twice to make sure no timezone change occurred while you where fetching/formatting the date.
I.e.
Get timezone
Get date
format date
get timezone
is timezone offset different than a moment ago? If so then re-format the date.

You might wonder what use GetSystemTime_() is, myself I'd use it for database entries or filenames etc as it's guaranteed to be unaffected by timezones and offsets.
I would probably in the case of a database or filename use it as a number, see code here: http://www.purebasic.fr/english/viewtop ... 12&t=61132
Last edited by Rescator on Tue Dec 09, 2014 2:27 am, edited 1 time in total.
User avatar
VB6_to_PBx
Enthusiast
Enthusiast
Posts: 627
Joined: Mon May 09, 2011 9:36 am

Re: TimeZoneOffset()

Post by VB6_to_PBx »

Debug "Hour Time Offset = " + StrF(TimeZoneOffset()/3600.0,1) + " Hours"
thanks for that Tip

Also i just ran you new Code , but i'm getting wrong results ???

3 December 2014, Thursday 18:23 PM,
but its Wednesday here at my location instead of Thursday
 
PureBasic .... making tiny electrons do what you want !

"With every mistake we must surely be learning" - George Harrison
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: TimeZoneOffset()

Post by Thunder93 »

wDayOfWeek 0 starts at Sunday, not Monday. http://msdn.microsoft.com/en-us/library ... s.85).aspx

:wink:
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
User avatar
Thunder93
Addict
Addict
Posts: 1788
Joined: Tue Mar 21, 2006 12:31 am
Location: Canada

Re: TimeZoneOffset()

Post by Thunder93 »

Another adjustment needs to be made.. on the minute.

Code: Select all

RSet(Str(st\wMinute), 2, "0")
Instead of returning 19:2 it'll now return 19:02 :wink:
ʽʽSuccess is almost totally dependent upon drive and persistence. The extra energy required to make another effort or try another approach is the secret of winning.ʾʾ --Dennis Waitley
Post Reply