combining date variables is unreliable

Get help with using AutoHotkey and its commands and hotkeys
User avatar
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

combining date variables is unreliable

26 Aug 2017, 13:47

Combining (concatenating) date variables is unreliable e.g.:

Code: Select all

;unreliable:
vDate := A_Now A_MSec

;unreliable:
vDate := A_YYYY "-"  A_MM "-"  A_DD
vTime := A_Hour "-"  A_Min "-"  A_Sec
vDate := A_YYYY "-"  A_MM "-"  A_DD " " A_Hour "-"  A_Min "-"  A_Sec

;reliable:
FormatTime, vDate,, yyyy-MM-dd
FormatTime, vTime,, HH-mm-ss
FormatTime, vDate,, yyyy-MM-dd HH-mm-ss

;reliable:
MsgBox, % vDate := RegExReplace(A_Now, "(....)(..)(..)......", "$1-$2-$3")
MsgBox, % vDate := RegExReplace(A_Now, "........(..)(..)(..)", "$1-$2-$3")
MsgBox, % vDate := RegExReplace(A_Now, "(....)(..)(..)(..)(..)(..)", "$1-$2-$3 $4-$5-$6")

;reliable:
MsgBox, % vDate := RegExReplace(A_Now, "(....)(..)(..).{6}", "$1-$2-$3")
MsgBox, % vDate := RegExReplace(A_Now, ".{8}(..)(..)(..)", "$1-$2-$3")
MsgBox, % vDate := RegExReplace(A_Now, "(....)(..)(..)(..)(..)(..)", "$1-$2-$3 $4-$5-$6")
[see: 'vDate := A_Now A_MSec' is unreliable]
[I provide a DllCall method there to achieve 'A_Now A_MSec' reliably]
jeeswg's documentation extension tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=33596

I am trying to create a native AutoHotkey solution, I believe this code is correct. I share in case anyone has any other ideas, or notices a problem below. Even simple while loops can be a little tricky. Thanks for reading.

Code: Select all

q:: how many datestamps can be retrieved per second
vOutput := ""
VarSetCapacity(vOutput, 1000000*2)
vTickCount1 := A_TickCount
Loop, 100000
	vOutput .= A_MSec "`r`n"
vTickCount2 := A_TickCount
Clipboard := (vTickCount2 - vTickCount1) "`r`n`r`n" vOutput
MsgBox, % "done"
return

w:: ;attempt at a native 'A_Now A_MSec' solution
vDate2 := A_Now A_MSec
Loop
{
	vDate := A_Now A_MSec
	if (vDate = vDate2)
		break
	vDate2 := vDate
}
MsgBox, % vDate
return

e:: ;attempt at a shorter 'A_Now A_MSec' solution
vDate2 := A_Now A_MSec
while !((vDate := A_Now A_MSec) = vDate2)
	vDate2 := vDate
MsgBox, % vDate
return
Links:
Variables and Expressions
https://autohotkey.com/docs/Variables.htm#BuiltIn
FormatTime
https://autohotkey.com/docs/commands/FormatTime.htm
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
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: combining date variables is unreliable

26 Sep 2018, 18:36

Two additions:

Code: Select all

MsgBox, % vDate := RegExReplace(A_Now, "(?<=..)..(?=.)", "$0|") ;e.g. 20060504030201 -> 2006|05|04|03|02|01
MsgBox, % vDate := RegExReplace(A_Now, "(?<=..)..(?=.)", "$0 ") ;e.g. 20060504030201 -> 2006 05 04 03 02 01
Also added here:
jeeswg's RegEx tutorial (RegExMatch, RegExReplace) - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=28031
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
jeeswg
Posts: 6904
Joined: 19 Dec 2016, 01:58
Location: UK

Re: combining date variables is unreliable

03 Oct 2018, 14:01

Re. unreliable dates.

A_ variables - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 35#p190635
A_NowMSec/A_NowMSecUTC: or a function to get both A_Now and A_MSec separately but in sync. It is never reliable to combine the use of separate variables to form a datestamp. E.g. New Year's Eve: you get the month, then the year, but the year changed as you did this.
Based on:
Historical Backup Scripts - AutoHotkey Community
https://autohotkey.com/boards/viewtopic ... 68#p160168
On New Year's Eve/Day 2016/2017:
'31 Dec 2017' [should be: '31 Dec 2016' or '01 Jan 2017']
You get an incorrect date because when you retrieved the day/month it was 2016, but when you retrieved the year it was 2017.

Code: Select all

vDate := % A_DD " " A_MMM " " A_YYYY ;unreliable (retrieves the date 3 times)
MsgBox, % vDate
FormatTime, vDate,, dd MMM yyyy ;reliable
MsgBox, % vDate
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: Albireo, Bing [Bot], Odlanir, pn4265 and 248 guests