Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine my time zone offset using VBScript?

How can I determine my time zone offset using VBScript?

The Windows OS provides the TZ environment variable. For Eastern Standard Time (New York), its value is EST5EDT. However, I am looking for the signed integer offset from UTC. (This is -5 for Eastern Standard Time.)

like image 271
DavidRR Avatar asked Dec 20 '12 21:12

DavidRR


People also ask

How do I find the offset of time zones?

You can get the offset of a timezone using ZonedDateTime#getOffset . Note that the offset of a timezone that observes DST changes as per the changes in DST. For other places (e.g. India), it remains fixed. Therefore, it is recommended to mention the moment when the offset of a timezone is shown.

What is offset in time zone?

What is a "zone offset"? A zone offset is the difference in hours and minutes between a particular time zone and UTC. In ISO 8601, the particular zone offset can be indicated in a date or time value. The zone offset can be Z for UTC or it can be a value "+" or "-" from UTC.

How many timezone offsets are there?

Time zones There are currently 38 observed UTC offsets in the world (37 when Iran is on daylight saving time).


1 Answers

Here is a revised function that appears to account for Daylight Saving Time. (Inspired by this SO question.)

Function GetTimeZoneOffset()
    Const sComputer = "."

    Dim oWmiService : Set oWmiService = _
        GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
                  & sComputer & "\root\cimv2")

    Set cItems = oWmiService.ExecQuery("SELECT * FROM Win32_ComputerSystem")

    For Each oItem In cItems
        GetTimeZoneOffset = oItem.CurrentTimeZone / 60
        Exit For
    Next
End Function

[Original function that does NOT account for Daylight Saving Time.]

Here is my answer to my question (original source).

For Eastern Standard Time (New York), this VBScript function will return -5:

Function GetTimeZoneOffset()
    Const sComputer = "."

    Dim oWmiService : Set oWmiService = _
        GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
                  & sComputer & "\root\cimv2")

    Dim cTimeZone : Set cTimeZone = _
        oWmiService.ExecQuery("Select * from Win32_TimeZone")

    Dim oTimeZone
    For Each oTimeZone in cTimeZone
        GetTimeZoneOffset = oTimeZone.Bias / 60
        Exit For
    Next
End Function
like image 184
DavidRR Avatar answered Sep 30 '22 17:09

DavidRR