I am trying to convert a timestamp like this:
2015-06-27T09:34:22+00:00
to a time since format so it would say like 9 months ago 1 day 2 hours 30 minutes 2 seconds.
something like that.
I used time.Parse
and time.Since
to get to this:
6915h7m47.6901559s
But how do I convert from there on? Something like this is what I thought:
for hours > 24 {
days++
hours -= 24
}
But the issue with this is that this won't be accurate for months because months can have 28, 30 and 31 days.
Is there a better way of achieving what I want?
Foreword: I released this utility in github.com/icza/gox
, see timex.Diff()
.
The days in a month depends on the date, just like the days in a year (leap years).
If you use time.Since()
to get the elapsed time since a time.Time
value, or when you calculate the difference between 2 time.Time
values using the Time.Sub()
method, the result is a time.Duration
which loses the time context (as Duration
is just the time difference in nanoseconds). This means you cannot accurately and unambiguously calculate the difference in years, months, etc. from a Duration
value.
The right solution must calculate the difference in the context of the time. You may calculate the difference for each field (year, month, day, hour, minute, second), and then normalize the result to not have any negative values. It is also recommended to swap the Time
values if the relation between them is not the expected.
Normalization means if a value is negative, add the maximum value of that field and decrement the next field by 1. For example if seconds
is negative, add 60
to it and decrement minutes
by 1. One thing to look out for is when normalizing the difference of days (days in month), the number of days in the proper month has to be applied. This can easily be calculated with this little trick:
// Max days in year y1, month M1
t := time.Date(y1, M1, 32, 0, 0, 0, 0, time.UTC)
daysInMonth := 32 - t.Day()
The logic behind this is that the day 32
is bigger than the max day in any month. It will get automatically normalized (extra days rolled to the next month and day decremented properly). And when we subtract day we have after normalization from 32, we get exactly what the last day was in the month.
Time zone handling:
The difference calculation will only give correct result if both of the time values we pass in are in the same time zone (time.Location
). We incorporate a check into our function: if this is not the case, we "convert" one of the time value to be in the same location as the other using the Time.In()
method:
if a.Location() != b.Location() {
b = b.In(a.Location())
}
Here's a solution which calculates difference in year, month, day, hour, min, sec:
func diff(a, b time.Time) (year, month, day, hour, min, sec int) {
if a.Location() != b.Location() {
b = b.In(a.Location())
}
if a.After(b) {
a, b = b, a
}
y1, M1, d1 := a.Date()
y2, M2, d2 := b.Date()
h1, m1, s1 := a.Clock()
h2, m2, s2 := b.Clock()
year = int(y2 - y1)
month = int(M2 - M1)
day = int(d2 - d1)
hour = int(h2 - h1)
min = int(m2 - m1)
sec = int(s2 - s1)
// Normalize negative values
if sec < 0 {
sec += 60
min--
}
if min < 0 {
min += 60
hour--
}
if hour < 0 {
hour += 24
day--
}
if day < 0 {
// days in month:
t := time.Date(y1, M1, 32, 0, 0, 0, 0, time.UTC)
day += 32 - t.Day()
month--
}
if month < 0 {
month += 12
year--
}
return
}
Some tests:
var a, b time.Time
a = time.Date(2015, 5, 1, 0, 0, 0, 0, time.UTC)
b = time.Date(2016, 6, 2, 1, 1, 1, 1, time.UTC)
fmt.Println(diff(a, b)) // Expected: 1 1 1 1 1 1
a = time.Date(2016, 1, 2, 0, 0, 0, 0, time.UTC)
b = time.Date(2016, 2, 1, 0, 0, 0, 0, time.UTC)
fmt.Println(diff(a, b)) // Expected: 0 0 30 0 0 0
a = time.Date(2016, 2, 2, 0, 0, 0, 0, time.UTC)
b = time.Date(2016, 3, 1, 0, 0, 0, 0, time.UTC)
fmt.Println(diff(a, b)) // Expected: 0 0 28 0 0 0
a = time.Date(2015, 2, 11, 0, 0, 0, 0, time.UTC)
b = time.Date(2016, 1, 12, 0, 0, 0, 0, time.UTC)
fmt.Println(diff(a, b)) // Expected: 0 11 1 0 0 0
Output is as expected:
1 1 1 1 1 1
0 0 30 0 0 0
0 0 28 0 0 0
0 11 1 0 0 0
Try it on the Go Playground.
To calculate how old you are:
// Your birthday: let's say it's January 2nd, 1980, 3:30 AM
birthday := time.Date(1980, 1, 2, 3, 30, 0, 0, time.UTC)
year, month, day, hour, min, sec := diff(birthday, time.Now())
fmt.Printf("You are %d years, %d months, %d days, %d hours, %d mins and %d seconds old.",
year, month, day, hour, min, sec)
Example output:
You are 36 years, 3 months, 8 days, 11 hours, 57 mins and 41 seconds old.
The magic date/time at which the Go playground time starts is: 2009-11-10 23:00:00 UTC
This is the time when Go was first announced. Let's calculate how old Go is:
goAnnounced := time.Date(2009, 11, 10, 23, 0, 0, 0, time.UTC)
year, month, day, hour, min, sec := diff(goAnnounced, time.Now())
fmt.Printf("Go was announced "+
"%d years, %d months, %d days, %d hours, %d mins and %d seconds ago.",
year, month, day, hour, min, sec)
Output:
Go was announced 6 years, 4 months, 29 days, 16 hours, 53 mins and 31 seconds ago.