Jump to content

_UnixTimeParse()


-Ultima-
 Share

Recommended Posts

I'm not sure if some Unix time converter has been submitted, but I've never seen one, so here's one I cooked up a while back. I don't know if there's a more efficient way to do this; this was just a straightforward conversion :)

This function doesn't zero-pad any numbers, so if you want to format it into a readable string, you should take note that minutes and seconds less than 10 will look a bit funny if you don't pad them yourself.

_UnixTimeParse.au3

#Include <Date.au3>

Local $asDOTW[7] = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
Local $iUnixTime = 1111111111
Local $anDate = _UnixTimeParse($iUnixTime)
MsgBox(0,"_UnixTimeParse(" & $iUnixTime & ")", StringFormat("%s %.2d/%.2d/%.2d At %.2d:%.2d:%.2d UTC", $asDOTW[$anDate[0]], $anDate[1], $anDate[2], $anDate[3], $anDate[4], $anDate[5], $anDate[6]))oÝ÷ Ù°Úµæ®ßöÚºÚ"µÍÒ[ÛYKSÛÙBÏOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOBÂÈØÜ[ÛÛÛÈHÚ][[^[YH[È[^HÛÛZ[[ÈHÈUÈ]Z][[[YKÈ[Y]ÊN ÌÍÛ[^[YHHH[^[YHÈÛÛÈ]Z[Y[
ÊNÛKÈ][YJÊN]È[^HÙHÜBÈÑVHÑÑQRÈ
ËXÙY
KQPTSÓVKÕTRSUKÑPÓÓKÈ]]ÜÊN[[XBÈÝJÊNHÛHÙH[XÈ[H]Y^HHËYYÈHH^HÙHÙYZÈØ[H[H[YH]ÙY[[[ÛY[ÂÈ
[Û^JH[

Ý[^JBÂÏOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOB[ÈÕ[^[YTÙJ  ÌÍÛ[^[YJBØØ[   ÌÍÚVYX^ÈHÍBØØ[   ÌÍØZS[Û^ÖÌLHHÌÌKÌKÌÌKÌÌKÌKÌÌKÌÌWBØØ[    ÌÍØ[]VÍ×HHÌËNMÌHÈ^HÙÙYZÈ
ËXÙY
KYX[Û^KÝZ[]KÙXÛÛÈÙXÛÛ  ÌÍØ[]VÍHH[Ù
    ÌÍÛ[^[YK

B   ÌÍÛ[^[YHH[
    ÌÍÛ[^[YKÍ
BÈZ[]B ÌÍØ[]VÍWHH[Ù
    ÌÍÛ[^[YK

B   ÌÍÛ[^[YHH[
    ÌÍÛ[^[YKÍ
BÈÝ   ÌÍØ[]VÍHH[Ù
    ÌÍÛ[^[YK
B   ÌÍÛ[^[YHH[
    ÌÍÛ[^[YKÌ
BÈ^HÙÙYZÂ   ÌÍØ[]VÌHH[Ù
    ÌÍØ[]VÌJÉÌÍÛ[^[YK
ÊBÈYXÚ[H ÌÍÛ[^[YH ÝÏH   ÌÍÚVYX^ÂYÝ[Ù
    ÌÍØ[]VÌWK
H[  ÌÍÚVYX^ÈHÍ[ÙB ÌÍÚVYX^ÈHÍB[Y  ÌÍÛ[^[YHOH   ÌÍÚVYX^    ÌÍØ[]VÌWH
ÏHBÑ[È[ÛÚ[H    ÌÍÛ[^[YH ÝÏH   ÌÍØZS[Û^ÖÉÌÍØ[]VÌWB   ÌÍÛ[^[YHOH   ÌÍØZS[Û^ÖÉÌÍØ[]VÌWBY  ÌÍØ[]VÌHHH[[Ù
    ÌÍØ[]VÌWK
HH[ ÌÍÛ[^[YHOHB  ÌÍØ[]VÌH
ÏHBÑ[È^BY    ÌÍÛ[^[YH È[ ÌÍØ[]VÌ×HH ÌÍØZS[Û^ÖÉÌÍØ[]VÌKLWJÌB[ÙB  ÌÍØ[]VÌH
ÏHB    ÌÍØ[]VÌ×HH ÌÍÛ[^[YJÌB[Y]   ÌÍØ[]B[[ÈÏOIÝ×Õ[^[YTÙ

Edit (2007-10-08): Added ability to calculate day of the week; 0 = Monday, ..., 6 = Sunday

Edited by -Ultima-

[ WinINet.au3 | Array.au3 (Optimized) | _UnixTimeParse() ]

Link to comment
Share on other sites

This could come in handy. ;)

However, since the Date UDF already uses the filename, "Date.au3," it might be a wise idea to rename your file, unless it is meant to be an addition to the existing Date.au3 include file, in which case users should not simply replace the file with yours, but copy and paste the function into the original Date.au3 UDF. But perhaps I'm insulting someone's intelligence.

Otherwise, nice job. :)

Link to comment
Share on other sites

if i'am not totally wrong there is already a date function to handle this problem :)

#include <Date.au3>

; Calculated the number of seconds since EPOCH (1970/01/01 00:00:00) 
$iDateCalc = _DateDiff( 's',"1970/01/01 00:00:00",_NowCalc())
MsgBox( 4096, "", "Number of seconds since EPOCH: " & $iDateCalc )

EDIT: Sry GMK didn't notice that you already mentioned it.

Edited by JRSmile
$a=StringSplit("547275737420796F757220546563686E6F6C75737421","")
For $b=1 To UBound($a)+(-1*-1*-1)step(2^4/8);&$b+=1*2/40*µ&Asc(4)
Assign("c",Eval("c")&Chr(Dec($a[$b]&$a[$b+1])));''Chr("a")&"HI"
Next ;time_U&r34d,ths,U-may=get$the&c.l.u.e;b3st-regards,JRSmile;
MsgBox(0x000000,"",Eval("c"));PiEs:d0nt+*b3.s4d.4ft3r.1st-try:-)
Link to comment
Share on other sites

That's not the same thing; what you're doing is getting the Unix timestamp, and what I'm doing is converting the Unix timestamp back into regular time.

GMK wasn't really referring to the same thing either; he was mentioning the fact that I named the file as Date.au3, which can conflict with the existing Date.au3. He sorta had the right idea; I named my file accordingly only because I rewrote this function into a UDF by following the standard UDF writing guidelines. I guess it was wishful thinking on my part that the function might get added to the standard UDFs, but I'm not too worried if it doesn't get added either.

Edited by -Ultima-

[ WinINet.au3 | Array.au3 (Optimized) | _UnixTimeParse() ]

Link to comment
Share on other sites

  • 1 year later...

That's not the same thing; what you're doing is getting the Unix timestamp, and what I'm doing is converting the Unix timestamp back into regular time.

GMK wasn't really referring to the same thing either; he was mentioning the fact that I named the file as Date.au3, which can conflict with the existing Date.au3. He sorta had the right idea; I named my file accordingly only because I rewrote this function into a UDF by following the standard UDF writing guidelines. I guess it was wishful thinking on my part that the function might get added to the standard UDFs, but I'm not too worried if it doesn't get added either.

I've needed, but never wrapped my head around the conversion so this will really be useful.

I don't see any reason for this not to be included with Time.au3. (There's so many useful functions on the forum that go unnoticed. :P )

Don't bother, It's inside your monitor!------GUISetOnEvent should behave more like HotKeySet()
Link to comment
Share on other sites

I've needed, but never wrapped my head around the conversion so this will really be useful.

I don't see any reason for this not to be included with Time.au3. (There's so many useful functions on the forum that go unnoticed. :P )

I agree. This function should, in fact, be built-in one (JS have it).

...only not this one because it's wrong.

Leap year definition in that script is wrong.

And there is "days in current month" issue, looking at it.

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

ähhm, don't want to hurt you to much but here we already have such a function :P

#include <date.au3>

$unixtimestamp = "1111111111"
$iDateCalc = _DateAdd('s', $unixtimestamp, "1970/01/01 00:00:00")
MsgBox( 4096, "", "Timestamp converted to Date: " & $iDateCalc )
$a=StringSplit("547275737420796F757220546563686E6F6C75737421","")
For $b=1 To UBound($a)+(-1*-1*-1)step(2^4/8);&$b+=1*2/40*µ&Asc(4)
Assign("c",Eval("c")&Chr(Dec($a[$b]&$a[$b+1])));''Chr("a")&"HI"
Next ;time_U&r34d,ths,U-may=get$the&c.l.u.e;b3st-regards,JRSmile;
MsgBox(0x000000,"",Eval("c"));PiEs:d0nt+*b3.s4d.4ft3r.1st-try:-)
Link to comment
Share on other sites

ähhm, don't want to hurt you to much but here we already have such a function :P

#include <date.au3>

$unixtimestamp = "1111111111"
$iDateCalc = _DateAdd('s', $unixtimestamp, "1970/01/01 00:00:00")
MsgBox( 4096, "", "Timestamp converted to Date: " & $iDateCalc )
Where is "here"?

I was commenting on built-innenes (probably not a word) and feelings of mrRevoked are impossible to predict at this moment.

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

in the good old date.au3 already in everybodys include folder ...

it is called _DateAdd and lets you add seconds ( which are used to state the unix timestamp) to a date, lets take the 1970/01/01 00:00:00 which everybody knows is the starting date of the unix timestamp, and BINGO you got a unixtimestamp to date converter beware of leap years vacations and sundays :P

and why build everything in if we can use udfs :(

Edited by JRSmile
$a=StringSplit("547275737420796F757220546563686E6F6C75737421","")
For $b=1 To UBound($a)+(-1*-1*-1)step(2^4/8);&$b+=1*2/40*µ&Asc(4)
Assign("c",Eval("c")&Chr(Dec($a[$b]&$a[$b+1])));''Chr("a")&"HI"
Next ;time_U&r34d,ths,U-may=get$the&c.l.u.e;b3st-regards,JRSmile;
MsgBox(0x000000,"",Eval("c"));PiEs:d0nt+*b3.s4d.4ft3r.1st-try:-)
Link to comment
Share on other sites

I agree. This function should, in fact, be built-in one (JS have it).

...only not this one because it's wrong.

Leap year definition in that script is wrong.

And there is "days in current month" issue, looking at it.

I hope for a fix soon.

Don't bother, It's inside your monitor!------GUISetOnEvent should behave more like HotKeySet()
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...