Convert .NET DateTime ticks to AHK YYYYMMDDHH24MISS timestamp

Get help with using AutoHotkey and its commands and hotkeys
zotune
Posts: 83
Joined: 17 Nov 2014, 17:57

Convert .NET DateTime ticks to AHK YYYYMMDDHH24MISS timestamp

24 Jul 2015, 03:20

Here's an example: 130814384725063210
If you type [Datetime]130814384725063210 into powershell it returns the date. Anyone know how to convert this information in AHK?

Appreciate any help :)
Mikael
User avatar
jNizM
Posts: 2663
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: Convert .NET DateTime ticks to AHK YYYYMMDDHH24MISS timestamp

24 Jul 2015, 04:19

Code: Select all

[Datetime]130814384725063210   =>   Wednesday, 15. Juli 0415 12:54:32
[Datetime]10000000             =>   Monday, 1. Januar 0001 00:00:01
1 Second = 10000000
1 Second / 10000000 = 100 Nanoseconds
Not with WinAPI (DllCall)
I think the problem here is the FILETIME structure
Contains a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 (UTC).
A. GetDateFormat works with minimum Date 160101010000000 (Monday, 01. Januar 1601)

Code: Select all

MsgBox % GetDateFormat(160101010000000)    ; ==> Monday, 01. Januar 1601
 
GetDateFormat(Date, Format := "dddd',' dd. MMMM yyyy")
{
    VarSetCapacity(SYSTEMTIME, 16)
    , NumPut(SubStr(Date, 1, 4), SYSTEMTIME, 0, "UShort")
    , NumPut(SubStr(Date, 5, 2), SYSTEMTIME, 2, "UShort")
    , NumPut(SubStr(Date, 7, 2), SYSTEMTIME, 6, "UShort")
    size := DllCall("GetDateFormat", "UInt", 0x0400, "UInt", 0, "Ptr", &SYSTEMTIME, "Ptr", &Format, "Ptr", 0, "Int", 0)
    VarSetCapacity(buf, size * (A_IsUnicode ? 2 : 1), 0)
    if !(DllCall("GetDateFormat", "UInt", 0x0400, "UInt", 0, "Ptr", &SYSTEMTIME, "Ptr", &Format, "Str", buf, "Int", size))
        return "*" A_LastError
    return buf
}
B. commands/FormatTime.htm works with minimum Date 1601010100000000 (Monday, 01. Januar 1601 12:00:00)

Code: Select all

FormatTime, TimeString, 1601010100000000, dddd, dd. MMMM yyyy hh:mm:ss
MsgBox % TimeString    ; ==> Monday, 01. Januar 1601 12:00:00
but... it looks like it is somehow possible with FormatTime
Although only years between 1601 and 9999 are supported, a formatted time can still be produced for earlier years as long as the time portion is valid.

Maybe someone else has an idea to format this
[AHK] 1.1.32.00 x64 Unicode | [WIN] 10 Pro (Version 2004) x64 | [GitHub] Profile
Donations are appreciated if I could help you
zotune
Posts: 83
Joined: 17 Nov 2014, 17:57

Re: Convert .NET DateTime ticks to AHK YYYYMMDDHH24MISS timestamp

24 Jul 2015, 06:31

I'm not even sure where that 0415 comes from, should be this year. Maybe [DateTime] doesn't work properly either.

IE: 130822047195938865, should be 24 June 2015 11:45

Currently I base it on File created time, but I figure if someone copy these files around, then that will break it.
User avatar
jNizM
Posts: 2663
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: Convert .NET DateTime ticks to AHK YYYYMMDDHH24MISS timestamp

24 Jul 2015, 07:16

In Powershell

Code: Select all

[DATETIME]130822047195938865 ==> Friday, 24. July 0415 09:45:19
Where you get the number from?
[AHK] 1.1.32.00 x64 Unicode | [WIN] 10 Pro (Version 2004) x64 | [GitHub] Profile
Donations are appreciated if I could help you
zotune
Posts: 83
Joined: 17 Nov 2014, 17:57

Re: Convert .NET DateTime ticks to AHK YYYYMMDDHH24MISS timestamp

24 Jul 2015, 07:19

Name of the file. Ticks or something created by .NET
User avatar
jNizM
Posts: 2663
Joined: 30 Sep 2013, 01:33
GitHub: jNizM
Contact:

Re: Convert .NET DateTime ticks to AHK YYYYMMDDHH24MISS timestamp

24 Jul 2015, 07:25

Name or Date of a file?

if you want the date of a file you can use skans function

Code: Select all

MsgBox % FileGetTime("C:\Windows\System32\calc.exe")

FileGetTime(File, Mode := "M") ; by SKAN
{
    if (VarSetCapacity($, 342, 0) && (H := DllCall("FindFirstFile", "Str", File, "Ptr", &$)))
    {
        DllCall("FindClose", "Ptr", H), VC := &$ + 4, VA := &$ + 12, VM := &$ + 20, FT := V%Mode%
        LFT := &$ + 318 , DllCall("FileTimeToLocalFileTime", "Ptr", FT, "Ptr", LFT)
        ST := LFT + 8   , DllCall("FileTimeToSystemTime", "Ptr", LFT, "Ptr", ST)
        loop 7
            T .= StrLen(N:= NumGet(ST + 0, (A_Index - 1) * 2, "UShort")) < 2 ? "0" N : N
        return SubStr(T, 1, 6) SubStr(T, 9, 8) SubStr("00" NumGet(ST + 0, 14, "UShort"), -2)
    }
}
[AHK] 1.1.32.00 x64 Unicode | [WIN] 10 Pro (Version 2004) x64 | [GitHub] Profile
Donations are appreciated if I could help you
zotune
Posts: 83
Joined: 17 Nov 2014, 17:57

Re: Convert .NET DateTime ticks to AHK YYYYMMDDHH24MISS timestamp

24 Jul 2015, 07:38

File looks like this SHOW.TEST_130814384725063210.xml
Numbers are supposed to provide a date based on ticks from 1970 or something (.NET)
User avatar
Blackholyman
Posts: 1292
Joined: 29 Sep 2013, 22:57
Facebook: socialjsz
Google: +Jszapp
Location: Denmark
Contact:

Re: Convert .NET DateTime ticks to AHK YYYYMMDDHH24MISS timestamp

24 Jul 2015, 08:02

Sounds like The Unix epoch
(or Unix time or POSIX time or Unix timestamp) is the number of seconds that have elapsed since January 1, 1970 (midnight UTC/GMT), not counting leap seconds (in ISO 8601: 1970-01-01T00:00:00Z). Literally speaking the epoch is Unix time 0 (midnight 1/1/1970), but 'epoch' is often used as a synonym for 'Unix time'. Many Unix systems store epoch dates as a signed 32-bit integer, which might cause problems on January 19, 2038 (known as the Year 2038 problem or Y2038).

although this site does not give the date you ask for http://www.epochconverter.com/
Also check out:
Courses on AutoHotkey

My Autohotkey Blog
:dance:
zotune
Posts: 83
Joined: 17 Nov 2014, 17:57

Re: Convert .NET DateTime ticks to AHK YYYYMMDDHH24MISS timestamp

26 Jul 2015, 11:23

Blackholyman, this site does the conversion somewhat correctly: http://tickstodatetime.com/

Tick (from filename): 130824030856564401
File created time (from windows): 26.07.2015 18:51
TicksToDatetime: 0415-07-26​T16:51:25.657Z

Date is correct, but year should be 2015, not 0415
just me
Posts: 7382
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Convert .NET DateTime ticks to AHK YYYYMMDDHH24MISS timestamp

27 Jul 2015, 01:21

This seem to be FILETIME values: Contains a 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 (UTC).

As jNizM said: 1 second = 10000000 100-nanoseconds. So you have to divide the value by 10000000 to get the seconds, which can be easily added to a 1601 timestamp. The result will be UTC. If you want the local time, you have to process an appropriate conversion.

Code: Select all

FileTicks := 130824030856564401
FileSeconds := FileTicks // 10000000
DateTimeUTC := 1601
DateTimeUTC += FileSeconds, S
LocalTicks := FileTimeToLocalFileTime(FileTicks)
FileSeconds := LocalTicks // 10000000
DateTimeLoc := 1601
DateTimeLoc += FileSeconds, S
MsgBox, UTC:`n%DateTimeUTC%`nLocal:`n%DateTimeLoc%


FileTimeToLocalFileTime(FileTicks) { ; 64-bit value representing the number of 100-nanosecond intervals since January 1, 1601 (UTC)
   VarSetCapacity(FileTime, 8, 0)
   VarSetCapacity(LocalTime, 8, 0)
   NumPut(FileTicks, FileTime, "UInt64")
   DllCall("FileTimeToLocalFileTime", "Ptr", &FileTime, "Ptr", &LocalTime)
   Return NumGet(LocalTime, "UInt64")
}
Just a guess:
Date is correct, but year should be 2015, not 0415
What do you get if you add 04 to 16?
zotune
Posts: 83
Joined: 17 Nov 2014, 17:57

Re: Convert .NET DateTime ticks to AHK YYYYMMDDHH24MISS timestamp

14 Oct 2018, 05:03

@just me: This works great. Sorry for late reply. Thanks so much :)

Return to “Ask For Help”

Who is online

Users browsing this forum: c7aesa7r, mikeyww, PuzzledGreatly and 31 guests