Post by just me » 31 Mar 2016, 05:18
Hallo fump,
jein, das sollte nur zeigen, dass man nicht einfach immer 1 Stunde addieren kann. Die berühmten 'Unix Time Stamps' beziehen sich immer auf die UTC (Universal Time, Coordinated). Die Differenz zwischen UTC und lokaler Zeit verändert sich im Laufe des Jahres durch die unerquickliche und überflüssige Sommerzeit. Deshalb sind die meisten der von Dir gefundenen Funktionen 'echt mega kompliziert'.
Glücklicherweise tickt die interne Uhr bei Windows Rechnern ebenfalls in UTC. Und es gibt API-Funktionen zum Umrechnen zwischen beiden Welten:
Code: Select all
#NoEnv
UTC1 := unix2Human(1459402085)
LOC := UTC_ToLocalTime(UTC1)
UTC2 := UTC_FromLocalTime(LOC)
MsgBox, UTC1: %UTC1%`nLOC: %LOC%`nUTC2: %UTC2%
ExitApp
; ----------------------------------------------------------------------------------------------------------------------------------
unix2Human(unixTimestamp) {
returnDate = 19700101000000
returnDate += unixTimestamp, s
return returnDate
}
; ----------------------------------------------------------------------------------------------------------------------------------
UTC_ToLocalTime(UTC := "") {
UTC += 0, S
If UTC Is Not Time
Return ""
UTC_TS2SYSTEMTIME(UTC, UTCTime)
VarSetCapacity(LocTime, 16, 0)
DllCall("SystemTimeToTzSpecificLocalTime", "Ptr", 0, "Ptr", &UTCTime, "Ptr", &LocTime)
Return UTC_SYSTEMTIME2TS(LocTime)
}
; ----------------------------------------------------------------------------------------------------------------------------------
UTC_FromLocalTime(Local := "") {
Local += 0, S
If Local Is Not Time
Return ""
UTC_TS2SYSTEMTIME(Local, LocTime)
VarSetCapacity(UTCTime, 16, 0)
DllCall("TzSpecificLocalTimeToSystemTime", "Ptr", 0, "Ptr", &LocTime, "Ptr", &UTCTime)
Return UTC_SYSTEMTIME2TS(UTCTime)
}
; ----------------------------------------------------------------------------------------------------------------------------------
UTC_TS2SYSTEMTIME(TS, ByRef SYSTEMTIME) { ; Timestamp to SYSTEMTIME
VarSetCapacity(SYSTEMTIME, 16, 0)
NumPut(SubStr(TS, 1, 4), SYSTEMTIME, 0, "UShort")
NumPut(SubStr(TS, 5, 2), SYSTEMTIME, 2, "UShort")
NumPut(SubStr(TS, 7, 2), SYSTEMTIME, 6, "UShort")
NumPut(SubStr(TS, 9, 2), SYSTEMTIME, 8, "UShort")
NumPut(SubStr(TS, 11, 2), SYSTEMTIME, 10, "UShort")
NumPut(SubStr(TS, 13, 2), SYSTEMTIME, 12, "UShort")
}
; ----------------------------------------------------------------------------------------------------------------------------------
UTC_SYSTEMTIME2TS(ByRef SYSTEMTIME) { ; Timestamp from SYSTEMTIME
YYYY := NumGet(SYSTEMTIME, 0, "UShort")
MM := NumGet(SYSTEMTIME, 2, "UShort")
DD := NumGet(SYSTEMTIME, 6, "UShort")
HH := NumGet(SYSTEMTIME, 8, "UShort")
MN := NumGet(SYSTEMTIME, 10, "UShort")
SS := NumGet(SYSTEMTIME, 12, "UShort")
Return Format("{:04}{:02}{:02}{:02}{:02}{:02}", YYYY, MM, DD, HH, MN, SS)
}
Hallo fump,
jein, das sollte nur zeigen, dass man nicht einfach immer 1 Stunde addieren kann. Die berühmten 'Unix Time Stamps' beziehen sich immer auf die UTC (Universal Time, Coordinated). Die Differenz zwischen UTC und lokaler Zeit verändert sich im Laufe des Jahres durch die unerquickliche und überflüssige Sommerzeit. Deshalb sind die meisten der von Dir gefundenen Funktionen 'echt mega kompliziert'.
Glücklicherweise tickt die interne Uhr bei Windows Rechnern ebenfalls in UTC. Und es gibt API-Funktionen zum Umrechnen zwischen beiden Welten:
[code]#NoEnv
UTC1 := unix2Human(1459402085)
LOC := UTC_ToLocalTime(UTC1)
UTC2 := UTC_FromLocalTime(LOC)
MsgBox, UTC1: %UTC1%`nLOC: %LOC%`nUTC2: %UTC2%
ExitApp
; ----------------------------------------------------------------------------------------------------------------------------------
unix2Human(unixTimestamp) {
returnDate = 19700101000000
returnDate += unixTimestamp, s
return returnDate
}
; ----------------------------------------------------------------------------------------------------------------------------------
UTC_ToLocalTime(UTC := "") {
UTC += 0, S
If UTC Is Not Time
Return ""
UTC_TS2SYSTEMTIME(UTC, UTCTime)
VarSetCapacity(LocTime, 16, 0)
DllCall("SystemTimeToTzSpecificLocalTime", "Ptr", 0, "Ptr", &UTCTime, "Ptr", &LocTime)
Return UTC_SYSTEMTIME2TS(LocTime)
}
; ----------------------------------------------------------------------------------------------------------------------------------
UTC_FromLocalTime(Local := "") {
Local += 0, S
If Local Is Not Time
Return ""
UTC_TS2SYSTEMTIME(Local, LocTime)
VarSetCapacity(UTCTime, 16, 0)
DllCall("TzSpecificLocalTimeToSystemTime", "Ptr", 0, "Ptr", &LocTime, "Ptr", &UTCTime)
Return UTC_SYSTEMTIME2TS(UTCTime)
}
; ----------------------------------------------------------------------------------------------------------------------------------
UTC_TS2SYSTEMTIME(TS, ByRef SYSTEMTIME) { ; Timestamp to SYSTEMTIME
VarSetCapacity(SYSTEMTIME, 16, 0)
NumPut(SubStr(TS, 1, 4), SYSTEMTIME, 0, "UShort")
NumPut(SubStr(TS, 5, 2), SYSTEMTIME, 2, "UShort")
NumPut(SubStr(TS, 7, 2), SYSTEMTIME, 6, "UShort")
NumPut(SubStr(TS, 9, 2), SYSTEMTIME, 8, "UShort")
NumPut(SubStr(TS, 11, 2), SYSTEMTIME, 10, "UShort")
NumPut(SubStr(TS, 13, 2), SYSTEMTIME, 12, "UShort")
}
; ----------------------------------------------------------------------------------------------------------------------------------
UTC_SYSTEMTIME2TS(ByRef SYSTEMTIME) { ; Timestamp from SYSTEMTIME
YYYY := NumGet(SYSTEMTIME, 0, "UShort")
MM := NumGet(SYSTEMTIME, 2, "UShort")
DD := NumGet(SYSTEMTIME, 6, "UShort")
HH := NumGet(SYSTEMTIME, 8, "UShort")
MN := NumGet(SYSTEMTIME, 10, "UShort")
SS := NumGet(SYSTEMTIME, 12, "UShort")
Return Format("{:04}{:02}{:02}{:02}{:02}{:02}", YYYY, MM, DD, HH, MN, SS)
}[/code]