How to convert current time to unix timestamp?

Get help with using AutoHotkey and its commands and hotkeys
User avatar
vvhitevvizard
Posts: 299
Joined: 25 Nov 2018, 10:15
Location: Russia

Re: How to convert current time to unix timestamp?

20 Jan 2019, 07:15

afe wrote:
20 Jan 2019, 07:11
Thank you. But I think leap seconds should be considered.
Is there a function available?
Leap seconds were introduced in 1972. So there is none between 1601 (Microsoft) and 1970 (POSIX). So there is no need to count them in for this conversion
afe wrote:
20 Jan 2019, 07:11
"With the SYSTEMTIME structure set by GetSystemTime, it's easy to create a a struct tm (see asctime for a reference of the structure) and convert it to a "UNIX time stamp" using the mktime function."
But I don't know how to create a struct tm.
See my post. A system function "GetSystemTimeAsFileTime" returns FILETIME in form of a single number. All u have to do is some simple math to convert it from Microsoft time (base at 1601) to POSIX (1970)
IMEime
Posts: 750
Joined: 20 Sep 2014, 06:15

Re: How to convert current time to unix timestamp?

20 Jan 2019, 09:09

afe wrote:
20 Jan 2019, 07:11
IMEime wrote:
20 Jan 2019, 05:04
something like this ?

Code: Select all

myNow := A_YYYY A_MM A_DD A_Hour A_Min A_Sec
myNow -= 19700101000000, S
MsgBox % myNow
Thank you. But ....
Did you check it out with your own fingers ? Not just listen to others "so Called Perfect Gurus of Here and There"

I just confirmed using the C# codes with my own 10 fingers.
The above code makes correct result.
I just hope, I am not wrong and C# is not wrong.
Last edited by IMEime on 20 Jan 2019, 09:20, edited 1 time in total.
IMEime
Posts: 750
Joined: 20 Sep 2014, 06:15

Re: How to convert current time to unix timestamp?

20 Jan 2019, 09:19

Code: Select all

long myTesting = ((DateTimeOffset)DateTime.UtcNow).ToUnixTimeSeconds();
Just for your reference. .NET Framework 4.6 or Above.
User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: How to convert current time to unix timestamp?

20 Jan 2019, 13:22

I put a few functions in this thread.
convert common date formats (Excel / FileTime / Unix) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=44727
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
vvhitevvizard
Posts: 299
Joined: 25 Nov 2018, 10:15
Location: Russia

Re: How to convert current time to unix timestamp?

20 Jan 2019, 20:44

some benchmark (AHK v2). 1st variant is over 2 times as faster. No wonder, AHK2's DateDiff/DateAdd and time-math operations of AHK1 use the same GetSystemTimeAsFileTime DllCall along with other DllCalls internally. :D

Code: Select all

n:=1000*1000
t:=A_TickCount
loop(n)
	DllCall("GetSystemTimeAsFileTime",'uint64p',t1), s1:=t1//10000000-11644473600
a1:=A_TickCount-t

t:=A_TickCount
loop(n)
	s2:=DateDiff(A_NowUTC, 1970, "S")
a2:=A_TickCount-t

msgbox(clipboard:=a1 "|" a2 "`n`n" s1 "|" s2)
IMEime
Posts: 750
Joined: 20 Sep 2014, 06:15

Re: How to convert current time to unix timestamp?

20 Jan 2019, 21:05

N******e please do not be rude when replying in the forum - final warning

If you satisfied with this kind of results
Use "0s and 1s" language only, not the languages of high level
0s and 1s will be The Fastest of all, I promise.
And
It will take several millions of years to finish your first testing code of Unix time. I promise.
just me
Posts: 6551
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: How to convert current time to unix timestamp?

20 Jan 2019, 22:23

Unix time (also known as POSIX time[citation needed] or UNIX Epoch time[1]) is a system for describing a point in time. It is the number of seconds that have elapsed since 00:00:00 Thursday, 1 January 1970,[2] Coordinated Universal Time (UTC), minus leap seconds. Every day is treated as if it contains exactly 86400 seconds,[2] so leap seconds are to be subtracted since the epoch.[3] It is used widely in Unix-like and many other operating systems and file formats. However, Unix time is not a true representation of UTC, as a leap second in UTC shares the same Unix time as the second which came before it. Unix time may be checked on most Unix systems by typing date +%s on the command line.
Source: Unix time
User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: How to convert current time to unix timestamp?

20 Jan 2019, 22:30

- Hello vvhitevvizard, thanks for the speed tests.
- You might find these links interesting:
How to optimize the speed of a script as much as possible. - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=6413
jeeswg's benchmark tests - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=74&t=37876
- It might be good to collect some of your speed test discoveries in one place. E.g. by collecting urls in one thread post. Cheers.

- Thanks just me, great link, although it's not very clear what they're saying.
- So, in certain situations you have to use a hardcoded list of historical leap seconds?
Leap second - Wikipedia
https://en.wikipedia.org/wiki/Leap_second#Insertion_of_leap_seconds
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
vvhitevvizard
Posts: 299
Joined: 25 Nov 2018, 10:15
Location: Russia

Re: How to convert current time to unix timestamp?

20 Jan 2019, 23:50

IMEime wrote:
20 Jan 2019, 21:05
N******e
If you satisfied with this kind of results
Use "0s and 1s" language only, not the languages of high level
Please, cut this aggressive demeanor and quit insulting me. Look at AHK sources and find the way date math turns into system calls (LOTS OF THEM), learn how dll functions r cached and run from RAM not disk.
jeeswg wrote:
20 Jan 2019, 22:30
- So, in certain situations you have to use a hardcoded list of historical leap seconds?
Leap seconds for time base starting from 1972. When we transcode from Microsoft timebase (1601) to Posix (1970), we dont need to count in leap seconds. As I get it.
just me wrote:
20 Jan 2019, 22:23
Source: Unix time
Well, we transcode from Microsoft timestamp (with leap seconds already calculated). We just adjust timestamp base, thats it.
just me
Posts: 6551
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: How to convert current time to unix timestamp?

21 Jan 2019, 06:50

Code: Select all

UnixTimeStamp() {
   Static UnixStart := 116444736000000000
   DllCall("GetSystemTimeAsFileTime", "Int64P", FileTime)
   Return ((FileTime - UnixStart) // 10000000) - 27 ; currently (2019-01-21) 27 leap seconds have been added
}
User avatar
vvhitevvizard
Posts: 299
Joined: 25 Nov 2018, 10:15
Location: Russia

Re: How to convert current time to unix timestamp?

21 Jan 2019, 08:18

just me wrote:
21 Jan 2019, 06:50

Code: Select all

UnixTimeStamp() {
   Static UnixStart := 116444736000000000
   DllCall("GetSystemTimeAsFileTime", "Int64P", FileTime)
   Return ((FileTime - UnixStart) // 10000000) - 27 ; currently (2019-01-21) 27 leap seconds have been added
}
Interesting. But Unix timestamps I can compare it with dont seem to subtract these seconds. See for urself, e.g.:
https://www.epochconverter.com/

I had a script working with some server API requiring to put Unix timestamps inside every packet header and, if the offset is more than 5 seconds, rejecting the packet. The server accepts simple Microsoft->Unix timestamp conversion (FileTime-UnixStart in ur example) otherwise if the calculation is wrong the offset would be way off.
afe
Posts: 577
Joined: 06 Dec 2018, 04:36

Re: How to convert current time to unix timestamp?

21 Jan 2019, 11:37

vvhitevvizard wrote:
21 Jan 2019, 08:18
Interesting. But Unix timestamps I can compare it with dont seem to subtract these seconds. See for urself, e.g.:
https://www.epochconverter.com/
Yes. Why is there no leap second?

When Human date to Timestamp and UNIX Timestamp are converted to each other, it seems that leap seconds are not considered.
User avatar
TLM
Posts: 1418
Joined: 01 Oct 2013, 07:52
GitHub: TLMcode
Contact:

Re: How to convert current time to unix timestamp?

25 Jan 2019, 13:47

IMEime wrote:
20 Jan 2019, 06:53
What ? Are you blind ? This is good enough.
:facepalm: Please see pm
for direct help, tweet at me: https://twitter.com/CW_DTech
awel20
Posts: 182
Joined: 19 Mar 2018, 14:09

Re: How to convert current time to unix timestamp?

25 Jan 2019, 14:04

afe wrote:
21 Jan 2019, 11:37
Yes. Why is there no leap second?

When Human date to Timestamp and UNIX Timestamp are converted to each other, it seems that leap seconds are not considered.
I might misunderstand but I think this is the relevant part:
Every day is treated as if it contains exactly 86400 seconds [...] a leap second in UTC shares the same Unix time as the second which came before it.
ewerybody
Posts: 5
Joined: 04 Nov 2016, 09:16
GitHub: ewerybody

Re: How to convert current time to unix timestamp?

08 Mar 2019, 12:14

Hello! Thanks for investigating in this already! Super Valuable nfo! :thumbup:
On another note:

Code: Select all

myNow -= 19700101000000, S

How does this work?!? I mean it DOES work! :D but What is ", S"?

Where is documentation for that? Shouldn't that be somewhere around here:
... docs/Variables.htm#Intro
or here:
... docs/commands/SetExpression.htm

Thanks again!
cheers!
User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: How to convert current time to unix timestamp?

08 Mar 2019, 13:40

EnvSub - Syntax & Usage | AutoHotkey
https://autohotkey.com/docs/commands/EnvSub.htm
Cheers.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask For Help”

Who is online

Users browsing this forum: Odlanir, pn4265 and 246 guests