Created
January 11, 2012 23:15
-
-
Save kylelemons/1597389 to your computer and use it in GitHub Desktop.
Humanize a date! (go r60)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import "fmt" | |
| import "time" | |
| func Humanize(t *time.Time) string { | |
| const ( | |
| Minute = 60 | |
| Hour = 60 * Minute | |
| Day = 24 * Hour | |
| Week = 7 * Day | |
| Month = 30 * Day | |
| Year = 12 * Month | |
| ) | |
| then, now := t.Seconds(), time.Seconds() | |
| switch diff := now - then; { | |
| case diff <= 0: return fmt.Sprintf("now") | |
| case diff < 1*Minute: return fmt.Sprintf("%d seconds ago", diff) | |
| case diff < 2*Minute: return fmt.Sprintf("1 minute ago") | |
| case diff < 1*Hour: return fmt.Sprintf("%d minutes ago", diff/Minute) | |
| case diff < 2*Hour: return fmt.Sprintf("1 hour ago") | |
| case diff < 1*Day: return fmt.Sprintf("%d hours ago", diff/Hour) | |
| case diff < 2*Day: return fmt.Sprintf("1 day ago") | |
| case diff < 1*Week: return fmt.Sprintf("%d days ago", diff/Day) | |
| case diff < 2*Week: return fmt.Sprintf("1 week ago") | |
| case diff < 1*Month: return fmt.Sprintf("%d weeks ago", diff/Week) | |
| case diff < 2*Month: return fmt.Sprintf("1 month ago") | |
| case diff < 1*Year: return fmt.Sprintf("%d months ago", diff/Month) | |
| case diff < 18*Month: return fmt.Sprintf("1 year ago") | |
| } | |
| return t.String() | |
| } | |
| // now | |
| // 12 seconds ago | |
| // 30 seconds ago | |
| // 45 seconds ago | |
| // 15 minutes ago | |
| // 2 hours ago | |
| // 21 hours ago | |
| // 1 day ago | |
| // 2 days ago | |
| // 3 days ago | |
| // 1 week ago | |
| // 1 week ago | |
| // 2 weeks ago | |
| // 1 month ago | |
| // 1 year ago | |
| func ExampleHumanize() { | |
| const ( | |
| Minute = 60 | |
| Hour = 60 * Minute | |
| Day = 24 * Hour | |
| Week = 7 * Day | |
| Month = 30 * Day | |
| Year = 12 * Month | |
| ) | |
| now := time.Seconds() | |
| fmt.Println(Humanize(time.SecondsToLocalTime(now))) | |
| fmt.Println(Humanize(time.SecondsToLocalTime(now-12))) | |
| fmt.Println(Humanize(time.SecondsToLocalTime(now-30))) | |
| fmt.Println(Humanize(time.SecondsToLocalTime(now-45))) | |
| fmt.Println(Humanize(time.SecondsToLocalTime(now-15*Minute))) | |
| fmt.Println(Humanize(time.SecondsToLocalTime(now-2*Hour))) | |
| fmt.Println(Humanize(time.SecondsToLocalTime(now-21*Hour))) | |
| fmt.Println(Humanize(time.SecondsToLocalTime(now-26*Hour))) | |
| fmt.Println(Humanize(time.SecondsToLocalTime(now-49*Hour))) | |
| fmt.Println(Humanize(time.SecondsToLocalTime(now-3*Day))) | |
| fmt.Println(Humanize(time.SecondsToLocalTime(now-7*Day))) | |
| fmt.Println(Humanize(time.SecondsToLocalTime(now-12*Day))) | |
| fmt.Println(Humanize(time.SecondsToLocalTime(now-15*Day))) | |
| fmt.Println(Humanize(time.SecondsToLocalTime(now-39*Day))) | |
| fmt.Println(Humanize(time.SecondsToLocalTime(now-365*Day))) | |
| } |
Author
It's there to be used ;-).
…---
Kyle Lemons
Google Platforms - Servers and Storage
Georgia Tech - BS CmpE '10
Sent from my iPhone, so pardon any spelling or punctuation issues!
On Jan 12, 2012, at 7:49 PM, Dustin ***@***.*** wrote:
Hope you don't mind, but I put this in a humanize package since I needed one for file sizes. https://github.com/dustin/go-humanize
---
Reply to this email directly or view it on GitHub:
https://gist.github.com/1597389
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hope you don't mind, but I put this in a humanize package since I needed one for file sizes. https://github.com/dustin/go-humanize