Jump to content

EPOCH time


trancexx
 Share

Recommended Posts

This can be done by dllcalling function or two from msvcrt.dll. Limitation of that way is that it cannot proceed negative EPOCH times and is limited to maximum year of 2038 (3000).

Probably there is some other function inside some other dll since Javascript has this as built-in function and for example VBS can deal with "negative" times.

Anyway, this is done without DllCall(). It is completley based on Date.au3 and functions inside it.

- Why did I extract this out of Date.au3? Because this way these two functions are about 6-7 times faster than using Date.au3.

- Speed compared to some other methods? Incredible! Impressive! omg!

- Why? Date.au3 is very resourceful script. When I look at functions inside of it I'm starting to wonder if people who wrote them are humans at all? I have serious suspicions about Jos van der Zande. I think that he (it!) is some sort of artificial intelligence. I think that that entity has no form but exists in form that is far beyond our... something. I'm serious.

Functions with small example:

ConsoleWrite("EPOCH time 1234567890 is " & _Epoch_decrypt(1234567890) & @CRLF)
ConsoleWrite("Date 4712/12/31 23:59:59 is EPOCH " & _Epoch_encrypt("4712/12/31 23:59:59") & @CRLF)



Func _EPOCH_decrypt($iEpochTime)
    Local $iDayToAdd = Int($iEpochTime / 86400)
    Local $iTimeVal = Mod($iEpochTime, 86400)
    If $iTimeVal < 0 Then
        $iDayToAdd -= 1
        $iTimeVal += 86400
    EndIf
    Local $i_wFactor = Int((573371.75 + $iDayToAdd) / 36524.25)
    Local $i_xFactor = Int($i_wFactor / 4)
    Local $i_bFactor = 2442113 + $iDayToAdd + $i_wFactor - $i_xFactor
    Local $i_cFactor = Int(($i_bFactor - 122.1) / 365.25)
    Local $i_dFactor = Int(365.25 * $i_cFactor)
    Local $i_eFactor = Int(($i_bFactor - $i_dFactor) / 30.6001)
    Local $aDatePart[3]
    $aDatePart[2] = $i_bFactor - $i_dFactor - Int(30.6001 * $i_eFactor)
    $aDatePart[1] = $i_eFactor - 1 - 12 * ($i_eFactor - 2 > 11)
    $aDatePart[0] = $i_cFactor - 4716 + ($aDatePart[1] < 3)
    Local $aTimePart[3]
    $aTimePart[0] = Int($iTimeVal / 3600)
    $iTimeVal = Mod($iTimeVal, 3600)
    $aTimePart[1] = Int($iTimeVal / 60)
    $aTimePart[2] = Mod($iTimeVal, 60)
    Return StringFormat("%.2d/%.2d/%.2d %.2d:%.2d:%.2d", $aDatePart[0], $aDatePart[1], $aDatePart[2], $aTimePart[0], $aTimePart[1], $aTimePart[2])
EndFunc


Func _Epoch_encrypt($date)
    Local $main_split = StringSplit($date, " ")
    If $main_split[0] - 2 Then
        Return SetError(1, 0, "") ; invalid time format
    EndIf
    Local $asDatePart = StringSplit($main_split[1], "/")
    Local $asTimePart = StringSplit($main_split[2], ":")
    If $asDatePart[0] - 3 Or $asTimePart[0] - 3 Then
        Return SetError(1, 0, "") ; invalid time format
    EndIf
    If $asDatePart[2] < 3 Then
        $asDatePart[2] += 12
        $asDatePart[1] -= 1
    EndIf
    Local $i_aFactor = Int($asDatePart[1] / 100)
    Local $i_bFactor = Int($i_aFactor / 4)
    Local $i_cFactor = 2 - $i_aFactor + $i_bFactor
    Local $i_eFactor = Int(1461 * ($asDatePart[1] + 4716) / 4)
    Local $i_fFactor = Int(153 * ($asDatePart[2] + 1) / 5)
    Local $aDaysDiff = $i_cFactor + $asDatePart[3] + $i_eFactor + $i_fFactor - 2442112
    Local $iTimeDiff = $asTimePart[1] * 3600 + $asTimePart[2] * 60 + $asTimePart[3]
    Return $aDaysDiff * 86400 + $iTimeDiff
EndFunc

What is really special about this functions is that they cover dates from -4712/12/31 23:59:59 (EPOCH -210831897601) to 2147483647/12/25 20:00:00 (EPOCH 67767976233000000). First date is nothing special but second one is far beyond other converters can convert. Far, far...

edit: "comared" is not a word lol

Edited by trancexx

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

  • 2 weeks later...

Thank you for sharing this :(

It will help me great at the moment because I just wanted to start writing such a function myself and my Guardian Angel whispered me to search first on forum ;) Lucky me :)

I wouldn't mind seeing the dllcall version too :mellow:

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

  • Developers

. I think that he (it!) is some sort of artificial intelligence. I think that that entity has no form but exists in form that is far beyond our... something. I'm serious.

Excuse me ... you are referring to me as "it"?

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Thank you for sharing this :(

It will help me great at the moment because I just wanted to start writing such a function myself and my Guardian Angel whispered me to search first on forum ;) Lucky me :)

I wouldn't mind seeing the dllcall version too :mellow:

$epoch_time = 1234567890
    $aCall = DllCall("msvcrt.dll", "str:cdecl", "ctime", "int*", $epoch_time) 
    ConsoleWrite("EPOCH time = " & $epoch_time & @CRLF)
    ConsoleWrite("EPOCH time converted to your timezone time = " & $aCall[0] & @CRLF)

Can be used for 0 < $epoch_time < 2147483647

@AI (a.k.a. Jos);

What? You're gonna X-ray me now or something?

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

Excuse me ... you are referring to me as "it"?

Jos

And he/she/it went so far as to suggest that your intelligence was artificial. Ik zou zeggen dat een klap in het gezicht.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

  • Developers

@AI (a.k.a. Jos);

What? You're gonna X-ray me now or something?

That would be a too friendly treatment :mellow:

Ik zou zeggen dat een klap in het gezicht.

Yes... that's it for sure! :( Edited by Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

  • Developers

You would. Wouldn't you?

Don't think you translated that correctly when you make this remark.

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

$epoch_time = 1234567890
    $aCall = DllCall("msvcrt.dll", "str:cdecl", "ctime", "int*", $epoch_time) 
    ConsoleWrite("EPOCH time = " & $epoch_time & @CRLF)
    ConsoleWrite("EPOCH time converted to your timezone time = " & $aCall[0] & @CRLF)

Can be used for 0 < $epoch_time < 2147483647

Sweet :mellow:

Thanks alot.

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

  • 11 months later...
  • 2 months later...

Very nice, trancexx, I've been looking for an alternative to the limited range of msvcrt.dll.

There is something wrong with your decrypt function though.

A timestamp of 1263769500 returns a invalid date of 2009/13/17 23:05:00.

You are right.

But I would lie if I tell you I didn't know that :D

Corrected it a long ago but didn't post here. Corrected can be found inside the script called ResourcesViewerAndCompiler.au3.

Function there is called _EpochDecrypt. It was just a touch.

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

  • 2 weeks later...

May I add a little reminder to those that could be tempted to use trancexx code for applications where leap seconds matter (stock operations, remote auctions, documents timestamping, astronomy and the like) without asking themselves the right questions. Of course, this is a general remark and is in no way specific to what trancexx kindly shares with us.

You need to determine exactly which taste of Unix epoch you have/want, which time scale you are converting to/from and which local legislation you need to take care of, in cases where placing a point in time makes a big difference if it falls that day/month/year or the next.

AFAIK there can be up to 26 (no, not 24) seconds of difference between legal and official time. Also trying to place a time point more than 6 months in future within a few seconds is almost impossible.

It's possible that those leap seconds will be suppressed completely someday, but until this happens (2013?) some of us will have to manipulate time with great care on such demanding occasions.

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

  • 1 year later...
Link to comment
Share on other sites

I read the preamble and the rest. :graduated:

Interesting bit of code trancexx. I designed a front end, what do you think?

Posted Image

It does have something to do with measuring time, honest!

Edit

I just looked at how you are using comparison operators alongside mathematical operators in lines 26 and 27. That's something new to me. Your example is appreciated. Thanks.

Edited by czardas
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...