diff --git a/datetime/datetime.go b/datetime/datetime.go index 20c5e1c..9c5bcc7 100644 --- a/datetime/datetime.go +++ b/datetime/datetime.go @@ -3,7 +3,7 @@ // Package datetime implements some functions to format date and time. // Note: -// 1. `format` param in FormatTimeToStr function should be as flow: +// 1. `format` param in FormatTimeToStr function should be as flow (case no sensitive): // "yyyy-mm-dd hh:mm:ss" // "yyyy-mm-dd hh:mm" // "yyyy-mm-dd hh" @@ -18,14 +18,20 @@ // "yyyy/mm" // "mm/dd" // "dd/mm/yy hh:mm:ss" +// "yyyymmdd" +// "mmddyy" // "yyyy" +// "yy" // "mm" // "hh:mm:ss" +// "hh:mm" // "mm:ss" + package datetime import ( "fmt" + "strings" "time" ) @@ -47,9 +53,13 @@ func init() { "yyyy/mm": "2006/01", "mm/dd": "01/02", "dd/mm/yy hh:mm:ss": "02/01/06 15:04:05", + "yyyymmdd": "20060102", + "mmddyy": "010206", "yyyy": "2006", + "yy": "06", "mm": "01", "hh:mm:ss": "15:04:05", + "hh:mm": "15:04", "mm:ss": "04:05", } } @@ -102,18 +112,39 @@ func GetNightTimestamp() int64 { } // FormatTimeToStr convert time to string -func FormatTimeToStr(t time.Time, format string) string { - return t.Format(timeFormat[format]) +func FormatTimeToStr(t time.Time, format string, timezone ...string) string { + tf, ok := timeFormat[strings.ToLower(format)] + if !ok { + return "" + } + + if timezone != nil && timezone[0] != "" { + loc, err := time.LoadLocation(timezone[0]) + if err != nil { + return "" + } + return t.In(loc).Format(tf) + } + return t.Format(tf) } // FormatStrToTime convert string to time -func FormatStrToTime(str, format string) (time.Time, error) { - v, ok := timeFormat[format] +func FormatStrToTime(str, format string, timezone ...string) (time.Time, error) { + tf, ok := timeFormat[strings.ToLower(format)] if !ok { - return time.Time{}, fmt.Errorf("format %s not found", format) + return time.Time{}, fmt.Errorf("format %s not support", format) } - return time.Parse(v, str) + if timezone != nil && timezone[0] != "" { + loc, err := time.LoadLocation(timezone[0]) + if err != nil { + return time.Time{}, err + } + + return time.ParseInLocation(tf, str, loc) + } + + return time.Parse(tf, str) } // BeginOfMinute return beginning minute time of day @@ -210,3 +241,83 @@ func DayOfYear(t time.Time) int { func IsWeekend(t time.Time) bool { return time.Saturday == t.Weekday() || time.Sunday == t.Weekday() } + +// NowDateOrTime return current datetime with specific format and timezone. +func NowDateOrTime(format string, timezone ...string) string { + tf, ok := timeFormat[strings.ToLower(format)] + if !ok { + return "" + } + + if timezone != nil && timezone[0] != "" { + loc, err := time.LoadLocation(timezone[0]) + if err != nil { + return "" + } + + return time.Now().In(loc).Format(tf) + } + + return time.Now().Format(tf) +} + +// Timestamp return current second timestamp. +func Timestamp(timezone ...string) int64 { + t := time.Now() + + if timezone != nil && timezone[0] != "" { + loc, err := time.LoadLocation(timezone[0]) + if err != nil { + return 0 + } + + t = t.In(loc) + } + + return t.Unix() +} + +// TimestampMilli return current mill second timestamp. +func TimestampMilli(timezone ...string) int64 { + t := time.Now() + + if timezone != nil && timezone[0] != "" { + loc, err := time.LoadLocation(timezone[0]) + if err != nil { + return 0 + } + t = t.In(loc) + } + + return int64(time.Nanosecond) * t.UnixNano() / int64(time.Millisecond) +} + +// TimestampMicro return current micro second timestamp. +func TimestampMicro(timezone ...string) int64 { + t := time.Now() + + if timezone != nil && timezone[0] != "" { + loc, err := time.LoadLocation(timezone[0]) + if err != nil { + return 0 + } + t = t.In(loc) + } + + return int64(time.Nanosecond) * t.UnixNano() / int64(time.Microsecond) +} + +// TimestampNano return current nano second timestamp. +func TimestampNano(timezone ...string) int64 { + t := time.Now() + + if timezone != nil && timezone[0] != "" { + loc, err := time.LoadLocation(timezone[0]) + if err != nil { + return 0 + } + t = t.In(loc) + } + + return t.UnixNano() +} diff --git a/datetime/datetime_test.go b/datetime/datetime_test.go index ccfc486..781cb43 100644 --- a/datetime/datetime_test.go +++ b/datetime/datetime_test.go @@ -298,3 +298,38 @@ func TestIsWeekend(t *testing.T) { result2 := IsWeekend(date2) assert.Equal(false, result2) } + +func TestNowDateOrTime(t *testing.T) { + t.Parallel() + + formats := []string{ + "yyyy-mm-dd hh:mm:ss", + "yyyy-mm-dd", + "dd-mm-yy hh:mm:ss", + "yyyy/mm/dd hh:mm:ss", + "hh:mm:ss", + "yyyy/mm", + "yyyy-mm-dd hh", + } + + for i := 0; i < len(formats); i++ { + result := NowDateOrTime(formats[i], "UTC") + t.Log(result) + } +} + +func TestTimestamp(t *testing.T) { + t.Parallel() + + ts1 := Timestamp() + t.Log(ts1) + + ts2 := TimestampMilli() + t.Log(ts2) + + ts3 := TimestampMicro() + t.Log(ts3) + + ts4 := TimestampNano() + t.Log(ts4) +} diff --git a/docs/datetime.md b/docs/datetime.md index 0a74886..1ad0f47 100644 --- a/docs/datetime.md +++ b/docs/datetime.md @@ -57,6 +57,11 @@ import ( - [BetweenSeconds](#BetweenSeconds) - [DayOfYear](#DayOfYear) - [IsWeekend](#IsWeekend) +- [NowDateOrTime](#NowDateOrTime) +- [Timestamp](#Timestamp) +- [TimestampMilli](#TimestampMilli) +- [TimestampMicro](#TimestampMicro) +- [TimestampNano](#TimestampNano)
@@ -64,7 +69,7 @@ import ( ## Note: -1. 'format' string param in func FormatTimeToStr and FormatStrToTime function should be one of flows: +1. In below functions, the `format` string param should be one of flows value (case no sensitive): - yyyy-mm-dd hh:mm:ss - yyyy-mm-dd hh:mm @@ -75,14 +80,18 @@ import ( - dd-mm-yy hh:mm:ss - yyyy/mm/dd hh:mm:ss - yyyy/mm/dd hh:mm -- yyyy-mm-dd hh +- yyyy/mm/dd hh - yyyy/mm/dd - yyyy/mm - mm/dd - dd/mm/yy hh:mm:ss +- yyyymmdd +- mmddyy - yyyy +- yy - mm - hh:mm:ss +- hh:mm - mm:ss ### AddDay @@ -1036,7 +1045,6 @@ func main() { } ``` - ### DayOfYearReturns which day of the year the parameter date `t` is.
@@ -1157,3 +1165,198 @@ func main() { // false } ``` + +### IsWeekend + +Checks if passed time is weekend or not.
+ +Signature: + +```go +func IsWeekend(t time.Time) bool +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/datetime" +) + +func main() { + date1 := time.Date(2023, 06, 03, 0, 0, 0, 0, time.Local) + date2 := time.Date(2023, 06, 04, 0, 0, 0, 0, time.Local) + date3 := time.Date(2023, 06, 02, 0, 0, 0, 0, time.Local) + + result1 := datetime.IsWeekend(date1) + result2 := datetime.IsWeekend(date2) + result3 := datetime.IsWeekend(date3) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // true + // true + // false +} +``` + +### NowDateOrTime + +Return current datetime with specific format and timezone.
+ +Signature: + +```go +func NowDateOrTime(format string, timezone ...string) string +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/datetime" +) + +func main() { + result1 := datetime.NowDateOrTime("yyyy-mm-dd hh:mm:ss") + + result2 := datetime.NowDateOrTime("yyyy-mm-dd hh:mm:ss", "EST") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // 2023-07-26 15:01:30 + // 2023-07-26 02:01:30 +} +``` + +### Timestamp + +Return current second timestamp.
+ +Signature: + +```go +func Timestamp(timezone ...string) int64 +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/datetime" +) + +func main() { + ts := datetime.Timestamp() + + fmt.Println(ts) + + // Output: + // 1690363051 +} +``` + + +### TimestampMilli + +Return current mill second timestamp.
+ +Signature: + +```go +func TimestampMilli(timezone ...string) int64 +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/datetime" +) + +func main() { + ts := datetime.TimestampMilli() + + fmt.Println(ts) + + // Output: + // 1690363051331 +} +``` + +### TimestampMicro + +Return current micro second timestamp.
+ +Signature: + +```go +func TimestampMicro(timezone ...string) int64 +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/datetime" +) + +func main() { + ts := datetime.TimestampMicro() + + fmt.Println(ts) + + // Output: + // 1690363051331784 +} +``` + +### TimestampNano + +Return current nano second timestamp.
+ +Signature: + +```go +func TimestampNano(timezone ...string) int64 +``` + +Example: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/datetime" +) + +func main() { + ts := datetime.TimestampNano() + + fmt.Println(ts) + + // Output: + // 1690363051331788000 +} +``` diff --git a/docs/datetime_zh-CN.md b/docs/datetime_zh-CN.md index 1a1a619..cd9f35c 100644 --- a/docs/datetime_zh-CN.md +++ b/docs/datetime_zh-CN.md @@ -57,7 +57,11 @@ import ( - [BetweenSeconds](#BetweenSeconds) - [DayOfYear](#DayOfYear) - [IsWeekend](#IsWeekend) - +- [NowDateOrTime](#NowDateOrTime) +- [Timestamp](#Timestamp) +- [TimestampMilli](#TimestampMilli) +- [TimestampMicro](#TimestampMicro) +- [TimestampNano](#TimestampNano) @@ -65,7 +69,7 @@ import ( ## 注: -1. 方法 FormatTimeToStr 和 FormatStrToTime 中的 format 参数值需要传以下类型之一: +1. 函数中`format`参数值需要传以下值之一 (忽略大小写): - yyyy-mm-dd hh:mm:ss - yyyy-mm-dd hh:mm @@ -76,14 +80,18 @@ import ( - dd-mm-yy hh:mm:ss - yyyy/mm/dd hh:mm:ss - yyyy/mm/dd hh:mm -- yyyy-mm-dd hh +- yyyy/mm/dd hh - yyyy/mm/dd - yyyy/mm - mm/dd - dd/mm/yy hh:mm:ss +- yyyymmdd +- mmddyy - yyyy +- yy - mm - hh:mm:ss +- hh:mm - mm:ss ### AddDay @@ -1077,3 +1085,198 @@ func main() { // 0 } ``` + +### IsWeekend + +判断日期是否是周末。
+ +函数签名: + +```go +func IsWeekend(t time.Time) bool +``` + +示例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/datetime" +) + +func main() { + date1 := time.Date(2023, 06, 03, 0, 0, 0, 0, time.Local) + date2 := time.Date(2023, 06, 04, 0, 0, 0, 0, time.Local) + date3 := time.Date(2023, 06, 02, 0, 0, 0, 0, time.Local) + + result1 := datetime.IsWeekend(date1) + result2 := datetime.IsWeekend(date2) + result3 := datetime.IsWeekend(date3) + + fmt.Println(result1) + fmt.Println(result2) + fmt.Println(result3) + + // Output: + // true + // true + // false +} +``` + +### NowDateOrTime + +根据指定的格式和时区返回当前时间字符串。
+ +函数签名: + +```go +func NowDateOrTime(format string, timezone ...string) string +``` + +实例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/datetime" +) + +func main() { + result1 := datetime.NowDateOrTime("yyyy-mm-dd hh:mm:ss") + + result2 := datetime.NowDateOrTime("yyyy-mm-dd hh:mm:ss", "EST") + + fmt.Println(result1) + fmt.Println(result2) + + // Output: + // 2023-07-26 15:01:30 + // 2023-07-26 02:01:30 +} +``` + +### Timestamp + +返回当前秒级时间戳。
+ +函数签名: + +```go +func Timestamp(timezone ...string) int64 +``` + +实例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/datetime" +) + +func main() { + ts := datetime.Timestamp() + + fmt.Println(ts) + + // Output: + // 1690363051 +} +``` + + +### TimestampMilli + +返回当前毫秒级时间戳。
+ +函数签名: + +```go +func TimestampMilli(timezone ...string) int64 +``` + +实例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/datetime" +) + +func main() { + ts := datetime.TimestampMilli() + + fmt.Println(ts) + + // Output: + // 1690363051331 +} +``` + +### TimestampMicro + +返回当前微秒级时间戳。
+ +函数签名: + +```go +func TimestampMicro(timezone ...string) int64 +``` + +实例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/datetime" +) + +func main() { + ts := datetime.TimestampMicro() + + fmt.Println(ts) + + // Output: + // 1690363051331784 +} +``` + +### TimestampNano + +返回当前纳秒级时间戳。
+ +函数签名: + +```go +func TimestampNano(timezone ...string) int64 +``` + +实例: + +```go +package main + +import ( + "fmt" + "github.com/duke-git/lancet/datetime" +) + +func main() { + ts := datetime.TimestampNano() + + fmt.Println(ts) + + // Output: + // 1690363051331788000 +} +``` \ No newline at end of file