I have some code here in TCL that tries to measure the time between two dates.
The two first work, but as you see the last one not working; it counts totally wrong, as there can only be 12 month in a year, but this has more than that.
Anyway, I think that the leap year is the problem, but I'm not sure. Can you help?
proc isTimeAgo {t1} {
set t2 "1387524660"
#set t2 [clock seconds]
set cnt [expr {(($t2 - $t1) / 31536000)}]
set cur [clock add $t1 $cnt years]
set res {}
foreach unit {years months weeks days hours minutes seconds} {
while {$cur <= $t2} {
set cur [clock add $cur 1 $unit]
incr cnt
}
set cur [clock add $cur -1 $unit]
incr cnt -1
if {$cnt} {
lappend res $cnt $unit
}
set cnt 0
}
return $res
}
puts "1: [isTimeAgo "1355988659"]"
puts "2: [isTimeAgo "1355988660"]"
puts "3: [isTimeAgo "1355988661"]"
proc days_per_month year {
set leap [expr {($year%4)==0 && (!($year%400) || ($year%100))}]
set days [list 31 [expr {$leap ? 29 : 28}] 31 30 31 30 31 31 30 31 30 31]
return $days
}
The result of this is:
1: 1 years 1 seconds <- Correct
2: 1 years <- Correct
3: 11 months 4 weeks 1 days 23 hours 59 minutes 59 seconds <- Wrong