Skip to content

Instantly share code, notes, and snippets.

@dimm0k
Last active December 20, 2015 17:39
Show Gist options
  • Save dimm0k/6169766 to your computer and use it in GitHub Desktop.
Save dimm0k/6169766 to your computer and use it in GitHub Desktop.
convert time to secs with help from DateParse
;take in number of minutes and outputs seconds.
;with help via DateParse also takes in time with formats of 13:00, 1:00 pm, 1:00pm, 1:00 p, 1:00p, 13:00, 100p, 100 p, 1p, 1 p, 1pm, 1 pm
ConvertToSecs(time)
{
if time is integer
diff := time * 60
else if (InStr(time, "a")!=0 || InStr(time, "p")!=0 || InStr(time, "A")!=0 ||
InStr(time, "P")!=0 || InStr(time, ":")!=0)
{
StringRight, fixampm, time, 1
StringLower, fixampm, fixampm
if (fixampm = "a" || fixampm = "p")
time := time . "m"
if (InStr(time, ":")=0)
{
StringRight, ispm, time, 2
StringReplace, strip, time, %A_SPACE%,, All
StringLeft, strip, strip, % StrLen(strip)-2
if (StrLen(strip) > 2)
{
StringRight, min, strip, 2
StringTrimRight, hour, strip, 2
}
else
{
hour := strip
min := 0
}
if (ispm = "pm")
hour := hour + 12
}
else
{
FormatTime, hour, % DateParse(time), HH
FormatTime, min, % DateParse(time), mm
}
current = %A_Now%
FormatTime, chour, % current, HH
FormatTime, cmin, % current, mm
seconds := hour * 3600 + min * 60
cseconds := chour * 3600 + cmin * 60
diff := seconds - cseconds
}
Return diff
}
;formivore - http://www.autohotkey.com/board/topic/18760-date-parser-convert-any-date-format-to-yyyymmddhh24miss/?p=561591
DateParse(str, americanOrder=0) {
static monthNames := "(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\w*"
, dayAndMonth := "(?:(\d{1,2}|" . monthNames . ")[\s\.\-\/,]+)?(\d{1,2}|" . monthNames . ")"
If RegExMatch(str, "i)^\s*(?:(\d{4})([\s\-:\/])(\d{1,2})\2(\d{1,2}))?"
. "(?:\s*[T\s](\d{1,2})([\s\-:\/])(\d{1,2})(?:\6(\d{1,2})\s*(?:(Z)|(\+|\-)?"
. "(\d{1,2})\6(\d{1,2})(?:\6(\d{1,2}))?)?)?)?\s*$", i) ;ISO 8601 timestamps
year := i1, month := i3, day := i4, t1 := i5, t2 := i7, t3 := i8
Else If !RegExMatch(str, "^\W*(\d{1,2}+)(\d{2})\W*$", t){
RegExMatch(str, "i)(\d{1,2})" ;hours
. "\s*:\s*(\d{1,2})" ;minutes
. "(?:\s*:\s*(\d{1,2}))?" ;seconds
. "(?:\s*([ap]m))?", t) ;am/pm
StringReplace, str, str, %t%
If Regexmatch(str, "i)(\d{4})[\s\.\-\/,]+" . dayAndMonth, d) ;2004/22/03
year := d1, month := d3, day := d2
Else If Regexmatch(str, "i)" . dayAndMonth . "[\s\.\-\/,]+(\d{2,4})", d) ;22/03/2004 or 22/03/04
year := d3, month := d2, day := d1
If (RegExMatch(day, monthNames) or americanOrder and !RegExMatch(month, monthNames)) ;try to infer day/month order
tmp := month, month := day, day := tmp
}
f = %A_FormatFloat%
SetFormat, Float, 02.0
d := (StrLen(year) == 2 ? "20" . year : (year ? year : A_YYYY))
. ((month := month + 0 ? month : InStr(monthNames, SubStr(month, 1, 3)) // 4 ) > 0 ? month + 0.0 : A_MM)
. ((day += 0.0) ? day : A_DD)
. t1 + (t1 == 12 ? t4 = "am" ? -12.0 : 0.0 : t4 = "pm" ? 12.0 : 0.0)
. t2 + 0.0 . t3 + 0.0
SetFormat, Float, %f%
return, d
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment