-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
benchmarks added to as and xmath, xtime package added
- Loading branch information
1 parent
271d3a5
commit a15dcd5
Showing
9 changed files
with
241 additions
and
3 deletions.
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,2 @@ | ||
go: | ||
enabled: true |
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
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
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,24 @@ | ||
package xtime_test | ||
|
||
import ( | ||
xtime "../xtime/" | ||
"fmt" | ||
"time" | ||
//"simonwaldherr.de/go/golibs/xtime" | ||
) | ||
|
||
func ExampleFmt() { | ||
t := time.Unix(1234567890, 0) | ||
cet, _ := time.LoadLocation("CET") | ||
t = t.In(cet) | ||
fmt.Println(xtime.Fmt("%Y-%m-%d %H:%M:%S", t)) | ||
// Output: | ||
// 2009-02-14 00:31:30 | ||
} | ||
|
||
func ExampleFmt2() { | ||
t, _ := time.Parse(time.ANSIC, "Mon Jan 02 15:04:05 2006") | ||
fmt.Println(xtime.Fmt("%Y-%m-%d %H:%M:%S", t)) | ||
// Output: | ||
// 2006-01-02 15:04:05 | ||
} |
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 xtime | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
var conv = map[rune]string{ | ||
'a': "Mon", | ||
'A': "Monday", | ||
'b': "Jan", | ||
'B': "January", | ||
'd': "02", | ||
'D': "01-02-2006", | ||
'F': "2006-01-02", | ||
'H': "15", | ||
'I': "03", | ||
'L': ".000", | ||
'm': "01", | ||
'M': "04", | ||
'p': "PM", | ||
'S': "05", | ||
'x': "2006-01-02", | ||
'X': "15:04:05", | ||
'y': "06", | ||
'Y': "2006", | ||
'z': "-0700", | ||
'Z': "MST", | ||
'0': "", | ||
'%': "", | ||
} | ||
|
||
// StrfTime implements a subset of strftime | ||
// http://man7.org/linux/man-pages/man3/strftime.3.html | ||
func StrfTime(format string, t time.Time) string { | ||
ret := make([]byte, 0, len(format)) | ||
for i := 0; i < len(format); i++ { | ||
if format[i] == '%' { | ||
if layout, ok := conv[rune(format[i+1])]; ok { | ||
ret = append(ret, []byte(t.Format(layout))...) | ||
i++ | ||
} else { | ||
ret = append(ret, format[i]) | ||
} | ||
} else { | ||
ret = append(ret, format[i]) | ||
} | ||
} | ||
return string(ret) | ||
} | ||
|
||
// Fmt (xtime.Fmt) is an alias for StrfTime | ||
func Fmt(format string, t time.Time) string { | ||
return StrfTime(format, t) | ||
} | ||
|
||
// FmtNow is like StrfTime, but automatically with the current local time | ||
func FmtNow(format string) string { | ||
t := time.Now() | ||
return StrfTime(format, t) | ||
} |
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,27 @@ | ||
package xtime | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
) | ||
|
||
func Test_Fmt(t *testing.T) { | ||
tt := time.Unix(1234571490, 0) | ||
cet, _ := time.LoadLocation("CET") | ||
tt = tt.In(cet) | ||
if Fmt("%Y-%m-%d %H:%M:%S", tt) != "2009-02-14 01:31:30" { | ||
t.Fatalf("%s != %s", Fmt("%Y-%m-%d %H:%M:%S", tt), "2009-02-14 01:31:30") | ||
} | ||
} | ||
|
||
func Benchmark_Fmt(b *testing.B) { | ||
t := time.Unix(1333333333, 0) | ||
cet, _ := time.LoadLocation("CET") | ||
t = t.In(cet) | ||
for i := 0; i < b.N; i++ { | ||
if Fmt("DATE: %Z %X %x", t) != "DATE: CEST 04:22:13 2012-04-02" { | ||
b.Logf("%s != %s", Fmt("DATE: %Z %X %x", t), "DATE: CEST 04:22:13 2012-04-02") | ||
b.Fail() | ||
} | ||
} | ||
} |