Jump to content

Convert_LT_to_UTC


Recommended Posts

Is there a way to convert a (any) date/time combination from local time to UTC or vice versa?

I can’t find a way to do that except by changing the date/time of my system (see code example). I don’t like that too much. Does anyone have a working solution?

On my PC, I have also a strange result with the example of function _Date_Time_GetTimeZoneInformation( )

Result ............: 2

Current bias ......: -60

Standard name .....: Romance Standard Time

Standard date/time : 10/05/0000 03:00:00

Standard bias......: 0

Daylight name .....: Romance Daylight Time

Daylight date/time : 03/05/0000 02:00:00

Daylight bias......: -60

Why is the date/time wrong? I changed the date format from my local setting to US but don’t make any difference. What did I forget?

GreenCan

#Include <Date.au3>

$TZ = _Date_Time_GetTimeZoneInformation()

ConsoleWrite("Result ............: " & $TZ[0] & @CR)
ConsoleWrite("Current bias ......: " & $TZ[1] & @CR)
ConsoleWrite("Standard name .....: " & $TZ[2] & @CR)
ConsoleWrite("Standard date/time : " & _Date_Time_SystemTimeToDateTimeStr ($TZ[3]) & @CR)
ConsoleWrite("Standard bias......: " & $TZ[4] & @CR)
ConsoleWrite("Daylight name .....: " & $TZ[5] & @CR)
ConsoleWrite("Daylight date/time : " & _Date_Time_SystemTimeToDateTimeStr ($TZ[6]) & @CR)
ConsoleWrite("Daylight bias......: " & $TZ[7] & @CR)

; strings used for cosmetic reason, numbers will work fine too

$_Day = "27"
$_Month = "03"
$_Year = "2010"
$_Time = "02:59"
ConsoleWrite ("Local time : " & $_Month & "/" & $_Day & "/" & $_Year & " " & $_Time & ":00" & " ==> "  & "UTC: " & Convert_to_UTC($_Day, $_Month, $_Year, $_Time) & @CR)

$_Day = "28"
$_Month = "03"
$_Year = "2010"
$_Time = "03:00"
ConsoleWrite ("Local time : " & $_Month & "/" & $_Day & "/" & $_Year & " " & $_Time & ":00" & " ==> "  & "UTC: " & Convert_to_UTC($_Day, $_Month, $_Year, $_Time) & @CR)

$_Day = 10
$_Month = 12
$_Year = 2008
$_Time = "22:00"
ConsoleWrite ("Local time : " & $_Month & "/" & $_Day & "/" & $_Year & " " & $_Time & ":00" & " ==> "  & "UTC: " & Convert_to_UTC($_Day, $_Month, $_Year, $_Time) & @CR)

$_Day = "15"
$_Month = "05"
$_Year = "2007"
$_Time = "18:23"
ConsoleWrite ("Local time : " & $_Month & "/" & $_Day & "/" & $_Year & " " & $_Time & ":00" & " ==> "  & "UTC: " & Convert_to_UTC($_Day, $_Month, $_Year, $_Time) & @CR)

Exit

#FUNCTION# ==============================================================
Func Convert_to_UTC(ByRef $_Day, ByRef $_Month, ByRef $_Year, ByRef $_Time)
    ; the function changes temporarely the system data/time to passed arguments
    ; sets the system date back to the orignal date and returns the UTC date/time in MM/DD/YYYY hh:mm:ss format
    Local $_hour = Number(StringLeft($_Time,2))
    Local $_min = Number(StringRight($_Time,2))
    Local $_sec = 0
    ; Keep current local time
    Local $tCur = _Date_Time_GetLocalTime()
    ; Set new local time on PC
    Local $tNew = _Date_Time_EncodeSystemTime($_Month, $_Day, $_Year, $_hour, $_min, $_sec)
    _Date_Time_SetLocalTime(DllStructGetPtr($tNew))
    $tNew = _Date_Time_GetLocalTime()
    Local $tTime = _Date_Time_GetSystemTime()
    ; Reset local time
    _Date_Time_SetLocalTime(DllStructGetPtr($tCur))
    ; return UTC time
    Return (_Date_Time_SystemTimeToDateTimeStr($tTime) )
EndFunc ;==>Convert_to_UTC
#FUNCTION# ==============================================================

Contributions

CheckUpdate - SelfUpdating script ------- Self updating script

Dynamic input validation ------------------- Use a Input masks can make your life easier and Validation can be as simple

MsgBox with CountDown ------------------- MsgBox with visual countdown

Display Multiline text cells in ListView ---- Example of pop-up or ToolTip for multiline text items in ListView

Presentation Manager ---------------------- Program to display and refresh different Border-less GUI's on a Display (large screen TV)

USB Drive Tools ------------------------------ Tool to help you with your USB drive management

Input Period udf ------------------------------ GUI for a period input

Excel ColorPicker ---------------------------- Color pickup tool will allow you to select a color from the standard Excel color palette

Excel Chart UDF ----------------------------- Collaboration project with water 

GetDateInString ------------------------------ Find date/time in a string using a date format notation like DD Mon YYYY hh:mm

TaskListAllDetailed --------------------------- List All Scheduled Tasks

Computer Info --------------------------------- A collection of information for helpdesk

Shared memory Demo ----------------------- Demo: Two applications communicate with each other through means of a memory share (using Nomad function, 32bit only)

Universal Date Format Conversion -------- Universal date converter from your PC local date format to any format

Disable Windows DetailsPane -------------- Disable Windows Explorer Details Pane

Oracle SQL Report Generator -------------  Oracle Report generator using SQL

SQLite Report Generator -------------------  SQLite Report generator using SQL

SQLite ListView and BLOB demo ---------- Demo: shows how binary (image) objects can be recognized natively in a database BLOB field

DSN-Less Database connection demo --- Demo: ActiveX Data Objects DSN-Less Database access

Animated animals ----------------------------- Fun: Moving animated objects

Perforated image in GUI --------------------- Fun: Perforate your image with image objects

UEZ's Perforator major update ------------- Fun: Pro version of Perforator by UEZ

Visual Crop Tool (GUI) ----------------------- Easy to use Visual Image Crop tool

Visual Image effect (GUI) -------------------- Visually apply effects on an image

 

 

 

Link to comment
Share on other sites

I'm not sure if this would work, as you seem to be getting odd results from your _Date_Time_GetTimeZoneInformation( ), or I could just be reading it wrong (I haven't played with that function yet), but once you get the offset, it should be fairly easy to use this to do the conversion:

_DateAdd("h", $UTC_OFFSET, _NowCalcDate())

Edited by Fulano

#fgpkerw4kcmnq2mns1ax7ilndopen (Q, $0); while ($l = <Q>){if ($l =~ m/^#.*/){$l =~ tr/a-z1-9#/Huh, Junketeer's Alternate Pro Ace /; print $l;}}close (Q);[code] tag ninja!

Link to comment
Share on other sites

Is there a way to convert a (any) date/time combination from local time to UTC or vice versa?

The short answer to the question above is: NO, that is completely impossible.

The problem is with definitions and politicians.

(I understand this short answer doesn't please you!)

Being careful at what you do, you should be able to get an approximation using the functions you cite, but you really have to be sure of your settings, or you'll get garbage.

If you need anything precise, you need to first learn more about time and the related (hard) problems.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

_DateAdd("h", $UTC_OFFSET, _NowCalcDate())

Thanks Fulano,

Yes, I know the function but that doesn't help. the problem is that _Date_Time_GetTimeZoneInformation() returns the TZ information for today only.

Lets imagine that I want to convert Feb 10, 2010 12:10 from LT to UTC.

The conversion for the wintertime will be wrong because _Date_Time_GetTimeZoneInformation() returns a DST bias (-60 + -60 in my case)

I still need to find out at what moment (here 03/28/2010 03:00) the DST is activated.

03/28/2010 02:59 UTC +1

03/28/2010 03:00 UTC +2

As there is no mathematical formula to calculate DST (The problem is with definitions and politicians as jchd is referring to) , the only way it can work is by creating a cross reference table like the one hereunder, but this is heavy maintenance...

Year;DST from;DST until;Time Zone;Offset Winter;Offset Summer

2010;28-Mar-2010;31-Oct-2010;DK;60;120

2011;27-Mar-2011;30-Oct-2011;DK;60;120

2010;01-Jan-2010;31-Dec-2010;EG;120;120

2010;28-Mar-2010;31-Oct-2010;ES2;0;60

2011;27-Mar-2011;30-Oct-2011;ES2;0;60

2010;28-Mar-2010;31-Oct-2010;TR;120;180

2011;27-Mar-2011;30-Oct-2011;TR;120;180

2010;28-Mar-2010;31-Oct-2010;TR;120;180

2011;27-Mar-2011;30-Oct-2011;TR;120;180

2010;25-Mar-2010;29-Oct-2010;JO;120;180

2011;31-Mar-2011;28-Oct-2011;JO;120;180

BTW, the same result will be given by _Date_Time_GetSystemTime() except for the date format which will be returned in mm/dd/yyyy

Greencan

$TZ = _Date_Time_GetTimeZoneInformation()
If $TZ[0] = 1 Then
    $TotalBias = $TZ[1]
ElseIf $TZ[0] = 2 Then
    $TotalBias = $TZ[1]+$TZ[7]
Else
    $TotalBias = 0
EndIf
ConsoleWrite("Current date: " & _NowCalc () & @CR)
ConsoleWrite("UTC         : " & _DateAdd("n", $TotalBias, _NowCalc ()) & @CR)

; exactly same result but with different date format
$Cur = _Date_Time_GetSystemTime()
ConsoleWrite("UTC         : " & _Date_Time_SystemTimeToDateTimeStr($Cur) & @CR)

Contributions

CheckUpdate - SelfUpdating script ------- Self updating script

Dynamic input validation ------------------- Use a Input masks can make your life easier and Validation can be as simple

MsgBox with CountDown ------------------- MsgBox with visual countdown

Display Multiline text cells in ListView ---- Example of pop-up or ToolTip for multiline text items in ListView

Presentation Manager ---------------------- Program to display and refresh different Border-less GUI's on a Display (large screen TV)

USB Drive Tools ------------------------------ Tool to help you with your USB drive management

Input Period udf ------------------------------ GUI for a period input

Excel ColorPicker ---------------------------- Color pickup tool will allow you to select a color from the standard Excel color palette

Excel Chart UDF ----------------------------- Collaboration project with water 

GetDateInString ------------------------------ Find date/time in a string using a date format notation like DD Mon YYYY hh:mm

TaskListAllDetailed --------------------------- List All Scheduled Tasks

Computer Info --------------------------------- A collection of information for helpdesk

Shared memory Demo ----------------------- Demo: Two applications communicate with each other through means of a memory share (using Nomad function, 32bit only)

Universal Date Format Conversion -------- Universal date converter from your PC local date format to any format

Disable Windows DetailsPane -------------- Disable Windows Explorer Details Pane

Oracle SQL Report Generator -------------  Oracle Report generator using SQL

SQLite Report Generator -------------------  SQLite Report generator using SQL

SQLite ListView and BLOB demo ---------- Demo: shows how binary (image) objects can be recognized natively in a database BLOB field

DSN-Less Database connection demo --- Demo: ActiveX Data Objects DSN-Less Database access

Animated animals ----------------------------- Fun: Moving animated objects

Perforated image in GUI --------------------- Fun: Perforate your image with image objects

UEZ's Perforator major update ------------- Fun: Pro version of Perforator by UEZ

Visual Crop Tool (GUI) ----------------------- Easy to use Visual Image Crop tool

Visual Image effect (GUI) -------------------- Visually apply effects on an image

 

 

 

Link to comment
Share on other sites

Thanks jchd,

I am pleased that you replied :(, I am less pleased that my fear has been confirmed.

Contributions

CheckUpdate - SelfUpdating script ------- Self updating script

Dynamic input validation ------------------- Use a Input masks can make your life easier and Validation can be as simple

MsgBox with CountDown ------------------- MsgBox with visual countdown

Display Multiline text cells in ListView ---- Example of pop-up or ToolTip for multiline text items in ListView

Presentation Manager ---------------------- Program to display and refresh different Border-less GUI's on a Display (large screen TV)

USB Drive Tools ------------------------------ Tool to help you with your USB drive management

Input Period udf ------------------------------ GUI for a period input

Excel ColorPicker ---------------------------- Color pickup tool will allow you to select a color from the standard Excel color palette

Excel Chart UDF ----------------------------- Collaboration project with water 

GetDateInString ------------------------------ Find date/time in a string using a date format notation like DD Mon YYYY hh:mm

TaskListAllDetailed --------------------------- List All Scheduled Tasks

Computer Info --------------------------------- A collection of information for helpdesk

Shared memory Demo ----------------------- Demo: Two applications communicate with each other through means of a memory share (using Nomad function, 32bit only)

Universal Date Format Conversion -------- Universal date converter from your PC local date format to any format

Disable Windows DetailsPane -------------- Disable Windows Explorer Details Pane

Oracle SQL Report Generator -------------  Oracle Report generator using SQL

SQLite Report Generator -------------------  SQLite Report generator using SQL

SQLite ListView and BLOB demo ---------- Demo: shows how binary (image) objects can be recognized natively in a database BLOB field

DSN-Less Database connection demo --- Demo: ActiveX Data Objects DSN-Less Database access

Animated animals ----------------------------- Fun: Moving animated objects

Perforated image in GUI --------------------- Fun: Perforate your image with image objects

UEZ's Perforator major update ------------- Fun: Pro version of Perforator by UEZ

Visual Crop Tool (GUI) ----------------------- Easy to use Visual Image Crop tool

Visual Image effect (GUI) -------------------- Visually apply effects on an image

 

 

 

Link to comment
Share on other sites

Even if you imagine a large table, there are more incredibly hard problems to solve to obtain a precise correct answer, even if you relax the question somehow.

You have to know exactly where you are talking about, and that can be difficult.

Each country has its own rules about DST, they are not time-zone based.

In some countries, regions have their own DST rules, independant of the official country-wide time.

Countries and regions boundaries change over time. Countries and regions get merged, or split apart.

There are enclaves of countries inside others, with varying boundaries.

Some countries have or have had official or local civil time not based on UTC (weird, but real). For these, you have to use a table of leap seconds as well.

Outside of countries and disputed sea pseudo-boundaries, you should refer to time zones, but these have changed many times in placement and offset.

The obstacles are tremendous and only apply for a very limited time range. For instance, UTC started at 1972/01/01 so prior timestamps are unconvertible, but you don't have any clue about future either, so future timestamps are no more convertible. UTC is strange in that it's very useful but also introduces problems.

As a basic example, the difference between two UTC timestamps has no exact semantics in general. :(

There are serious plans to get rid of UTC and its non-linearities which, if they are ever applied, will give birth to yet another timescale to convert to/from.

This is if you "just" need approximation to few seconds. One single second may seem very short, but it can make a big difference in numerous contexts.

I agree that in most cases, a crude computation using a less than perfect simple table can work without ground-shaking consequences, but most people are surprised by the practical weirdness of human timescales.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

The problem is that _Date_Time_GetTimeZoneInformation() returns the TZ information for today only.

Ahh, my bad, I misunderstood the scope of the problem.

Thanks for the explanation jchd, humans are weird :(

#fgpkerw4kcmnq2mns1ax7ilndopen (Q, $0); while ($l = <Q>){if ($l =~ m/^#.*/){$l =~ tr/a-z1-9#/Huh, Junketeer's Alternate Pro Ace /; print $l;}}close (Q);[code] tag ninja!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...