Jump to content

convert a Unix/Epoch time to local time (with DST correction)


Recommended Posts

Hello people, I have a date from a log who is set in Unix/Epoch time (like 1444077951) that I want to convert into a local time. I have try with this:

_DateAdd('s', $unixdate, "1970/01/01 00:00:00")

who work almost well because I have something like 2015/10/05 20:45:51

But actually this result is late from 2H from the time I should expect. I'm living in a TimeZone : +1 with the DST so I understand where is the problem, but how have the resultat with the local setting time ?

I have try with this function : _Date_Time_SystemTimeToTzSpecificLocalTime

But I don't understand how should I format the variable $pUTC..

Thank you
Edited by JamesPTG
Link to comment
Share on other sites

#include <Date.au3>

Global $Result = _GetHumanTimeFromUNIX("1444077951")

MsgBox(0, "", $Result)


Func _GetHumanTimeFromUNIX($UnixTime)
    Local $Seconds = $UnixTime + _GetCurrentGMTDiff() * 3600
    Return _DateAdd("s", $Seconds, "1970/01/01 00:00:00")
EndFunc

Func _GetCurrentGMTDiff()
    Local $Struct = DllStructCreate("struct;long Bias;wchar StdName[32];word StdDate[8];long StdBias;wchar DayName[32];word DayDate[8];long DayBias;endstruct")
    Local $Result = DllCall("kernel32.dll", "dword", "GetTimeZoneInformation", "struct*", $Struct)
    If @error Or $Result[0] = -1 Then Return SetError(@error, @extended, 0)

    Return $Result[0]
EndFunc

 

Link to comment
Share on other sites

An other way...

#include <Date.au3>

$epoch = 1444077951 
$aCall = DllCall("msvcrt.dll", "str:cdecl", "ctime", "int*", $epoch) 
$tmp = StringSplit($aCall[0], " ")
For $i = 1 to 12
   If $tmp[2] = _DateToMonth($i, $DMW_SHORTNAME) Then $tmp[2] = $i
Next
$date = StringFormat("%4i/%02i/%02i %8s", $tmp[5], $tmp[2], $tmp[3], $tmp[4])
msgbox(0,"", $date)

 

Link to comment
Share on other sites

  • 1 year later...

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...