-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
289 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
language: go | ||
go: | ||
- tip | ||
before_install: | ||
- go get github.com/axw/gocov/gocov | ||
- go get github.com/mattn/goveralls | ||
- if ! go get code.google.com/p/go.tools/cmd/cover; then go get golang.org/x/tools/cmd/cover; fi | ||
script: | ||
- $HOME/gopath/bin/goveralls -service=travis-ci |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
package timeutil | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
var longDayNames = []string{ | ||
"Sunday", | ||
"Monday", | ||
"Tuesday", | ||
"Wednesday", | ||
"Thursday", | ||
"Friday", | ||
"Saturday", | ||
} | ||
|
||
var shortDayNames = []string{ | ||
"Sun", | ||
"Mon", | ||
"Tue", | ||
"Wed", | ||
"Thu", | ||
"Fri", | ||
"Sat", | ||
} | ||
|
||
var shortMonthNames = []string{ | ||
"---", | ||
"Jan", | ||
"Feb", | ||
"Mar", | ||
"Apr", | ||
"May", | ||
"Jun", | ||
"Jul", | ||
"Aug", | ||
"Sep", | ||
"Oct", | ||
"Nov", | ||
"Dec", | ||
} | ||
|
||
var longMonthNames = []string{ | ||
"---", | ||
"January", | ||
"February", | ||
"March", | ||
"April", | ||
"May", | ||
"June", | ||
"July", | ||
"August", | ||
"September", | ||
"October", | ||
"November", | ||
"December", | ||
} | ||
|
||
func weekNumber(t *time.Time, char int) int { | ||
weekday := int(t.Weekday()) | ||
|
||
if char == 'W' { | ||
// Monday as the first day of the week | ||
if weekday == 0 { | ||
weekday = 6 | ||
} else { | ||
weekday -= 1 | ||
} | ||
} | ||
|
||
return (t.YearDay() + 6 - weekday) / 7 | ||
} | ||
|
||
func Strftime(t *time.Time, format string) string { | ||
var result string | ||
|
||
for i := 0; i < len(format); i++ { | ||
switch format[i] { | ||
case '%': | ||
if i < len(format)-1 { | ||
switch format[i+1] { | ||
case 'a': | ||
result += shortDayNames[t.Weekday()] | ||
case 'A': | ||
result += longDayNames[t.Weekday()] | ||
case 'w': | ||
result += fmt.Sprintf("%d", t.Weekday()) | ||
case 'd': | ||
result += fmt.Sprintf("%02d", t.Day()) | ||
case 'b': | ||
result += shortMonthNames[t.Month()] | ||
case 'B': | ||
result += longMonthNames[t.Month()] | ||
case 'm': | ||
result += fmt.Sprintf("%02d", t.Month()) | ||
case 'y': | ||
result += fmt.Sprintf("%02d", t.Year()%100) | ||
case 'Y': | ||
result += fmt.Sprintf("%02d", t.Year()) | ||
case 'H': | ||
result += fmt.Sprintf("%02d", t.Hour()) | ||
case 'I': | ||
if t.Hour() == 0 { | ||
result += fmt.Sprintf("%02d", 12) | ||
} else if t.Hour() > 12 { | ||
result += fmt.Sprintf("%02d", t.Hour()-12) | ||
} else { | ||
result += fmt.Sprintf("%02d", t.Hour()) | ||
} | ||
case 'p': | ||
if t.Hour() < 12 { | ||
result += "AM" | ||
} else { | ||
result += "PM" | ||
} | ||
case 'M': | ||
result += fmt.Sprintf("%02d", t.Minute()) | ||
case 'S': | ||
result += fmt.Sprintf("%02d", t.Second()) | ||
case 'f': | ||
result += fmt.Sprintf("%06d", t.Nanosecond()/1000) | ||
case 'z': | ||
result += t.Format("-0700") | ||
case 'Z': | ||
result += t.Location().String() | ||
case 'j': | ||
result += fmt.Sprintf("%03d", t.YearDay()) | ||
case 'U': | ||
result += fmt.Sprintf("%02d", weekNumber(t, 'U')) | ||
case 'W': | ||
result += fmt.Sprintf("%02d", weekNumber(t, 'W')) | ||
case 'c': | ||
result += t.Format("Mon Jan 2 15:04:05 2006") | ||
case 'x': | ||
result += fmt.Sprintf("%02d/%02d/%02d", t.Month(), t.Day(), t.Year()%100) | ||
case 'X': | ||
result += fmt.Sprintf("%02d:%02d:%02d", t.Hour(), t.Minute(), t.Second()) | ||
case '%': | ||
result += "%" | ||
} | ||
i += 1 | ||
} | ||
default: | ||
result += fmt.Sprintf("%c", format[i]) | ||
} | ||
} | ||
|
||
return result | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package timeutil | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestStrftime(t *testing.T) { | ||
date := time.Date(2005, 2, 3, 4, 5, 6, 7000, time.UTC) | ||
AssertEqual(t, Strftime(&date, | ||
"%a %A %w %d %b %B %m %y %Y %H %I %p %M %S %f %z %Z %j %U %W %c %x %X %%"), | ||
"Thu Thursday 4 03 Feb February 02 05 2005 04 04 AM 05 06 000007 +0000 UTC 034 05 05 Thu Feb 3 04:05:06 2005 02/03/05 04:05:06 %") | ||
|
||
date = time.Date(2015, 7, 2, 15, 24, 30, 35, time.UTC) | ||
AssertEqual(t, Strftime(&date, "%U %W"), "26 26") | ||
|
||
date = time.Date(1962, 3, 23, 15, 24, 30, 35, time.UTC) | ||
AssertEqual(t, Strftime(&date, "%U %W"), "11 12") | ||
|
||
date = time.Date(1989, 12, 31, 15, 24, 30, 35000, time.UTC) | ||
AssertEqual(t, Strftime(&date, "%U %W"), "53 52") | ||
|
||
AssertEqual(t, Strftime(&date, | ||
"%a %A %w %d %b %B %m %y %Y %H %I %p %M %S %f %z %Z %j %U %W %c %x %X %%"), | ||
"Sun Sunday 0 31 Dec December 12 89 1989 15 03 PM 24 30 000035 +0000 UTC 365 53 52 Sun Dec 31 15:24:30 1989 12/31/89 15:24:30 %") | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package timeutil | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
func abs(v time.Duration) time.Duration { | ||
if v < 0 { | ||
v *= -1 | ||
} | ||
return v | ||
} | ||
|
||
type Timedelta struct { | ||
days, seconds, microseconds, milliseconds, minutes, hours, weeks time.Duration | ||
} | ||
|
||
func (t *Timedelta) Add(t2 *Timedelta) { | ||
t.days += t2.days | ||
t.seconds += t2.seconds | ||
t.microseconds += t2.microseconds | ||
t.milliseconds += t2.milliseconds | ||
t.minutes += t2.minutes | ||
t.hours += t2.hours | ||
t.weeks += t2.weeks | ||
} | ||
|
||
func (t *Timedelta) Subtract(t2 *Timedelta) { | ||
t.days -= t2.days | ||
t.seconds -= t2.seconds | ||
t.microseconds -= t2.microseconds | ||
t.milliseconds -= t2.milliseconds | ||
t.minutes -= t2.minutes | ||
t.hours -= t2.hours | ||
t.weeks -= t2.weeks | ||
} | ||
|
||
func (t *Timedelta) Abs() { | ||
t.days = abs(t.days) | ||
t.seconds = abs(t.seconds) | ||
t.microseconds = abs(t.microseconds) | ||
t.milliseconds = abs(t.milliseconds) | ||
t.minutes = abs(t.minutes) | ||
t.hours = abs(t.hours) | ||
t.weeks = abs(t.weeks) | ||
} | ||
|
||
func (t *Timedelta) Duration() time.Duration { | ||
return t.days*24*time.Hour + | ||
t.seconds*time.Second + | ||
t.microseconds*time.Microsecond + | ||
t.milliseconds*time.Millisecond + | ||
t.minutes*time.Minute + | ||
t.hours*time.Hour + | ||
t.weeks*7*24*time.Hour | ||
} | ||
|
||
func (t *Timedelta) String() string { | ||
return t.Duration().String() | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package timeutil | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func AssertEqual(t *testing.T, x, y interface{}) { | ||
if !reflect.DeepEqual(x, y) { | ||
t.Error("Expected ", y, ", got ", x) | ||
} | ||
} | ||
|
||
func TestTimedelta(t *testing.T) { | ||
base := time.Date(1980, 1, 6, 0, 0, 0, 0, time.UTC) | ||
result := base.Add((&Timedelta{days: 1, seconds: 66355, weeks: 1722}).Duration()) | ||
AssertEqual(t, result.String(), "2013-01-07 18:25:55 +0000 UTC") | ||
|
||
result = result.Add((&Timedelta{microseconds: 3, milliseconds: 10, minutes: 1}).Duration()) | ||
AssertEqual(t, result.String(), "2013-01-07 18:26:55.010003 +0000 UTC") | ||
|
||
td := Timedelta{days: 10, minutes: 17, seconds: 56} | ||
td2 := Timedelta{days: 15, minutes: 13, seconds: 42} | ||
td.Add(&td2) | ||
|
||
base = time.Date(2015, 2, 3, 0, 0, 0, 0, time.UTC) | ||
result = base.Add(td.Duration()) | ||
AssertEqual(t, result.String(), "2015-02-28 00:31:38 +0000 UTC") | ||
|
||
td.Subtract(&td2) | ||
|
||
result = base.Add(td.Duration()) | ||
AssertEqual(t, result.String(), "2015-02-13 00:17:56 +0000 UTC") | ||
|
||
AssertEqual(t, td.String(), "240h17m56s") | ||
|
||
td = Timedelta{days: -1, seconds: -1, microseconds: -1, milliseconds: -1, minutes: -1, hours: -1, weeks: -1} | ||
td2 = td | ||
td.Abs() | ||
td.Add(&td2) | ||
AssertEqual(t, td.String(), "0") | ||
} |