Page 1 of 2

TimeZoneOffset()

Posted: Sat Nov 15, 2008 10:30 am
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

Posted: Tue Nov 18, 2008 11:04 pm
by ABBKlaus
Thanks for that, i just needed it in PurePDF :wink:

Re: TimeZoneOffset()

Posted: Sun Nov 16, 2014 12:10 pm
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()

Re: TimeZoneOffset()

Posted: Sun Nov 16, 2014 9:46 pm
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$


Re: TimeZoneOffset()

Posted: Mon Nov 17, 2014 5:27 am
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()

Re: TimeZoneOffset()

Posted: Mon Nov 17, 2014 6:12 am
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"


Re: TimeZoneOffset()

Posted: Mon Nov 17, 2014 8:32 am
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()

Re: TimeZoneOffset()

Posted: Mon Nov 17, 2014 10:27 am
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 !

Re: TimeZoneOffset()

Posted: Mon Nov 17, 2014 1:08 pm
by Thunder93
Your welcome. :)

Re: TimeZoneOffset()

Posted: Tue Nov 18, 2014 4:40 am
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 ""

Re: TimeZoneOffset()

Posted: Tue Nov 18, 2014 5:00 am
by Thunder93
I like the simplicity and convenience of mine. :twisted:

Re: TimeZoneOffset()

Posted: Wed Dec 03, 2014 6:59 pm
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

Re: TimeZoneOffset()

Posted: Wed Dec 03, 2014 7:27 pm
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

Re: TimeZoneOffset()

Posted: Wed Dec 03, 2014 7:57 pm
by Thunder93
wDayOfWeek 0 starts at Sunday, not Monday. http://msdn.microsoft.com/en-us/library ... s.85).aspx

:wink:

Re: TimeZoneOffset()

Posted: Wed Dec 03, 2014 8:04 pm
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: