Ok, I need to deliver some timestamp info to another utility in UNIX timestamp format (which is number of seconds since Jan 1, 1970). Has anyone already written the code in AutoHotkey to do this conversion from AHKs YYYYMMDDHHMISS format? I could probably do it, but it seems like it'll be quite tedious, so if anyone's already done this, then why not share the love, right?
If no one else has done it then I guess I'll bite the bullet and figure it out. I'll post my code here in a couple of days if I get it sorted. I imagine the only tricky bit will be allowing for leap years.
P.S. Chris, this might be a good built-in function for the AHK 'Transform' command in the future.
Cheers guys,
C.
Update: Ok, here's what I got. And it didn't take too long at all. Seems to work so far. If anyone can see any mistakes or ways to improve its efficiency, please point them out.
It only goes one way (AHK to Unix - maybe someone else can do the reverse some time?), and will work for dates between 1970 and 2400.
Code:
;###############Convert YYYYMMDDHHMISS into Unix timestamp#############
time_orig=%A_NOW%
StringLeft, now_year, time_orig, 4
StringMid, now_month, time_orig, 5, 2
StringMid, now_day, time_orig, 7, 2
StringMid, now_hour, time_orig, 9, 2
StringMid, now_min, time_orig, 11, 2
StringRight, now_sec, time_orig, 2
;Get year seconds
year_sec := 31536000*(now_year - 1970)
;Determine how many leap days
leap_days := (now_year - 1972)/4 + 1
Transform, leap_days, Floor, %leap_days%
;Determine if date is in a leap year, and if the leap day has been yet
this_leap := now_year/4
Transform, this_leap_round, Floor, %this_leap%
If (this_leap = this_leap_round)
{
If now_month <= 2
leap_days-- ;subtracts 1 because this year's leap day hasn't been yet
}
leap_sec := leap_days*86400
;Determine fully completed months
If now_month = 01
month_sec = 0
If now_month = 02
month_sec = 2678400
If now_month = 03
month_sec = 5097600
If now_month = 04
month_sec = 7776000
If now_month = 05
month_sec = 10368000
If now_month = 06
month_sec = 13046400
If now_month = 07
month_sec = 15638400
If now_month = 08
month_sec = 18316800
If now_month = 09
month_sec = 20995200
If now_month = 10
month_sec = 23587200
If now_month = 11
month_sec = 26265600
If now_month = 12
month_sec = 28857600
;Determine fully completed days
day_sec := (now_day - 1)*86400
;Determine fully completed hours
hour_sec := now_hour*3600 ;don't subtract 1 because it starts at 0
;Determine fully completed minutes
min_sec := now_min*60
;Calculate total seconds
date_sec := year_sec + month_sec + day_sec + leap_sec + hour_sec + min_sec + now_sec
MsgBox, %date_sec%