alias datediff {
; calculate difference between date 1 and date 2, in years,months,days,hours,minutes,seconds
; date 1 must be before date 2
; usage $datediff(dd/mm/yyyy HH:nn:ss, dd/mm/yyyy HH:nn:ss)
; meaning $datediff($date $time, $date $time)
; example: [color:green]//echo -a $datediff(3/5/2005 20:00:10, 5/10/2000 12:15:20)[/color]
; ** IMPORTANT: $date uses dd/mm/yyyy, if you want to input mm/dd/yyyy style,
; ** add a third parameter (not 0, $null or $false)
; example: [color:green]//echo -a $datediff(1/5/2005 11:15:22pm, 1/8/2005 12:15:22P.M., us)[/color]
; hh:nn:ss am/pm is supported (only now I realize how strange it actually is)
; my reference: http://www.worldtimezone.com/wtz-names/wtz-am-pm.html
if ($regex($2,/^0*(\d+)\D0*(\d+)\D(\d+)\D0*(\d+)\D0*(\d+)\D0*(\d+)(?:\D?([ap]\.?m\.?|noon|midday|midnight))?$/i)) {
var %d1 = $regml($iif($3,2,1)), %m1 = $regml($iif($3,1,2))
var %h1 = $regml(4), %n1 = $regml(5), %s1 = $regml(6), %y1 = $regml(3)
if ((%h1 == 12) && (($regml(7) == am) || ($v1 == midnight))) var %h1 = 0
elseif ((%h1 isnum 1-11) && ($regml(7) == pm)) inc %h1 12
}
else return Date 1 in incorrect format
if ($regex($1,/^0*(\d+)\D0*(\d+)\D(\d+)\D0*(\d+)\D0*(\d+)\D0*(\d+)(?:\D?([ap]\.?m\.?|noon|midday|midnight))?$/i)) {
var %d2 = $regml($iif($3,2,1)), %m2 = $regml($iif($3,1,2))
var %h2 = $regml(4), %n2 = $regml(5), %s2 = $regml(6), %y2 = $regml(3)
if ((%h2 == 12) && (($regml(7) == am) || ($v1 == midnight))) var %h2 = 0
elseif ((%h2 isnum 1-11) && ($regml(7) == pm)) inc %h2 12
}
else return Date 2 in incorrect format
if ((%m1 !isnum 1-12) || (%d1 !isnum 1-31) || (%h1 !isnum 0-23) || (%m1 !isnum 0-59) $&
|| (%s1 !isnum 0-59)) return Date 1 invalid
if ((%m2 !isnum 1-12) || (%d2 !isnum 1-31) || (%h2 !isnum 0-23) || (%m2 !isnum 0-59) $&
|| (%s2 !isnum 0-59)) return Date 2 invalid
var %s = %s1 - %s2
if (%s < 0) var %s = %s + 60, %n1 = %n1 - 1
var %n = %n1 - %n2
if (%n < 0) var %n = %n + 60, %h1 = %h1 - 1
var %h = %h1 - %h2
if (%h < 0) var %h = %h + 24, %d1 = %d1 - 1
var %d = %d1 - %d2
if (%d < 0) var %d = %d + $daysinmonth(%m1, %y), %m1 = %m1 - 1
var %m = %m1 - %m2
if (%m < 0) var %m = %m + 12, %y = %y - 1
var %y = %y1 - %y2
if (%y < 0) return reverse the dates please...
return Difference: %y years %m months %d days %h hours %n minutes %s seconds
}
alias -l daysinmonth {
; usage: $daysinmonth(number of month, year)
; returns the number of days that month has, with full support for leap years
if ($1 isin 1.3.5.7.8.10.12) return 31
elseif ($1 isin 4.6.9.11) return 30
elseif ($calc($2 % 400) == 0) return 29
elseif ($calc($2 % 100) == 0) return 28
elseif ($calc($2 % 4) == 0) return 29
return 28
}