How to Read computer's time zone ?

General FreeBASIC programming questions.
Post Reply
Vinion
Posts: 19
Joined: Sep 08, 2022 6:27

How to Read computer's time zone ?

Post by Vinion »

Hello,

Is there a possible way to read the computer's time zone ?
For example UTC+2, UTC+3 .etc ?

Thank you
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: How to Read computer's time zone ?

Post by caseih »

In Linux you can do:

Code: Select all

#include "crt/time.bi"

dim as time_t t
dim as tm lt

localtime_r(@t, @lt)

print "gmt offset is "; lt.__tm_gmtoff / 3600
print "time zone is "; *lt.__tm_zone
In Windows, there's a Win32 api call for this:
https://learn.microsoft.com/en-us/windo ... nformation
Here's a demo of that:

Code: Select all

#include "windows.bi"

dim as DWORD result
dim as TIME_ZONE_INFORMATION ti

result = GetTimeZoneInformation(@ti)

print "Time zone is UTC";ti.bias / -60 'utc is bias seconds + localtime
select case result
	case 1
		print "Standard time."
	case 2
		print "Daylight savings time."
end select
Vinion
Posts: 19
Joined: Sep 08, 2022 6:27

Re: How to Read computer's time zone ?

Post by Vinion »

caseih wrote: Sep 27, 2022 13:09 In Linux you can do:

Code: Select all

#include "crt/time.bi"

dim as time_t t
dim as tm lt

localtime_r(@t, @lt)

print "gmt offset is "; lt.__tm_gmtoff / 3600
print "time zone is "; *lt.__tm_zone
In Windows, there's a Win32 api call for this:
https://learn.microsoft.com/en-us/windo ... nformation
Here's a demo of that:

Code: Select all

#include "windows.bi"

dim as DWORD result
dim as TIME_ZONE_INFORMATION ti

result = GetTimeZoneInformation(@ti)

print "Time zone is UTC";ti.bias / -60 'utc is bias seconds + localtime
select case result
	case 1
		print "Standard time."
	case 2
		print "Daylight savings time."
end select
Thank you for your reply. I implemented both solutions you suggested to my framework https://github.com/nsiatras/kiwi/wiki/k ... e.Calendar

Windows solution works and I tested it. Unfortunately I cannot test the Linux one but I believe it is correct.

Thanks again
Post Reply