Jump to content
Realm

Seconds since Epoch (a.k.a. Unix Timestamp)

Recommended Posts

Realm

After Several Minutes searching the forums for a simple solution to calculating for the Unix Time, as required for another project I am working on, I was astonished to not find one clean and universal solution. After looking over the helpfile, I noticed one simple solution given in the example with _DateDiff(). However it did not account for my current timezone or daylight savings time.

Without further ado... This is my example of Calculating for a current Unix Time stamp or  seconds since Jan, 1st, 1970 00:00:00 GMT.

This function will account for your timezone as well as for daylight savings time.

Update: v0.3

-Added Function to revert a Unix Time stamp into Date form.

-Added parameter to original _GetUnixTime() allowing for a custom date since Jan 1st 1970 to be passed and converted to Unix Time.

#include <Date.au3>
#include <Array.au3>
#include <Constants.au3>

Local $iUnixTime1 = _GetUnixTime()
MsgBox($MB_SYSTEMMODAL, "Unix Timestamp", "Seconds Since Jan, 1st, 1970 00:00:00 GMT" & @CRLF & $iUnixTime1)

Local $sUnixDate1 = _GetDate_fromUnixTime($iUnixTime1)
MsgBox($MB_SYSTEMMODAL, "Unix Timestamp", "Get Date from Unix Timestamp in Local Time" & @CRLF & $sUnixDate1)

$sUnixDate1 = _GetDate_fromUnixTime($iUnixTime1, False)
MsgBox($MB_SYSTEMMODAL, "Unix Timestamp", "Get Date from Unix Timestamp with $iReturnLocal = False which returns UTC Time" & @CRLF & $sUnixDate1)

Local $iUnixTime2 = _GetUnixTime('2013/01/01 00:00:00')
MsgBox($MB_SYSTEMMODAL, "Unix Timestamp", "Seconds since 2013/01/01 00:00:00" & @CRLF & $iUnixTime2)

; Get timestamp for input datetime (or current datetime).
Func _GetUnixTime($sDate = 0);Date Format: 2013/01/01 00:00:00 ~ Year/Mo/Da Hr:Mi:Se

    Local $aSysTimeInfo = _Date_Time_GetTimeZoneInformation()
    Local $utcTime = ""

    If Not $sDate Then $sDate = _NowCalc()

    If Int(StringLeft($sDate, 4)) < 1970 Then Return ""

    If $aSysTimeInfo[0] = 2 Then ; if daylight saving time is active
        $utcTime = _DateAdd('n', $aSysTimeInfo[1] + $aSysTimeInfo[7], $sDate) ; account for time zone and daylight saving time
    Else
        $utcTime = _DateAdd('n', $aSysTimeInfo[1], $sDate) ; account for time zone
    EndIf

    Return _DateDiff('s', "1970/01/01 00:00:00", $utcTime)
EndFunc   ;==>_GetUnixTime

;$blTrim: Year in short format and no seconds.
Func _GetDate_fromUnixTime($iUnixTime, $iReturnLocal = True)
    Local $aRet = 0, $aDate = 0
    Local $aMonthNumberAbbrev[13] = ["", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
    Local $timeAdj = 0
    If Not $iReturnLocal Then
        Local $aSysTimeInfo = _Date_Time_GetTimeZoneInformation()
        Local $timeAdj = $aSysTimeInfo[1] * 60
        If $aSysTimeInfo[0] = 2 Then $timeAdj += $aSysTimeInfo[7] * 60
    EndIf

    $aRet = DllCall("msvcrt.dll", "str:cdecl", "ctime", "int*", $iUnixTime + $timeAdj )

    If @error Or Not $aRet[0] Then Return ""

    $aDate = StringSplit(StringTrimRight($aRet[0], 1), " ", 2)

    Return $aDate[4] & "/" & StringFormat("%.2d", _ArraySearch($aMonthNumberAbbrev, $aDate[1])) & "/" & $aDate[2] & " " & $aDate[3]
EndFunc   ;==>_GetUnixDate

Enjoy!

Realm

Update: >Added some functionality and a suggestion from FireFox

Edited by Realm
Included thegebi's revision and added commenting to MessageBoxes to better explain the functions. Also Removed 2nd parameter of _GetDate_fromUnixTime() which was unnecessary.
  • Like 1

My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry.  

Share this post


Link to post
Share on other sites
Realm

Thanks to FireFox, via a PM, for not only suggesting to include a function to revert a Unix Time to a Date stamp, but providing a very nice example as well.

Func _GetUnixDate($iUnixTime, $blTrim = True)
    Local $aRet = 0, $aDate = 0
    Local $aMonthNumberAbbrev[13] = ["", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
    Local $aSysTimeInfo = _Date_Time_GetTimeZoneInformation()

    $aRet = DllCall("msvcrt.dll", "str:cdecl", "ctime", "int*", $iUnixTime - $aSysTimeInfo[1] * 60)
    If @error Or Not $aRet[0] Then Return ""

    $aDate = StringSplit(StringTrimRight($aRet[0], 1), " ", $STR_NOCOUNT)

    If $blTrim Then Return StringFormat("%.2d", _ArraySearch($aMonthNumberAbbrev, $aDate[1])) & "/" & $aDate[2] & "/" & StringRight($aDate[4], 2) & " " & StringTrimRight($aDate[3], 3)

    Return $aDate[2] & "/" & StringFormat("%.2d", _ArraySearch($aMonthNumberAbbrev, $aDate[1])) & "/" & $aDate[4] & " " & $aDate[3]
EndFunc   ;==>_GetUnixDate

I have also added a parameter to _GetUnixTime($sDate) allowing any date since 1970 to be passed and converted to Unix Time. Leaving the parameter empty or passing zero(0) will convert the current date to Unix Time.

I have also made a few minor changes to the example provide from FireFox.

1. Change the format of the returned string to match the accepted string in function _GetUnixTime()

2. Added another parameter, $iReturnGMT to return the converted time to date in GMT(UTC) or in your current time with consideration to daylight savings time if 'False' is passed in the third parameter.

Thanks again FireFox!

Realm


My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry.  

Share this post


Link to post
Share on other sites
thegebi

I think I found a bug in the code. The result of _GetDate_fromUnixTime is wrong if the 3rd parameter, $iReturnGMT, is set to False, because the timezone offset is subtracted from the local time, but it should be added instead.

Furthermore, there are several calls to the function _GetUnixDate in the code, but this function was renamed to _GetDate_fromUnixTime, so the calls don't work anymore.

So I created an updated version of Realm's code and added a few comments to it:

#include <Date.au3>
#include <Array.au3>
#include <Constants.au3>

Local $iUnixTime1 = _GetUnixTime()

MsgBox($MB_SYSTEMMODAL, 'Seconds Since Jan, 1st, 1970 00:00:00 GMT', $iUnixTime1)

Local $sUnixDate1 = _GetDate_fromUnixTime($iUnixTime1)
MsgBox($MB_SYSTEMMODAL, "", $sUnixDate1)

$sUnixDate1 = _GetDate_fromUnixTime($iUnixTime1, False)
MsgBox($MB_SYSTEMMODAL, "", $sUnixDate1)

$sUnixDate1 = _GetDate_fromUnixTime($iUnixTime1, False, False)
MsgBox($MB_SYSTEMMODAL, "", $sUnixDate1)

Local $iUnixTime2 = _GetUnixTime('2013/01/01 00:00:00')
MsgBox($MB_SYSTEMMODAL, "", $iUnixTime2)

; Get timestamp for input datetime (or current datetime).
Func _GetUnixTime($sDate = 0);Date Format: 2013/01/01 00:00:00 ~ Year/Mo/Da Hr:Mi:Se

    Local $aSysTimeInfo = _Date_Time_GetTimeZoneInformation()
    Local $utcTime = ""

    If Not $sDate Then $sDate = _NowCalc()

    If Int(StringLeft($sDate, 4)) < 1970 Then Return ""

    If $aSysTimeInfo[0] = 2 Then ; if daylight saving time is active
        $utcTime = _DateAdd('n', $aSysTimeInfo[1] + $aSysTimeInfo[7], $sDate) ; account for time zone and daylight saving time
    Else
        $utcTime = _DateAdd('n', $aSysTimeInfo[1], $sDate) ; account for time zone
    EndIf

    Return _DateDiff('s', "1970/01/01 00:00:00", $utcTime)
EndFunc   ;==>_GetUnixTime

;$blTrim: Year in short format and no seconds.
Func _GetDate_fromUnixTime($iUnixTime, $blTrim = True, $iReturnLocal = True)
    Local $aRet = 0, $aDate = 0
    Local $aMonthNumberAbbrev[13] = ["", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
    Local $timeAdj = 0
    If Not $iReturnLocal Then
        Local $aSysTimeInfo = _Date_Time_GetTimeZoneInformation()
        Local $timeAdj = $aSysTimeInfo[1] * 60
        If $aSysTimeInfo[0] = 2 Then $timeAdj += $aSysTimeInfo[7] * 60
    EndIf

    $aRet = DllCall("msvcrt.dll", "str:cdecl", "ctime", "int*", $iUnixTime + $timeAdj )

    If @error Or Not $aRet[0] Then Return ""

    $aDate = StringSplit(StringTrimRight($aRet[0], 1), " ", 2)

    If $blTrim Then Return  StringRight($aDate[4], 2) & "/" & StringFormat("%.2d",_ArraySearch($aMonthNumberAbbrev, $aDate[1])) & "/" & $aDate[2] & " " & StringTrimRight($aDate[3], 3)

    Return $aDate[4] & "/" & StringFormat("%.2d", _ArraySearch($aMonthNumberAbbrev, $aDate[1])) & "/" & $aDate[2] & " " & $aDate[3]
EndFunc ;==>_GetDate_fromUnixTime

 

Thanks to Realm for creating this useful snippet!

Edited by thegebi
fixed last comment in the code

Share this post


Link to post
Share on other sites
Realm

Thanks @thegebi,

I have updated my OP including your revision and added some commenting to the MsgBox's to better demonstrate how the functions work.

Edited by Realm
Misspelling.

My Contributions: Unix Timestamp: Calculate Unix time, or seconds since Epoch, accounting for your local timezone and daylight savings time. RegEdit Jumper: A Small & Simple interface based on Yashied's Reg Jumper Function, for searching Hives in your registry.  

Share this post


Link to post
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

  • Similar Content

    • Jemboy
      By Jemboy
      Hi,
      Ones(some times twice) a month I get an e-mail with zip file, which has price updates from a supplier.
      I have to upload the file to an FTP to get it processed.
      When uploading the file, it will get "timestamped" with the time and date at which time the file was uploaded.
      Normally this is fine, because I mostly upload the file the same day.
      Sometimes it may take 1 or 2 days before I can upload the file.
      For historical purpose, I would like to have the file timestamped with the original date.
      I have tried using:  _FTP_Command ( $hFTPSession, "MFCT YYYYMMDDHHMMSS path") 
      however this command does not work or change the timestamp as I expected.
      Does anyone now a way how I can change the timestamp of a FTP-file?
    • Rskm
      By Rskm
      Hi, for a script of mine, I wish to keep a time frame on till how the program will work.. basically, I want the program to read the date and if the read date < 'a particular date', the program continues, else it exit. I don't want to read the time from the computer as the program can be fooled by resetting the pc time. is there a way to read time from internet - so that the user cannot fool the program by date reset? thanks
    • ergo
      By ergo
      CronToTime.au3 UDF converts a Unix Cron expression into a DateTime string.
      On Unix systems a Cron expression made of five fields separated by blanks/tabs followed by a shell command to execute.

      e.g. 15-30 2,3,4,5 29 1-12 0-6 reboot
      +------------- minute (0 - 59)
      ¦ +------------- hour (0 - 23)
      ¦ ¦ +------------- day of month (1 - 31)
      ¦ ¦ ¦ +------------- month (1 - 12)
      ¦ ¦ ¦ ¦ +------------- day of week (0 - 6) (Sunday to Saturday, 7 is also Sunday)
      ¦ ¦ ¦ ¦ ¦ +------------- some shell command
      ¦ ¦ ¦ ¦ ¦ ¦
      ¦ ¦ ¦ ¦ ¦ ¦
      * * * * * reboot
      (In this UDF the 6th field has no meaning and no influence on the result).
      For more information see https://en.wikipedia.org/wiki/Cron

      Finds the next event of the specified Cron expression, starting from current UTC/GMT.
      _pce_findNextTimeUTC($sCronExp [, $bForwardSearch = True [, $minutesOffset = 0]]])
      Finds the next event of the specified Cron expression. If $sDateTime is not set, local time will be used.
      _pce_findNextTime($sCronExp [, $bForwardSearch = True [, $sDateTime = "" [, $minutesOffset = 0]]])
      Convert names in usable Cron expression values. e.g. "15-30 2,3,4,5 29 July-December Mo-Su" to "15-30 2,3,4,5 29 7-12 1-7"
      _pce_convertNames( $sCronExp )
      See the include for details.
       
      CronToTime_Test.au3
      CronToTime.au3
    • Shedunn
      By Shedunn
      I've been looking through some different functions(not sure if that's the right word) to get the Date and time and I can't find one that doesn't include "/ " or ":" in what is returned.

      I'm trying to create a file name with this format "QuickConfig_MMDDYYYY_HHMM"  (doesn't matter if 24 or 12hr format)
      #include <Date.au3> Func _SaveConfig MouseClick('primary',155, 46, 1, 0) ;clicks save as WinWait('Save bluePRINT Configuration As...','', 4) MouseClick('primary',632, 47, 1, 0) ;clicks the file path bar $Documents = @MyDocumentsDir $FilePath = $Documents & '\BP3 Configs' Send($FilePath) Send('{ENTER}') MouseClick('primary',166, 580, 1, 0) ;clicks file name bar $FileName = 'QuickConfig_' & _NowDate & '_' & _NowTime Send($FileName) Send('{ENTER}') EndFunc  
       
      I'm wondering if there is a function somewhere to do this?
×