From c5a90d93a7c7aa44852cf2d55fd738d1a9bac1e9 Mon Sep 17 00:00:00 2001 From: John Guo Date: Sat, 10 May 2025 14:19:55 +0800 Subject: [PATCH] refract(os/gtime): swap function names between `Format` and `Layout` (#4275) --- contrib/drivers/dm/dm_z_unit_issue_test.go | 4 +- .../drivers/mysql/mysql_z_unit_core_test.go | 6 +- .../drivers/mysql/mysql_z_unit_model_test.go | 6 +- .../oracle/oracle_z_unit_model_test.go | 6 +- .../drivers/sqlite/sqlite_z_unit_core_test.go | 6 +- .../sqlitecgo/sqlitecgo_z_unit_core_test.go | 6 +- .../grpcx_unit_z_grpc_server_config_test.go | 2 +- database/gdb/gdb_core_structure.go | 20 +-- encoding/gjson/gjson_z_unit_test.go | 2 +- net/ghttp/ghttp_z_unit_feature_log_test.go | 6 +- os/glog/glog_logger.go | 2 +- os/glog/glog_logger_rotate.go | 2 +- os/gtime/gtime.go | 54 ++++---- os/gtime/gtime_format.go | 116 +++++++++--------- os/gtime/gtime_sql.go | 4 +- os/gtime/gtime_time.go | 36 +++--- os/gtime/gtime_z_bench_test.go | 6 +- os/gtime/gtime_z_example_basic_test.go | 44 +++---- os/gtime/gtime_z_example_time_test.go | 20 +-- os/gtime/gtime_z_unit_format_test.go | 64 +++++----- os/gtime/gtime_z_unit_issue_test.go | 14 +-- os/gtime/gtime_z_unit_test.go | 14 +-- os/gtime/gtime_z_unit_time_test.go | 98 +++++++-------- os/gview/gview_buildin.go | 2 +- .../internal/builtin/builtin_date_format.go | 2 +- .../internal/builtin/builtin_datetime.go | 2 +- 26 files changed, 272 insertions(+), 272 deletions(-) diff --git a/contrib/drivers/dm/dm_z_unit_issue_test.go b/contrib/drivers/dm/dm_z_unit_issue_test.go index 99e2a6b8f..cff0b6277 100644 --- a/contrib/drivers/dm/dm_z_unit_issue_test.go +++ b/contrib/drivers/dm/dm_z_unit_issue_test.go @@ -45,8 +45,8 @@ func Test_Issue2594(t *testing.T) { SubPrefix: "p_", Prefix: "m_", HandleName: "name", - CreateTime: gtime.Now().FormatTo("Y-m-d H:i:s").Time, - UpdateTime: gtime.Now().FormatTo("Y-m-d H:i:s").Time, + CreateTime: gtime.Now().LayoutTo("Y-m-d H:i:s").Time, + UpdateTime: gtime.Now().LayoutTo("Y-m-d H:i:s").Time, Value: []HandleValueMysql{ { Index: 10, diff --git a/contrib/drivers/mysql/mysql_z_unit_core_test.go b/contrib/drivers/mysql/mysql_z_unit_core_test.go index 2d363f5ea..03f654e90 100644 --- a/contrib/drivers/mysql/mysql_z_unit_core_test.go +++ b/contrib/drivers/mysql/mysql_z_unit_core_test.go @@ -1616,7 +1616,7 @@ func Test_Types(t *testing.T) { t.Assert(one["binary"].String(), data["binary"]) t.Assert(one["date"].String(), data["date"]) t.Assert(one["time"].String(), `10:00:01`) - t.Assert(one["timestamp"].GTime().Format(`Y-m-d H:i:s.u`), `2022-02-14 12:00:01.123`) + t.Assert(one["timestamp"].GTime().Layout(`Y-m-d H:i:s.u`), `2022-02-14 12:00:01.123`) t.Assert(one["decimal"].String(), -123.46) t.Assert(one["double"].String(), data["double"]) t.Assert(one["bit"].Int(), data["bit"]) @@ -1640,9 +1640,9 @@ func Test_Types(t *testing.T) { t.Assert(obj.Id, 1) t.Assert(obj.Blob, data["blob"]) t.Assert(obj.Binary, data["binary"]) - t.Assert(obj.Date.Format("Y-m-d"), data["date"]) + t.Assert(obj.Date.Layout("Y-m-d"), data["date"]) t.Assert(obj.Time.String(), `10:00:01`) - t.Assert(obj.Timestamp.Format(`Y-m-d H:i:s.u`), `2022-02-14 12:00:01.123`) + t.Assert(obj.Timestamp.Layout(`Y-m-d H:i:s.u`), `2022-02-14 12:00:01.123`) t.Assert(obj.Decimal, -123.46) t.Assert(obj.Double, data["double"]) t.Assert(obj.Bit, data["bit"]) diff --git a/contrib/drivers/mysql/mysql_z_unit_model_test.go b/contrib/drivers/mysql/mysql_z_unit_model_test.go index 2bf86492e..1036ff218 100644 --- a/contrib/drivers/mysql/mysql_z_unit_model_test.go +++ b/contrib/drivers/mysql/mysql_z_unit_model_test.go @@ -3726,9 +3726,9 @@ func Test_Model_Year_Date_Time_DateTime_Timestamp(t *testing.T) { // select. one, err := db.Model("date_time_example").One(ctx) t.AssertNil(err) - t.Assert(one["year"].String(), now.Format("Y")) - t.Assert(one["date"].String(), now.Format("Y-m-d")) - t.Assert(one["time"].String(), now.Format("H:i:s")) + t.Assert(one["year"].String(), now.Layout("Y")) + t.Assert(one["date"].String(), now.Layout("Y-m-d")) + t.Assert(one["time"].String(), now.Layout("H:i:s")) t.AssertLT(one["datetime"].GTime().Sub(now).Seconds(), 5) t.AssertLT(one["timestamp"].GTime().Sub(now).Seconds(), 5) }) diff --git a/contrib/drivers/oracle/oracle_z_unit_model_test.go b/contrib/drivers/oracle/oracle_z_unit_model_test.go index 8443bbe3d..aa5b09228 100644 --- a/contrib/drivers/oracle/oracle_z_unit_model_test.go +++ b/contrib/drivers/oracle/oracle_z_unit_model_test.go @@ -1128,7 +1128,7 @@ func Test_Model_Save(t *testing.T) { user User count int result sql.Result - createTime = gtime.Now().Format("Y-m-d") + createTime = gtime.Now().Layout("Y-m-d") err error ) @@ -1150,7 +1150,7 @@ func Test_Model_Save(t *testing.T) { t.Assert(user.Passport, "p1") t.Assert(user.Password, "15d55ad283aa400af464c76d713c07ad") t.Assert(user.NickName, "n1") - t.Assert(user.CreateTime.Format("Y-m-d"), createTime) + t.Assert(user.CreateTime.Layout("Y-m-d"), createTime) _, err = db.Model(table).Data(g.Map{ "id": 1, @@ -1166,7 +1166,7 @@ func Test_Model_Save(t *testing.T) { t.Assert(user.Passport, "p1") t.Assert(user.Password, "25d55ad283aa400af464c76d713c07ad") t.Assert(user.NickName, "n2") - t.Assert(user.CreateTime.Format("Y-m-d"), createTime) + t.Assert(user.CreateTime.Layout("Y-m-d"), createTime) count, err = db.Model(table).Count(ctx) t.AssertNil(err) diff --git a/contrib/drivers/sqlite/sqlite_z_unit_core_test.go b/contrib/drivers/sqlite/sqlite_z_unit_core_test.go index b56b2d022..f0d8a869b 100644 --- a/contrib/drivers/sqlite/sqlite_z_unit_core_test.go +++ b/contrib/drivers/sqlite/sqlite_z_unit_core_test.go @@ -1505,7 +1505,7 @@ func Test_Types(t *testing.T) { t.Assert(one["binary"].String(), data["binary"]) t.Assert(one["date"].String(), data["date"]) t.Assert(one["time"].String(), `10:00:01`) - t.Assert(one["timestamp"].GTime().Format(`Y-m-d H:i:s.u`), `2022-02-14 12:00:01.123`) + t.Assert(one["timestamp"].GTime().Layout(`Y-m-d H:i:s.u`), `2022-02-14 12:00:01.123`) t.Assert(one["decimal"].String(), data["decimal"]) // In SQLite, the datatype of a value is associated with the value itself, not with its container. t.Assert(one["double"].String(), data["double"]) t.Assert(one["tinyint"].Bool(), data["tinyint"]) @@ -1528,9 +1528,9 @@ func Test_Types(t *testing.T) { t.Assert(obj.Id, 1) t.Assert(obj.Blob, data["blob"]) t.Assert(obj.Binary, data["binary"]) - t.Assert(obj.Date.Format("Y-m-d"), data["date"]) + t.Assert(obj.Date.Layout("Y-m-d"), data["date"]) t.Assert(obj.Time.String(), `10:00:01`) - t.Assert(obj.Timestamp.Format(`Y-m-d H:i:s.u`), `2022-02-14 12:00:01.123`) + t.Assert(obj.Timestamp.Layout(`Y-m-d H:i:s.u`), `2022-02-14 12:00:01.123`) t.Assert(obj.Decimal, data["decimal"]) t.Assert(obj.Double, data["double"]) t.Assert(obj.TinyInt, data["tinyint"]) diff --git a/contrib/drivers/sqlitecgo/sqlitecgo_z_unit_core_test.go b/contrib/drivers/sqlitecgo/sqlitecgo_z_unit_core_test.go index 39165f33a..c242c9b0f 100644 --- a/contrib/drivers/sqlitecgo/sqlitecgo_z_unit_core_test.go +++ b/contrib/drivers/sqlitecgo/sqlitecgo_z_unit_core_test.go @@ -1505,7 +1505,7 @@ func Test_Types(t *testing.T) { t.Assert(one["binary"].String(), data["binary"]) t.Assert(one["date"].String(), data["date"]) t.Assert(one["time"].String(), `10:00:01`) - t.Assert(one["timestamp"].GTime().Format(`Y-m-d H:i:s.u`), `2022-02-14 12:00:01.123`) + t.Assert(one["timestamp"].GTime().Layout(`Y-m-d H:i:s.u`), `2022-02-14 12:00:01.123`) t.Assert(one["decimal"].String(), data["decimal"]) // In SQLite, the datatype of a value is associated with the value itself, not with its container. t.Assert(one["double"].String(), data["double"]) t.Assert(one["tinyint"].Bool(), data["tinyint"]) @@ -1528,9 +1528,9 @@ func Test_Types(t *testing.T) { t.Assert(obj.Id, 1) t.Assert(obj.Blob, data["blob"]) t.Assert(obj.Binary, data["binary"]) - t.Assert(obj.Date.Format("Y-m-d"), data["date"]) + t.Assert(obj.Date.Layout("Y-m-d"), data["date"]) t.Assert(obj.Time.String(), `10:00:01`) - t.Assert(obj.Timestamp.Format(`Y-m-d H:i:s.u`), `2022-02-14 12:00:01.123`) + t.Assert(obj.Timestamp.Layout(`Y-m-d H:i:s.u`), `2022-02-14 12:00:01.123`) t.Assert(obj.Decimal, data["decimal"]) t.Assert(obj.Double, data["double"]) t.Assert(obj.TinyInt, data["tinyint"]) diff --git a/contrib/rpc/grpcx/grpcx_unit_z_grpc_server_config_test.go b/contrib/rpc/grpcx/grpcx_unit_z_grpc_server_config_test.go index 1def4e335..c2e638c7f 100644 --- a/contrib/rpc/grpcx/grpcx_unit_z_grpc_server_config_test.go +++ b/contrib/rpc/grpcx/grpcx_unit_z_grpc_server_config_test.go @@ -90,7 +90,7 @@ func Test_Grpcx_Grpc_Server_Config_Logger(t *testing.T) { time.Sleep(time.Millisecond * 100) defer s.Stop() - var logFilePath = fmt.Sprintf("/tmp/log/%s.log", gtime.Now().Format("Y-m-d")) + var logFilePath = fmt.Sprintf("/tmp/log/%s.log", gtime.Now().Layout("Y-m-d")) defer gfile.RemoveFile(logFilePath) t.Assert(gfile.Exists(logFilePath), true) t.Assert(s.Logger().GetConfig().Prefix, "TestLogger") diff --git a/database/gdb/gdb_core_structure.go b/database/gdb/gdb_core_structure.go index ab7c11657..3e1810005 100644 --- a/database/gdb/gdb_core_structure.go +++ b/database/gdb/gdb_core_structure.go @@ -170,11 +170,11 @@ Default: } else { switch fieldType { case fieldTypeYear: - convertedValue = r.Layout("2006") + convertedValue = r.Format("2006") case fieldTypeDate: - convertedValue = r.Layout("2006-01-02") + convertedValue = r.Format("2006-01-02") case fieldTypeTime: - convertedValue = r.Layout("15:04:05") + convertedValue = r.Format("15:04:05") default: convertedValue = r.Time } @@ -186,11 +186,11 @@ Default: } else { switch fieldType { case fieldTypeYear: - convertedValue = r.Layout("2006") + convertedValue = r.Format("2006") case fieldTypeDate: - convertedValue = r.Layout("2006-01-02") + convertedValue = r.Format("2006-01-02") case fieldTypeTime: - convertedValue = r.Layout("15:04:05") + convertedValue = r.Format("15:04:05") default: convertedValue = r.Time } @@ -422,17 +422,17 @@ func (c *Core) ConvertValueForLocal( case LocalTypeDate: if t, ok := fieldValue.(time.Time); ok { - return gtime.NewFromTime(t).Format("Y-m-d"), nil + return gtime.NewFromTime(t).Layout("Y-m-d"), nil } t, _ := gtime.StrToTime(gconv.String(fieldValue)) - return t.Format("Y-m-d"), nil + return t.Layout("Y-m-d"), nil case LocalTypeTime: if t, ok := fieldValue.(time.Time); ok { - return gtime.NewFromTime(t).Format("H:i:s"), nil + return gtime.NewFromTime(t).Layout("H:i:s"), nil } t, _ := gtime.StrToTime(gconv.String(fieldValue)) - return t.Format("H:i:s"), nil + return t.Layout("H:i:s"), nil case LocalTypeDatetime: if t, ok := fieldValue.(time.Time); ok { diff --git a/encoding/gjson/gjson_z_unit_test.go b/encoding/gjson/gjson_z_unit_test.go index 526d5342b..8ab242e9f 100644 --- a/encoding/gjson/gjson_z_unit_test.go +++ b/encoding/gjson/gjson_z_unit_test.go @@ -400,7 +400,7 @@ func Test_Convert2(t *testing.T) { t.Assert(j.Get("name").IsNil(), false) t.Assert(j.Len("name1"), -1) t.Assert(j.Get("time").Time().Format("2006-01-02"), "2019-06-12") - t.Assert(j.Get("time").GTime().Format("Y-m-d"), "2019-06-12") + t.Assert(j.Get("time").GTime().Layout("Y-m-d"), "2019-06-12") t.Assert(j.Get("time").Duration().String(), "0s") err := j.Var().Scan(&name) diff --git a/net/ghttp/ghttp_z_unit_feature_log_test.go b/net/ghttp/ghttp_z_unit_feature_log_test.go index 64e050f90..b35862745 100644 --- a/net/ghttp/ghttp_z_unit_feature_log_test.go +++ b/net/ghttp/ghttp_z_unit_feature_log_test.go @@ -47,17 +47,17 @@ func Test_Log(t *testing.T) { t.Assert(client.GetContent(ctx, "/error"), "exception recovered: custom error") var ( - logPath1 = gfile.Join(logDir, gtime.Now().Format("Y-m-d")+".log") + logPath1 = gfile.Join(logDir, gtime.Now().Layout("Y-m-d")+".log") content = gfile.GetContents(logPath1) ) t.Assert(gstr.Contains(content, "http server started listening on"), true) t.Assert(gstr.Contains(content, "HANDLER"), true) - logPath2 := gfile.Join(logDir, "access-"+gtime.Now().Format("Ymd")+".log") + logPath2 := gfile.Join(logDir, "access-"+gtime.Now().Layout("Ymd")+".log") // fmt.Println(gfile.GetContents(logPath2)) t.Assert(gstr.Contains(gfile.GetContents(logPath2), " /hello "), true) - logPath3 := gfile.Join(logDir, "error-"+gtime.Now().Format("Ymd")+".log") + logPath3 := gfile.Join(logDir, "error-"+gtime.Now().Layout("Ymd")+".log") // fmt.Println(gfile.GetContents(logPath3)) t.Assert(gstr.Contains(gfile.GetContents(logPath3), "custom error"), true) }) diff --git a/os/glog/glog_logger.go b/os/glog/glog_logger.go index b77308644..b2d687f18 100644 --- a/os/glog/glog_logger.go +++ b/os/glog/glog_logger.go @@ -87,7 +87,7 @@ func (l *Logger) Clone() *Logger { func (l *Logger) getFilePath(now time.Time) string { // Content containing "{}" in the file name is formatted using gtime. file, _ := gregex.ReplaceStringFunc(`{.+?}`, l.config.File, func(s string) string { - return gtime.New(now).Format(strings.Trim(s, "{}")) + return gtime.New(now).Layout(strings.Trim(s, "{}")) }) file = gfile.Join(l.config.Path, file) return file diff --git a/os/glog/glog_logger_rotate.go b/os/glog/glog_logger_rotate.go index 23375476f..412661cca 100644 --- a/os/glog/glog_logger_rotate.go +++ b/os/glog/glog_logger_rotate.go @@ -91,7 +91,7 @@ func (l *Logger) doRotateFile(ctx context.Context, filePath string) error { dirPath, fmt.Sprintf( `%s.%s%d.%s`, - fileName, now.Format("YmdHisu"), micro, fileExtName, + fileName, now.Layout("YmdHisu"), micro, fileExtName, ), ) if !gfile.Exists(newFilePath) { diff --git a/os/gtime/gtime.go b/os/gtime/gtime.go index 7fa534007..fb62f356c 100644 --- a/os/gtime/gtime.go +++ b/os/gtime/gtime.go @@ -155,12 +155,12 @@ func Datetime() string { return time.Now().Format("2006-01-02 15:04:05") } -// ISO8601 returns current datetime in ISO8601 format like "2006-01-02T15:04:05-07:00". +// ISO8601 returns current datetime in ISO8601 layout like "2006-01-02T15:04:05-07:00". func ISO8601() string { return time.Now().Format("2006-01-02T15:04:05-07:00") } -// RFC822 returns current datetime in RFC822 format like "Mon, 02 Jan 06 15:04 MST". +// RFC822 returns current datetime in RFC822 layout like "Mon, 02 Jan 06 15:04 MST". func RFC822() string { return time.Now().Format("Mon, 02 Jan 06 15:04 MST") } @@ -196,16 +196,16 @@ func parseDateStr(s string) (year, month, day int) { } // StrToTime converts string to *Time object. It also supports timestamp string. -// The parameter `format` is unnecessary, which specifies the format for converting like "Y-m-d H:i:s". -// If `format` is given, it acts as same as function StrToTimeFormat. -// If `format` is not given, it converts string as a "standard" datetime string. +// The parameter `layout` is unnecessary, which specifies the layout for converting like "Y-m-d H:i:s". +// If `layout` is given, it acts as same as function StrToTimeLayout. +// If `layout` is not given, it converts string as a "standard" datetime string. // Note that, it fails and returns error if there's no date string in `str`. -func StrToTime(str string, format ...string) (*Time, error) { +func StrToTime(str string, layout ...string) (*Time, error) { if str == "" { return &Time{wrapper{time.Time{}}}, nil } - if len(format) > 0 { - return StrToTimeFormat(str, format[0]) + if len(layout) > 0 { + return StrToTimeLayout(str, layout[0]) } if isTimestampStr(str) { timestamp, _ := strconv.ParseInt(str, 10, 64) @@ -255,7 +255,7 @@ func StrToTime(str string, format ...string) (*Time, error) { nsec *= 10 } } - // If there's zone information in the string, + // If there's zone inlayoution in the string, // it then performs time zone conversion, which converts the time zone to UTC. if match[4] != "" && match[6] == "" { match[6] = "000000" @@ -318,43 +318,43 @@ func ConvertZone(strTime string, toZone string, fromZone ...string) (*Time, erro } } -// StrToTimeFormat parses string `str` to *Time object with given format `format`. -// The parameter `format` is like "Y-m-d H:i:s". -func StrToTimeFormat(str string, format string) (*Time, error) { - return StrToTimeLayout(str, formatToStdLayout(format)) +// StrToTimeLayout parses string `str` to *Time object with given layout `layout`. +// The parameter `layout` is like "Y-m-d H:i:s". +func StrToTimeLayout(str string, layout string) (*Time, error) { + return StrToTimeFormat(str, layoutToStdFormat(layout)) } -// StrToTimeLayout parses string `str` to *Time object with given format `layout`. -// The parameter `layout` is in stdlib format like "2006-01-02 15:04:05". -func StrToTimeLayout(str string, layout string) (*Time, error) { - if t, err := time.ParseInLocation(layout, str, time.Local); err == nil { +// StrToTimeFormat parses string `str` to *Time object with given layout `format`. +// The parameter `format` is in stdlib layout like "2006-01-02 15:04:05". +func StrToTimeFormat(str string, format string) (*Time, error) { + if t, err := time.ParseInLocation(format, str, time.Local); err == nil { return NewFromTime(t), nil } else { return nil, gerror.WrapCodef( gcode.CodeInvalidParameter, err, - `time.ParseInLocation failed for layout "%s" and value "%s"`, - layout, str, + `time.ParseInLocation failed for format "%s" and value "%s"`, + format, str, ) } } -// ParseTimeFromContent retrieves time information for content string, it then parses and returns it +// ParseTimeFromContent retrieves time inlayoution for content string, it then parses and returns it // as *Time object. -// It returns the first time information if there are more than one time string in the content. -// It only retrieves and parses the time information with given first matched `format` if it's passed. -func ParseTimeFromContent(content string, format ...string) *Time { +// It returns the first time inlayoution if there are more than one time string in the content. +// It only retrieves and parses the time inlayoution with given first matched `layout` if it's passed. +func ParseTimeFromContent(content string, layout ...string) *Time { var ( err error match []string ) - if len(format) > 0 { - for _, item := range format { - match, err = gregex.MatchString(formatToRegexPattern(item), content) + if len(layout) > 0 { + for _, item := range layout { + match, err = gregex.MatchString(layoutToRegexPattern(item), content) if err != nil { intlog.Errorf(context.TODO(), `%+v`, err) } if len(match) > 0 { - return NewFromStrFormat(match[0], item) + return NewFromStrLayout(match[0], item) } } } else { diff --git a/os/gtime/gtime_format.go b/os/gtime/gtime_format.go index ffe549ba7..bc69786c1 100644 --- a/os/gtime/gtime_format.go +++ b/os/gtime/gtime_format.go @@ -17,7 +17,7 @@ import ( var ( // Refer: http://php.net/manual/en/function.date.php - formats = map[byte]string{ + layouts = map[byte]string{ 'd': "02", // Day: Day of the month, 2 digits with leading zeros. Eg: 01 to 31. 'D': "Mon", // Day: A textual representation of a day, three letters. Eg: Mon through Sun. 'w': "Monday", // Day: Numeric representation of the day of the week. Eg: 0 (for Sunday) through 6 (for Saturday). @@ -36,10 +36,10 @@ var ( 'y': "06", // Year: A two-digit representation of a year. Eg: 99 or 03. 'a': "pm", // Time: Lowercase Ante meridiem and Post meridiem. Eg: am or pm. 'A': "PM", // Time: Uppercase Ante meridiem and Post meridiem. Eg: AM or PM. - 'g': "3", // Time: 12-hour format of an hour without leading zeros. Eg: 1 through 12. - 'G': "=G=15", // Time: 24-hour format of an hour without leading zeros. Eg: 0 through 23. - 'h': "03", // Time: 12-hour format of an hour with leading zeros. Eg: 01 through 12. - 'H': "15", // Time: 24-hour format of an hour with leading zeros. Eg: 00 through 23. + 'g': "3", // Time: 12-hour layout of an hour without leading zeros. Eg: 1 through 12. + 'G': "=G=15", // Time: 24-hour layout of an hour without leading zeros. Eg: 0 through 23. + 'h': "03", // Time: 12-hour layout of an hour with leading zeros. Eg: 01 through 12. + 'H': "15", // Time: 24-hour layout of an hour with leading zeros. Eg: 00 through 23. 'i': "04", // Time: Minutes with leading zeros. Eg: 00 to 59. 's': "05", // Time: Seconds with leading zeros. Eg: 00 through 59. 'u': "=u=.000", // Time: Milliseconds. Eg: 234, 678. @@ -47,8 +47,8 @@ var ( 'O': "-0700", // Zone: Difference to Greenwich time (GMT) in hours. Eg: +0200. 'P': "-07:00", // Zone: Difference to Greenwich time (GMT) with colon between hours and minutes. Eg: +02:00. 'T': "MST", // Zone: Timezone abbreviation. Eg: UTC, EST, MDT ... - 'c': "2006-01-02T15:04:05-07:00", // Format: ISO 8601 date. Eg: 2004-02-12T15:19:21+00:00. - 'r': "Mon, 02 Jan 06 15:04 MST", // Format: RFC 2822 formatted date. Eg: Thu, 21 Dec 2000 16:01:07 +0200. + 'c': "2006-01-02T15:04:05-07:00", // Layout: ISO 8601 date. Eg: 2004-02-12T15:19:21+00:00. + 'r': "Mon, 02 Jan 06 15:04 MST", // Layout: RFC 2822 layoutted date. Eg: Thu, 21 Dec 2000 16:01:07 +0200. } // Week to number mapping. @@ -66,13 +66,13 @@ var ( dayOfMonth = []int{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334} ) -// Format formats and returns the formatted result with custom `format`. -// Refer method Layout if you want to follow stdlib layout. -func (t *Time) Format(format string) string { +// Layout layouts and returns the layoutted result with custom `layout`. +// Refer method Format if you want to follow stdlib format. +func (t *Time) Layout(layout string) string { if t == nil { return "" } - runes := []rune(format) + runes := []rune(layout) buffer := bytes.NewBuffer(nil) for i := 0; i < len(runes); { switch runes[i] { @@ -97,7 +97,7 @@ func (t *Time) Format(format string) string { buffer.WriteRune(runes[i]) break } - if f, ok := formats[byte(runes[i])]; ok { + if f, ok := layouts[byte(runes[i])]; ok { result := t.Time.Format(f) // Particular chars should be handled here. switch runes[i] { @@ -118,7 +118,7 @@ func (t *Time) Format(format string) string { case 'N': buffer.WriteString(strings.ReplaceAll(weekMap[result], "0", "7")) case 'S': - buffer.WriteString(formatMonthDaySuffixMap(result)) + buffer.WriteString(layoutMonthDaySuffixMap(result)) default: buffer.WriteString(result) } @@ -131,49 +131,49 @@ func (t *Time) Format(format string) string { return buffer.String() } -// FormatNew formats and returns a new Time object with given custom `format`. -func (t *Time) FormatNew(format string) *Time { - if t == nil { - return nil - } - return NewFromStr(t.Format(format)) -} - -// FormatTo formats `t` with given custom `format`. -func (t *Time) FormatTo(format string) *Time { - if t == nil { - return nil - } - t.Time = NewFromStr(t.Format(format)).Time - return t -} - -// Layout formats the time with stdlib layout and returns the formatted result. -func (t *Time) Layout(layout string) string { - if t == nil { - return "" - } - return t.Time.Format(layout) -} - -// LayoutNew formats the time with stdlib layout and returns the new Time object. +// LayoutNew layouts and returns a new Time object with given custom `layout`. func (t *Time) LayoutNew(layout string) *Time { if t == nil { return nil } - newTime, err := StrToTimeLayout(t.Layout(layout), layout) + return NewFromStr(t.Layout(layout)) +} + +// LayoutTo layouts `t` with given custom `layout`. +func (t *Time) LayoutTo(layout string) *Time { + if t == nil { + return nil + } + t.Time = NewFromStr(t.Layout(layout)).Time + return t +} + +// Format layouts the time with stdlib format and returns the layoutted result. +func (t *Time) Format(format string) string { + if t == nil { + return "" + } + return t.Time.Format(format) +} + +// FormatNew layouts the time with stdlib format and returns the new Time object. +func (t *Time) FormatNew(format string) *Time { + if t == nil { + return nil + } + newTime, err := StrToTimeFormat(t.Format(format), format) if err != nil { panic(err) } return newTime } -// LayoutTo formats `t` with stdlib layout. -func (t *Time) LayoutTo(layout string) *Time { +// FormatTo layouts `t` with stdlib format. +func (t *Time) FormatTo(format string) *Time { if t == nil { return nil } - newTime, err := StrToTimeLayout(t.Layout(layout), layout) + newTime, err := StrToTimeFormat(t.Format(format), format) if err != nil { panic(err) } @@ -225,14 +225,14 @@ func (t *Time) WeeksOfYear() int { return week } -// formatToStdLayout converts the custom format to stdlib layout. -func formatToStdLayout(format string) string { +// layoutToStdFormat converts the custom layout to stdlib format. +func layoutToStdFormat(layout string) string { b := bytes.NewBuffer(nil) - for i := 0; i < len(format); { - switch format[i] { + for i := 0; i < len(layout); { + switch layout[i] { case '\\': - if i < len(format)-1 { - b.WriteByte(format[i+1]) + if i < len(layout)-1 { + b.WriteByte(layout[i+1]) i += 2 continue } else { @@ -240,15 +240,15 @@ func formatToStdLayout(format string) string { } default: - if f, ok := formats[format[i]]; ok { + if f, ok := layouts[layout[i]]; ok { // Handle particular chars. - switch format[i] { + switch layout[i] { case 'j': b.WriteString("2") case 'G': b.WriteString("15") case 'u': - if i > 0 && format[i-1] == '.' { + if i > 0 && layout[i-1] == '.' { b.WriteString("000") } else { b.WriteString(".000") @@ -258,7 +258,7 @@ func formatToStdLayout(format string) string { b.WriteString(f) } } else { - b.WriteByte(format[i]) + b.WriteByte(layout[i]) } i++ } @@ -266,17 +266,17 @@ func formatToStdLayout(format string) string { return b.String() } -// formatToRegexPattern converts the custom format to its corresponding regular expression. -func formatToRegexPattern(format string) string { - s := regexp.QuoteMeta(formatToStdLayout(format)) +// layoutToRegexPattern converts the custom layout to its corresponding regular expression. +func layoutToRegexPattern(layout string) string { + s := regexp.QuoteMeta(layoutToStdFormat(layout)) s, _ = gregex.ReplaceString(`[0-9]`, `[0-9]`, s) s, _ = gregex.ReplaceString(`[A-Za-z]`, `[A-Za-z]`, s) s, _ = gregex.ReplaceString(`\s+`, `\s+`, s) return s } -// formatMonthDaySuffixMap returns the short english word for current day. -func formatMonthDaySuffixMap(day string) string { +// layoutMonthDaySuffixMap returns the short english word for current day. +func layoutMonthDaySuffixMap(day string) string { switch day { case "01", "21", "31": return "st" diff --git a/os/gtime/gtime_sql.go b/os/gtime/gtime_sql.go index 4fdd4902b..ba6761d50 100644 --- a/os/gtime/gtime_sql.go +++ b/os/gtime/gtime_sql.go @@ -6,7 +6,7 @@ import ( // Scan implements interface used by Scan in package database/sql for Scanning value // from database to local golang variable. -func (t *Time) Scan(value interface{}) error { +func (t *Time) Scan(value any) error { if t == nil { return nil } @@ -27,7 +27,7 @@ func (t *Time) Value() (driver.Value, error) { if t.Year() == 0 { // Only time. - return t.Format("15:04:05"), nil + return t.Layout("15:04:05"), nil } return t.Time, nil diff --git a/os/gtime/gtime_time.go b/os/gtime/gtime_time.go index ae4752af4..8b618bfcd 100644 --- a/os/gtime/gtime_time.go +++ b/os/gtime/gtime_time.go @@ -31,7 +31,7 @@ type iUnixNano interface { // New("2024-10-29") // New(1390876568) // New(t) // The t is type of time.Time. -func New(param ...interface{}) *Time { +func New(param ...any) *Time { if len(param) > 0 { switch r := param[0].(type) { case time.Time: @@ -49,9 +49,9 @@ func New(param ...interface{}) *Time { if len(param) > 1 { switch t := param[1].(type) { case string: - return NewFromStrFormat(r, t) + return NewFromStrLayout(r, t) case []byte: - return NewFromStrFormat(r, string(t)) + return NewFromStrLayout(r, string(t)) } } return NewFromStr(r) @@ -60,9 +60,9 @@ func New(param ...interface{}) *Time { if len(param) > 1 { switch t := param[1].(type) { case string: - return NewFromStrFormat(string(r), t) + return NewFromStrLayout(string(r), t) case []byte: - return NewFromStrFormat(string(r), string(t)) + return NewFromStrLayout(string(r), string(t)) } } return NewFromStr(string(r)) @@ -107,21 +107,21 @@ func NewFromStr(str string) *Time { return nil } -// NewFromStrFormat creates and returns a Time object with given string and -// custom format like: Y-m-d H:i:s. +// NewFromStrLayout creates and returns a Time object with given string and +// custom layout like: Y-m-d H:i:s. // Note that it returns nil if there's error occurs. -func NewFromStrFormat(str string, format string) *Time { - if t, err := StrToTimeFormat(str, format); err == nil { +func NewFromStrLayout(str string, layout string) *Time { + if t, err := StrToTimeLayout(str, layout); err == nil { return t } return nil } -// NewFromStrLayout creates and returns a Time object with given string and -// stdlib layout like: 2006-01-02 15:04:05. +// NewFromStrFormat creates and returns a Time object with given string and +// stdlib format like: 2006-01-02 15:04:05. // Note that it returns nil if there's error occurs. -func NewFromStrLayout(str string, layout string) *Time { - if t, err := StrToTimeLayout(str, layout); err == nil { +func NewFromStrFormat(str string, format string) *Time { + if t, err := StrToTimeFormat(str, format); err == nil { return t } return nil @@ -307,14 +307,14 @@ func (t *Time) UTC() *Time { return newTime } -// ISO8601 formats the time as ISO8601 and returns it as string. +// ISO8601 layouts the time as ISO8601 and returns it as string. func (t *Time) ISO8601() string { - return t.Layout("2006-01-02T15:04:05-07:00") + return t.Format("2006-01-02T15:04:05-07:00") } -// RFC822 formats the time as RFC822 and returns it as string. +// RFC822 layouts the time as RFC822 and returns it as string. func (t *Time) RFC822() string { - return t.Layout("Mon, 02 Jan 06 15:04 MST") + return t.Format("Mon, 02 Jan 06 15:04 MST") } // AddDate adds year, month and day to the time. @@ -550,7 +550,7 @@ func (t *Time) UnmarshalText(data []byte) error { func (t *Time) NoValidation() {} // DeepCopy implements interface for deep copy of current type. -func (t *Time) DeepCopy() interface{} { +func (t *Time) DeepCopy() any { if t == nil { return nil } diff --git a/os/gtime/gtime_z_bench_test.go b/os/gtime/gtime_z_bench_test.go index 2e154df9b..b17baea35 100644 --- a/os/gtime/gtime_z_bench_test.go +++ b/os/gtime/gtime_z_bench_test.go @@ -43,15 +43,15 @@ func Benchmark_StrToTime(b *testing.B) { } } -func Benchmark_StrToTime_Format(b *testing.B) { +func Benchmark_StrToTime_Layout(b *testing.B) { for i := 0; i < b.N; i++ { gtime.StrToTime("2018-02-09 20:46:17.897", "Y-m-d H:i:su") } } -func Benchmark_StrToTime_Layout(b *testing.B) { +func Benchmark_StrToTime_Format(b *testing.B) { for i := 0; i < b.N; i++ { - gtime.StrToTimeLayout("2018-02-09T20:46:17.897Z", time.RFC3339) + gtime.StrToTimeFormat("2018-02-09T20:46:17.897Z", time.RFC3339) } } diff --git a/os/gtime/gtime_z_example_basic_test.go b/os/gtime/gtime_z_example_basic_test.go index 0ddb6a5a9..c5719c4c1 100644 --- a/os/gtime/gtime_z_example_basic_test.go +++ b/os/gtime/gtime_z_example_basic_test.go @@ -100,16 +100,16 @@ func ExampleConvertZone() { // 2006-01-02 16:04:05 } -func ExampleStrToTimeFormat() { - res, _ := gtime.StrToTimeFormat("2006-01-02 15:04:05", "Y-m-d H:i:s") +func ExampleStrToTimeLayout() { + res, _ := gtime.StrToTimeLayout("2006-01-02 15:04:05", "Y-m-d H:i:s") fmt.Println(res) // Output: // 2006-01-02 15:04:05 } -func ExampleStrToTimeLayout() { - res, _ := gtime.StrToTimeLayout("2018-08-08", "2006-01-02") +func ExampleStrToTimeFormat() { + res, _ := gtime.StrToTimeFormat("2018-08-08", "2006-01-02") fmt.Println(res) // Output: @@ -131,15 +131,15 @@ func ExampleParseDuration() { // 10h0m0s } -func ExampleTime_Format() { +func ExampleTime_Layout() { gt1 := gtime.New("2018-08-08 08:08:08") - fmt.Println(gt1.Format("Y-m-d")) - fmt.Println(gt1.Format("l")) - fmt.Println(gt1.Format("F j, Y, g:i a")) - fmt.Println(gt1.Format("j, n, Y")) - fmt.Println(gt1.Format("h-i-s, j-m-y, it is w Day z")) - fmt.Println(gt1.Format("D M j G:i:s T Y")) + fmt.Println(gt1.Layout("Y-m-d")) + fmt.Println(gt1.Layout("l")) + fmt.Println(gt1.Layout("F j, Y, g:i a")) + fmt.Println(gt1.Layout("j, n, Y")) + fmt.Println(gt1.Layout("h-i-s, j-m-y, it is w Day z")) + fmt.Println(gt1.Layout("D M j G:i:s T Y")) // Output: // 2018-08-08 @@ -150,48 +150,48 @@ func ExampleTime_Format() { // Wed Aug 8 8:08:08 CST 2018 } -func ExampleTime_FormatNew() { +func ExampleTime_LayoutNew() { gt1 := gtime.New("2018-08-08 08:08:08") - fmt.Println(gt1.FormatNew("Y-m-d")) - fmt.Println(gt1.FormatNew("Y-m-d H:i")) + fmt.Println(gt1.LayoutNew("Y-m-d")) + fmt.Println(gt1.LayoutNew("Y-m-d H:i")) // Output: // 2018-08-08 00:00:00 // 2018-08-08 08:08:00 } -func ExampleTime_FormatTo() { +func ExampleTime_LayoutTo() { gt1 := gtime.New("2018-08-08 08:08:08") - fmt.Println(gt1.FormatTo("Y-m-d")) + fmt.Println(gt1.LayoutTo("Y-m-d")) // Output: // 2018-08-08 00:00:00 } -func ExampleTime_Layout() { +func ExampleTime_Format() { gt1 := gtime.New("2018-08-08 08:08:08") - fmt.Println(gt1.Layout("2006-01-02")) + fmt.Println(gt1.Format("2006-01-02")) // Output: // 2018-08-08 } -func ExampleTime_LayoutNew() { +func ExampleTime_FormatNew() { gt1 := gtime.New("2018-08-08 08:08:08") - fmt.Println(gt1.LayoutNew("2006-01-02")) + fmt.Println(gt1.FormatNew("2006-01-02")) // Output: // 2018-08-08 00:00:00 } -func ExampleTime_LayoutTo() { +func ExampleTime_FormatTo() { gt1 := gtime.New("2018-08-08 08:08:08") - fmt.Println(gt1.LayoutTo("2006-01-02")) + fmt.Println(gt1.FormatTo("2006-01-02")) // Output: // 2018-08-08 00:00:00 diff --git a/os/gtime/gtime_z_example_time_test.go b/os/gtime/gtime_z_example_time_test.go index 085966fa1..bfa3a5fe6 100644 --- a/os/gtime/gtime_z_example_time_test.go +++ b/os/gtime/gtime_z_example_time_test.go @@ -41,8 +41,8 @@ func ExampleNew_Basic() { // 08:08:08 } -func ExampleNew_WithFormat() { - fmt.Println(gtime.New("20220629133225", "YmdHis").Format("Y-m-d H:i:s")) +func ExampleNew_WithLayout() { + fmt.Println(gtime.New("20220629133225", "YmdHis").Layout("Y-m-d H:i:s")) // Output: // 2022-06-29 13:32:25 @@ -82,22 +82,22 @@ func ExampleNewFromStr() { // 08:08:08 } -// NewFromStrFormat creates and returns a Time object with given string and -// custom format like: Y-m-d H:i:s. +// NewFromStrLayout creates and returns a Time object with given string and +// custom layout like: Y-m-d H:i:s. // Note that it returns nil if there's error occurs. -func ExampleNewFromStrFormat() { - t := gtime.NewFromStrFormat("2018-08-08 08:08:08", "Y-m-d H:i:s") +func ExampleNewFromStrLayout() { + t := gtime.NewFromStrLayout("2018-08-08 08:08:08", "Y-m-d H:i:s") fmt.Println(t) // Output: // 2018-08-08 08:08:08 } -// NewFromStrLayout creates and returns a Time object with given string and -// stdlib layout like: 2006-01-02 15:04:05. +// NewFromStrFormat creates and returns a Time object with given string and +// stdlib format like: 2006-01-02 15:04:05. // Note that it returns nil if there's error occurs. -func ExampleNewFromStrLayout() { - t := gtime.NewFromStrLayout("2018-08-08 08:08:08", "2006-01-02 15:04:05") +func ExampleNewFromStrFormat() { + t := gtime.NewFromStrFormat("2018-08-08 08:08:08", "2006-01-02 15:04:05") fmt.Println(t) // Output: diff --git a/os/gtime/gtime_z_unit_format_test.go b/os/gtime/gtime_z_unit_format_test.go index df840c6c5..7478801a9 100644 --- a/os/gtime/gtime_z_unit_format_test.go +++ b/os/gtime/gtime_z_unit_format_test.go @@ -13,43 +13,43 @@ import ( "github.com/gogf/gf/v3/test/gtest" ) -func Test_Format(t *testing.T) { +func Test_Layout(t *testing.T) { gtest.C(t, func(t *gtest.T) { timeTemp, err := gtime.StrToTime("2006-01-11 15:04:05", "Y-m-d H:i:s") timeTemp.ToZone("Asia/Shanghai") if err != nil { t.Error("test fail") } - t.Assert(timeTemp.Format("\\T\\i\\m\\e中文Y-m-j G:i:s.u\\"), "Time中文2006-01-11 15:04:05.000") + t.Assert(timeTemp.Layout("\\T\\i\\m\\e中文Y-m-j G:i:s.u\\"), "Time中文2006-01-11 15:04:05.000") - t.Assert(timeTemp.Format("d D j l"), "11 Wed 11 Wednesday") + t.Assert(timeTemp.Layout("d D j l"), "11 Wed 11 Wednesday") - t.Assert(timeTemp.Format("F m M n"), "January 01 Jan 1") + t.Assert(timeTemp.Layout("F m M n"), "January 01 Jan 1") - t.Assert(timeTemp.Format("Y y"), "2006 06") + t.Assert(timeTemp.Layout("Y y"), "2006 06") - t.Assert(timeTemp.Format("a A g G h H i s u .u"), "pm PM 3 15 03 15 04 05 000 .000") + t.Assert(timeTemp.Layout("a A g G h H i s u .u"), "pm PM 3 15 03 15 04 05 000 .000") - t.Assert(timeTemp.Format("O P T"), "+0800 +08:00 CST") + t.Assert(timeTemp.Layout("O P T"), "+0800 +08:00 CST") - t.Assert(timeTemp.Format("r"), "Wed, 11 Jan 06 15:04 CST") + t.Assert(timeTemp.Layout("r"), "Wed, 11 Jan 06 15:04 CST") - t.Assert(timeTemp.Format("c"), "2006-01-11T15:04:05+08:00") + t.Assert(timeTemp.Layout("c"), "2006-01-11T15:04:05+08:00") //补零 timeTemp1, err := gtime.StrToTime("2006-01-02 03:04:05", "Y-m-d H:i:s") if err != nil { t.Error("test fail") } - t.Assert(timeTemp1.Format("Y-m-d h:i:s"), "2006-01-02 03:04:05") + t.Assert(timeTemp1.Layout("Y-m-d h:i:s"), "2006-01-02 03:04:05") //不补零 timeTemp2, err := gtime.StrToTime("2006-01-02 03:04:05", "Y-m-d H:i:s") if err != nil { t.Error("test fail") } - t.Assert(timeTemp2.Format("Y-n-j G:i:s"), "2006-1-2 3:04:05") + t.Assert(timeTemp2.Layout("Y-n-j G:i:s"), "2006-1-2 3:04:05") - t.Assert(timeTemp2.Format("U"), "1136142245") + t.Assert(timeTemp2.Layout("U"), "1136142245") // 测试数字型的星期 times := []map[string]string{ @@ -88,22 +88,22 @@ func Test_Format(t *testing.T) { for _, v := range times { t1, err1 := gtime.StrToTime(v["k"], "Y-m-d") t.Assert(err1, nil) - t.Assert(t1.Format(v["f"]), v["r"]) + t.Assert(t1.Layout(v["f"]), v["r"]) } }) gtest.C(t, func(t *gtest.T) { var ti *gtime.Time = nil - t.Assert(ti.Format("Y-m-d h:i:s"), "") - t.Assert(ti.FormatNew("Y-m-d h:i:s"), nil) - t.Assert(ti.FormatTo("Y-m-d h:i:s"), nil) t.Assert(ti.Layout("Y-m-d h:i:s"), "") t.Assert(ti.LayoutNew("Y-m-d h:i:s"), nil) t.Assert(ti.LayoutTo("Y-m-d h:i:s"), nil) + t.Assert(ti.Format("Y-m-d h:i:s"), "") + t.Assert(ti.FormatNew("Y-m-d h:i:s"), nil) + t.Assert(ti.LayoutTo("Y-m-d h:i:s"), nil) }) } -func Test_Format_ZeroString(t *testing.T) { +func Test_Layout_ZeroString(t *testing.T) { gtest.C(t, func(t *gtest.T) { timeTemp, err := gtime.StrToTime("0000-00-00 00:00:00") t.AssertNE(err, nil) @@ -111,23 +111,23 @@ func Test_Format_ZeroString(t *testing.T) { }) } -func Test_FormatTo(t *testing.T) { - gtest.C(t, func(t *gtest.T) { - timeTemp := gtime.Now() - t.Assert(timeTemp.FormatTo("Y-m-01 00:00:01"), timeTemp.Time.Format("2006-01")+"-01 00:00:01") - }) -} - -func Test_Layout(t *testing.T) { - gtest.C(t, func(t *gtest.T) { - timeTemp := gtime.Now() - t.Assert(timeTemp.Layout("2006-01-02 15:04:05"), timeTemp.Time.Format("2006-01-02 15:04:05")) - }) -} - func Test_LayoutTo(t *testing.T) { gtest.C(t, func(t *gtest.T) { timeTemp := gtime.Now() - t.Assert(timeTemp.LayoutTo("2006-01-02 00:00:00"), timeTemp.Time.Format("2006-01-02 00:00:00")) + t.Assert(timeTemp.LayoutTo("Y-m-01 00:00:01"), timeTemp.Time.Format("2006-01")+"-01 00:00:01") + }) +} + +func Test_Format(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + timeTemp := gtime.Now() + t.Assert(timeTemp.Format("2006-01-02 15:04:05"), timeTemp.Time.Format("2006-01-02 15:04:05")) + }) +} + +func Test_FormatTo(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + timeTemp := gtime.Now() + t.Assert(timeTemp.FormatTo("2006-01-02 00:00:00"), timeTemp.Time.Format("2006-01-02 00:00:00")) }) } diff --git a/os/gtime/gtime_z_unit_issue_test.go b/os/gtime/gtime_z_unit_issue_test.go index 72c98ab13..74fe1872e 100644 --- a/os/gtime/gtime_z_unit_issue_test.go +++ b/os/gtime/gtime_z_unit_issue_test.go @@ -27,7 +27,7 @@ func Test_Issue1681(t *testing.T) { // https://github.com/gogf/gf/issues/2803 func Test_Issue2803(t *testing.T) { gtest.C(t, func(t *gtest.T) { - newTime := gtime.New("2023-07-26").LayoutTo("2006-01") + newTime := gtime.New("2023-07-26").FormatTo("2006-01") t.Assert(newTime.Year(), 2023) t.Assert(newTime.Month(), 7) t.Assert(newTime.Day(), 1) @@ -51,9 +51,9 @@ func Test_Issue3558(t *testing.T) { stdTime, err := time.Parse(time.RFC3339, timeStr) t.AssertNil(err) - stdTimeFormat := stdTime.Format("2006-01-02 15:04:05") - gfTimeFormat := gfTime.Format("Y-m-d H:i:s") - t.Assert(gfTimeFormat, stdTimeFormat) + stdTimeLayout := stdTime.Format("2006-01-02 15:04:05") + gfTimeLayout := gfTime.Layout("Y-m-d H:i:s") + t.Assert(gfTimeLayout, stdTimeLayout) }) gtest.C(t, func(t *gtest.T) { timeStr := "1880-10-24T00:00:00-08:05" @@ -66,8 +66,8 @@ func Test_Issue3558(t *testing.T) { t.Assert(gfTime.Second(), 0) stdTime, err := time.Parse(time.RFC3339, timeStr) t.AssertNil(err) - stdTimeFormat := stdTime.Format("2006-01-02 15:04:05") - gfTimeFormat := gfTime.Format("Y-m-d H:i:s") - t.Assert(gfTimeFormat, stdTimeFormat) + stdTimeLayout := stdTime.Format("2006-01-02 15:04:05") + gfTimeLayout := gfTime.Layout("Y-m-d H:i:s") + t.Assert(gfTimeLayout, stdTimeLayout) }) } diff --git a/os/gtime/gtime_z_unit_test.go b/os/gtime/gtime_z_unit_test.go index 16f199e57..69ac0a785 100644 --- a/os/gtime/gtime_z_unit_test.go +++ b/os/gtime/gtime_z_unit_test.go @@ -83,14 +83,14 @@ func Test_Datetime(t *testing.T) { func Test_ISO8601(t *testing.T) { gtest.C(t, func(t *gtest.T) { iso8601 := gtime.ISO8601() - t.Assert(iso8601, gtime.Now().Format("c")) + t.Assert(iso8601, gtime.Now().Layout("c")) }) } func Test_RFC822(t *testing.T) { gtest.C(t, func(t *gtest.T) { rfc822 := gtime.RFC822() - t.Assert(rfc822, gtime.Now().Format("r")) + t.Assert(rfc822, gtime.Now().Layout("r")) }) } @@ -145,8 +145,8 @@ func Test_StrToTime(t *testing.T) { t.Assert(time1.Time, time2) } - // formatToStdLayout - var testDateFormats = []string{ + // layoutToStdFormat + var testDateLayouts = []string{ "Y-m-d H:i:s", "\\T\\i\\m\\e Y-m-d H:i:s", "Y-m-d H:i:s\\", @@ -154,7 +154,7 @@ func Test_StrToTime(t *testing.T) { "Y-m-j G:i:su", } - var testDateFormatsResult = []string{ + var testDateLayoutsResult = []string{ "2007-01-02 15:04:05", "Time 2007-01-02 15:04:05", "2007-01-02 15:04:05", @@ -162,8 +162,8 @@ func Test_StrToTime(t *testing.T) { "2007-01-02 15:04:05.000", } - for index, item := range testDateFormats { - timeTemp, err := gtime.StrToTime(testDateFormatsResult[index], item) + for index, item := range testDateLayouts { + timeTemp, err := gtime.StrToTime(testDateLayoutsResult[index], item) if err != nil { t.Error("test fail") } diff --git a/os/gtime/gtime_z_unit_time_test.go b/os/gtime/gtime_z_unit_time_test.go index d62f0a0da..93100c91d 100644 --- a/os/gtime/gtime_z_unit_time_test.go +++ b/os/gtime/gtime_z_unit_time_test.go @@ -47,23 +47,23 @@ func Test_New(t *testing.T) { // short datetime. gtest.C(t, func(t *gtest.T) { timeTemp := gtime.New("2021-2-9 08:01:21") - t.Assert(timeTemp.Format("Y-m-d H:i:s"), "2021-02-09 08:01:21") + t.Assert(timeTemp.Layout("Y-m-d H:i:s"), "2021-02-09 08:01:21") t.Assert(timeTemp.Time.Format("2006-01-02 15:04:05"), "2021-02-09 08:01:21") timeTemp = gtime.New("2021-02-09 08:01:21", []byte("Y-m-d H:i:s")) - t.Assert(timeTemp.Format("Y-m-d H:i:s"), "2021-02-09 08:01:21") + t.Assert(timeTemp.Layout("Y-m-d H:i:s"), "2021-02-09 08:01:21") t.Assert(timeTemp.Time.Format("2006-01-02 15:04:05"), "2021-02-09 08:01:21") timeTemp = gtime.New([]byte("2021-02-09 08:01:21")) - t.Assert(timeTemp.Format("Y-m-d H:i:s"), "2021-02-09 08:01:21") + t.Assert(timeTemp.Layout("Y-m-d H:i:s"), "2021-02-09 08:01:21") t.Assert(timeTemp.Time.Format("2006-01-02 15:04:05"), "2021-02-09 08:01:21") timeTemp = gtime.New([]byte("2021-02-09 08:01:21"), "Y-m-d H:i:s") - t.Assert(timeTemp.Format("Y-m-d H:i:s"), "2021-02-09 08:01:21") + t.Assert(timeTemp.Layout("Y-m-d H:i:s"), "2021-02-09 08:01:21") t.Assert(timeTemp.Time.Format("2006-01-02 15:04:05"), "2021-02-09 08:01:21") timeTemp = gtime.New([]byte("2021-02-09 08:01:21"), []byte("Y-m-d H:i:s")) - t.Assert(timeTemp.Format("Y-m-d H:i:s"), "2021-02-09 08:01:21") + t.Assert(timeTemp.Layout("Y-m-d H:i:s"), "2021-02-09 08:01:21") t.Assert(timeTemp.Time.Format("2006-01-02 15:04:05"), "2021-02-09 08:01:21") }) // @@ -103,7 +103,7 @@ func Test_Nil(t *testing.T) { func Test_NewFromStr(t *testing.T) { gtest.C(t, func(t *gtest.T) { timeTemp := gtime.NewFromStr("2006-01-02 15:04:05") - t.Assert(timeTemp.Format("Y-m-d H:i:s"), "2006-01-02 15:04:05") + t.Assert(timeTemp.Layout("Y-m-d H:i:s"), "2006-01-02 15:04:05") timeTemp1 := gtime.NewFromStr("2006.0102") if timeTemp1 != nil { @@ -124,32 +124,32 @@ func Test_String(t *testing.T) { }) } -func Test_NewFromStrFormat(t *testing.T) { +func Test_NewFromStrLayout(t *testing.T) { gtest.C(t, func(t *gtest.T) { - timeTemp := gtime.NewFromStrFormat("2006-01-02 15:04:05", "Y-m-d H:i:s") - t.Assert(timeTemp.Format("Y-m-d H:i:s"), "2006-01-02 15:04:05") + timeTemp := gtime.NewFromStrLayout("2006-01-02 15:04:05", "Y-m-d H:i:s") + t.Assert(timeTemp.Layout("Y-m-d H:i:s"), "2006-01-02 15:04:05") - timeTemp1 := gtime.NewFromStrFormat("2006-01-02 15:04:05", "aabbcc") + timeTemp1 := gtime.NewFromStrLayout("2006-01-02 15:04:05", "aabbcc") if timeTemp1 != nil { t.Error("test fail") } }) gtest.C(t, func(t *gtest.T) { - t1 := gtime.NewFromStrFormat("2019/2/1", "Y/n/j") - t.Assert(t1.Format("Y-m-d"), "2019-02-01") + t1 := gtime.NewFromStrLayout("2019/2/1", "Y/n/j") + t.Assert(t1.Layout("Y-m-d"), "2019-02-01") - t2 := gtime.NewFromStrFormat("2019/10/12", "Y/n/j") - t.Assert(t2.Format("Y-m-d"), "2019-10-12") + t2 := gtime.NewFromStrLayout("2019/10/12", "Y/n/j") + t.Assert(t2.Layout("Y-m-d"), "2019-10-12") }) } -func Test_NewFromStrLayout(t *testing.T) { +func Test_NewFromStrFormat(t *testing.T) { gtest.C(t, func(t *gtest.T) { - timeTemp := gtime.NewFromStrLayout("2006-01-02 15:04:05", "2006-01-02 15:04:05") - t.Assert(timeTemp.Format("Y-m-d H:i:s"), "2006-01-02 15:04:05") + timeTemp := gtime.NewFromStrFormat("2006-01-02 15:04:05", "2006-01-02 15:04:05") + t.Assert(timeTemp.Layout("Y-m-d H:i:s"), "2006-01-02 15:04:05") - timeTemp1 := gtime.NewFromStrLayout("2006-01-02 15:04:05", "aabbcc") + timeTemp1 := gtime.NewFromStrFormat("2006-01-02 15:04:05", "aabbcc") if timeTemp1 != nil { t.Error("test fail") } @@ -159,11 +159,11 @@ func Test_NewFromStrLayout(t *testing.T) { func Test_NewFromTimeStamp(t *testing.T) { gtest.C(t, func(t *gtest.T) { timeTemp := gtime.NewFromTimeStamp(1554459846000) - t.Assert(timeTemp.Format("Y-m-d H:i:s"), "2019-04-05 18:24:06") + t.Assert(timeTemp.Layout("Y-m-d H:i:s"), "2019-04-05 18:24:06") timeTemp1 := gtime.NewFromTimeStamp(0) - t.Assert(timeTemp1.Format("Y-m-d H:i:s"), "0001-01-01 00:00:00") + t.Assert(timeTemp1.Layout("Y-m-d H:i:s"), "0001-01-01 00:00:00") timeTemp2 := gtime.NewFromTimeStamp(155445984) - t.Assert(timeTemp2.Format("Y-m-d H:i:s"), "1974-12-05 11:26:24") + t.Assert(timeTemp2.Layout("Y-m-d H:i:s"), "1974-12-05 11:26:24") }) } @@ -249,14 +249,14 @@ func Test_Time_String(t *testing.T) { func Test_Time_ISO8601(t *testing.T) { gtest.C(t, func(t *gtest.T) { now := gtime.Now() - t.Assert(now.ISO8601(), now.Format("c")) + t.Assert(now.ISO8601(), now.Layout("c")) }) } func Test_Time_RFC822(t *testing.T) { gtest.C(t, func(t *gtest.T) { now := gtime.Now() - t.Assert(now.RFC822(), now.Format("r")) + t.Assert(now.RFC822(), now.Layout("r")) }) } @@ -280,7 +280,7 @@ func Test_Add(t *testing.T) { gtest.C(t, func(t *gtest.T) { timeTemp := gtime.NewFromStr("2006-01-02 15:04:05") timeTemp = timeTemp.Add(time.Second) - t.Assert(timeTemp.Format("Y-m-d H:i:s"), "2006-01-02 15:04:06") + t.Assert(timeTemp.Layout("Y-m-d H:i:s"), "2006-01-02 15:04:06") }) } @@ -308,7 +308,7 @@ func Test_AddDate(t *testing.T) { gtest.C(t, func(t *gtest.T) { timeTemp := gtime.NewFromStr("2006-01-02 15:04:05") timeTemp = timeTemp.AddDate(1, 2, 3) - t.Assert(timeTemp.Format("Y-m-d H:i:s"), "2007-03-05 15:04:05") + t.Assert(timeTemp.Layout("Y-m-d H:i:s"), "2007-03-05 15:04:05") }) } @@ -352,7 +352,7 @@ func Test_StartOfMinute(t *testing.T) { gtest.C(t, func(t *gtest.T) { timeTemp := gtime.NewFromStr("2020-12-12 18:24:06") timeTemp1 := timeTemp.StartOfMinute() - t.Assert(timeTemp1.Format("Y-m-d H:i:s"), "2020-12-12 18:24:00") + t.Assert(timeTemp1.Layout("Y-m-d H:i:s"), "2020-12-12 18:24:00") }) } @@ -360,12 +360,12 @@ func Test_EndOfMinute(t *testing.T) { gtest.C(t, func(t *gtest.T) { timeTemp := gtime.NewFromStr("2020-12-12 18:24:06") timeTemp1 := timeTemp.EndOfMinute() - t.Assert(timeTemp1.Format("Y-m-d H:i:s.u"), "2020-12-12 18:24:59.000") + t.Assert(timeTemp1.Layout("Y-m-d H:i:s.u"), "2020-12-12 18:24:59.000") }) gtest.C(t, func(t *gtest.T) { timeTemp := gtime.NewFromStr("2020-12-12 18:24:06") timeTemp1 := timeTemp.EndOfMinute(true) - t.Assert(timeTemp1.Format("Y-m-d H:i:s.u"), "2020-12-12 18:24:59.999") + t.Assert(timeTemp1.Layout("Y-m-d H:i:s.u"), "2020-12-12 18:24:59.999") }) } @@ -373,7 +373,7 @@ func Test_StartOfHour(t *testing.T) { gtest.C(t, func(t *gtest.T) { timeTemp := gtime.NewFromStr("2020-12-12 18:24:06") timeTemp1 := timeTemp.StartOfHour() - t.Assert(timeTemp1.Format("Y-m-d H:i:s"), "2020-12-12 18:00:00") + t.Assert(timeTemp1.Layout("Y-m-d H:i:s"), "2020-12-12 18:00:00") }) } @@ -381,12 +381,12 @@ func Test_EndOfHour(t *testing.T) { gtest.C(t, func(t *gtest.T) { timeTemp := gtime.NewFromStr("2020-12-12 18:24:06") timeTemp1 := timeTemp.EndOfHour() - t.Assert(timeTemp1.Format("Y-m-d H:i:s.u"), "2020-12-12 18:59:59.000") + t.Assert(timeTemp1.Layout("Y-m-d H:i:s.u"), "2020-12-12 18:59:59.000") }) gtest.C(t, func(t *gtest.T) { timeTemp := gtime.NewFromStr("2020-12-12 18:24:06") timeTemp1 := timeTemp.EndOfHour(true) - t.Assert(timeTemp1.Format("Y-m-d H:i:s.u"), "2020-12-12 18:59:59.999") + t.Assert(timeTemp1.Layout("Y-m-d H:i:s.u"), "2020-12-12 18:59:59.999") }) } @@ -394,7 +394,7 @@ func Test_StartOfDay(t *testing.T) { gtest.C(t, func(t *gtest.T) { timeTemp := gtime.NewFromStr("2020-12-12 18:24:06") timeTemp1 := timeTemp.StartOfDay() - t.Assert(timeTemp1.Format("Y-m-d H:i:s"), "2020-12-12 00:00:00") + t.Assert(timeTemp1.Layout("Y-m-d H:i:s"), "2020-12-12 00:00:00") }) } @@ -402,12 +402,12 @@ func Test_EndOfDay(t *testing.T) { gtest.C(t, func(t *gtest.T) { timeTemp := gtime.NewFromStr("2020-12-12 18:24:06") timeTemp1 := timeTemp.EndOfDay() - t.Assert(timeTemp1.Format("Y-m-d H:i:s.u"), "2020-12-12 23:59:59.000") + t.Assert(timeTemp1.Layout("Y-m-d H:i:s.u"), "2020-12-12 23:59:59.000") }) gtest.C(t, func(t *gtest.T) { timeTemp := gtime.NewFromStr("2020-12-12 18:24:06") timeTemp1 := timeTemp.EndOfDay(true) - t.Assert(timeTemp1.Format("Y-m-d H:i:s.u"), "2020-12-12 23:59:59.999") + t.Assert(timeTemp1.Layout("Y-m-d H:i:s.u"), "2020-12-12 23:59:59.999") }) } @@ -415,7 +415,7 @@ func Test_StartOfWeek(t *testing.T) { gtest.C(t, func(t *gtest.T) { timeTemp := gtime.NewFromStr("2020-12-12 18:24:06") timeTemp1 := timeTemp.StartOfWeek() - t.Assert(timeTemp1.Format("Y-m-d H:i:s"), "2020-12-06 00:00:00") + t.Assert(timeTemp1.Layout("Y-m-d H:i:s"), "2020-12-06 00:00:00") }) } @@ -423,12 +423,12 @@ func Test_EndOfWeek(t *testing.T) { gtest.C(t, func(t *gtest.T) { timeTemp := gtime.NewFromStr("2020-12-12 18:24:06") timeTemp1 := timeTemp.EndOfWeek() - t.Assert(timeTemp1.Format("Y-m-d H:i:s.u"), "2020-12-12 23:59:59.000") + t.Assert(timeTemp1.Layout("Y-m-d H:i:s.u"), "2020-12-12 23:59:59.000") }) gtest.C(t, func(t *gtest.T) { timeTemp := gtime.NewFromStr("2020-12-12 18:24:06") timeTemp1 := timeTemp.EndOfWeek(true) - t.Assert(timeTemp1.Format("Y-m-d H:i:s.u"), "2020-12-12 23:59:59.999") + t.Assert(timeTemp1.Layout("Y-m-d H:i:s.u"), "2020-12-12 23:59:59.999") }) } @@ -436,7 +436,7 @@ func Test_StartOfMonth(t *testing.T) { gtest.C(t, func(t *gtest.T) { timeTemp := gtime.NewFromStr("2020-12-12 18:24:06") timeTemp1 := timeTemp.StartOfMonth() - t.Assert(timeTemp1.Format("Y-m-d H:i:s"), "2020-12-01 00:00:00") + t.Assert(timeTemp1.Layout("Y-m-d H:i:s"), "2020-12-01 00:00:00") }) } @@ -444,12 +444,12 @@ func Test_EndOfMonth(t *testing.T) { gtest.C(t, func(t *gtest.T) { timeTemp := gtime.NewFromStr("2020-12-12 18:24:06") timeTemp1 := timeTemp.EndOfMonth() - t.Assert(timeTemp1.Format("Y-m-d H:i:s.u"), "2020-12-31 23:59:59.000") + t.Assert(timeTemp1.Layout("Y-m-d H:i:s.u"), "2020-12-31 23:59:59.000") }) gtest.C(t, func(t *gtest.T) { timeTemp := gtime.NewFromStr("2020-12-12 18:24:06") timeTemp1 := timeTemp.EndOfMonth(true) - t.Assert(timeTemp1.Format("Y-m-d H:i:s.u"), "2020-12-31 23:59:59.999") + t.Assert(timeTemp1.Layout("Y-m-d H:i:s.u"), "2020-12-31 23:59:59.999") }) } @@ -457,7 +457,7 @@ func Test_StartOfQuarter(t *testing.T) { gtest.C(t, func(t *gtest.T) { timeTemp := gtime.NewFromStr("2020-12-06 18:24:06") timeTemp1 := timeTemp.StartOfQuarter() - t.Assert(timeTemp1.Format("Y-m-d H:i:s"), "2020-10-01 00:00:00") + t.Assert(timeTemp1.Layout("Y-m-d H:i:s"), "2020-10-01 00:00:00") }) } @@ -465,12 +465,12 @@ func Test_EndOfQuarter(t *testing.T) { gtest.C(t, func(t *gtest.T) { timeTemp := gtime.NewFromStr("2020-12-06 18:24:06") timeTemp1 := timeTemp.EndOfQuarter() - t.Assert(timeTemp1.Format("Y-m-d H:i:s.u"), "2020-12-31 23:59:59.000") + t.Assert(timeTemp1.Layout("Y-m-d H:i:s.u"), "2020-12-31 23:59:59.000") }) gtest.C(t, func(t *gtest.T) { timeTemp := gtime.NewFromStr("2020-12-06 18:24:06") timeTemp1 := timeTemp.EndOfQuarter(true) - t.Assert(timeTemp1.Format("Y-m-d H:i:s.u"), "2020-12-31 23:59:59.999") + t.Assert(timeTemp1.Layout("Y-m-d H:i:s.u"), "2020-12-31 23:59:59.999") }) } @@ -478,7 +478,7 @@ func Test_StartOfHalf(t *testing.T) { gtest.C(t, func(t *gtest.T) { timeTemp := gtime.NewFromStr("2020-12-06 18:24:06") timeTemp1 := timeTemp.StartOfHalf() - t.Assert(timeTemp1.Format("Y-m-d H:i:s"), "2020-07-01 00:00:00") + t.Assert(timeTemp1.Layout("Y-m-d H:i:s"), "2020-07-01 00:00:00") }) } @@ -486,12 +486,12 @@ func Test_EndOfHalf(t *testing.T) { gtest.C(t, func(t *gtest.T) { timeTemp := gtime.NewFromStr("2020-12-06 18:24:06") timeTemp1 := timeTemp.EndOfHalf() - t.Assert(timeTemp1.Format("Y-m-d H:i:s.u"), "2020-12-31 23:59:59.000") + t.Assert(timeTemp1.Layout("Y-m-d H:i:s.u"), "2020-12-31 23:59:59.000") }) gtest.C(t, func(t *gtest.T) { timeTemp := gtime.NewFromStr("2020-12-06 18:24:06") timeTemp1 := timeTemp.EndOfHalf(true) - t.Assert(timeTemp1.Format("Y-m-d H:i:s.u"), "2020-12-31 23:59:59.999") + t.Assert(timeTemp1.Layout("Y-m-d H:i:s.u"), "2020-12-31 23:59:59.999") }) } @@ -499,7 +499,7 @@ func Test_StartOfYear(t *testing.T) { gtest.C(t, func(t *gtest.T) { timeTemp := gtime.NewFromStr("2020-12-06 18:24:06") timeTemp1 := timeTemp.StartOfYear() - t.Assert(timeTemp1.Format("Y-m-d H:i:s"), "2020-01-01 00:00:00") + t.Assert(timeTemp1.Layout("Y-m-d H:i:s"), "2020-01-01 00:00:00") }) } @@ -507,12 +507,12 @@ func Test_EndOfYear(t *testing.T) { gtest.C(t, func(t *gtest.T) { timeTemp := gtime.NewFromStr("2020-12-06 18:24:06") timeTemp1 := timeTemp.EndOfYear() - t.Assert(timeTemp1.Format("Y-m-d H:i:s.u"), "2020-12-31 23:59:59.000") + t.Assert(timeTemp1.Layout("Y-m-d H:i:s.u"), "2020-12-31 23:59:59.000") }) gtest.C(t, func(t *gtest.T) { timeTemp := gtime.NewFromStr("2020-12-06 18:24:06") timeTemp1 := timeTemp.EndOfYear(true) - t.Assert(timeTemp1.Format("Y-m-d H:i:s.u"), "2020-12-31 23:59:59.999") + t.Assert(timeTemp1.Layout("Y-m-d H:i:s.u"), "2020-12-31 23:59:59.999") }) } diff --git a/os/gview/gview_buildin.go b/os/gview/gview_buildin.go index f4d110770..4801a10c4 100644 --- a/os/gview/gview_buildin.go +++ b/os/gview/gview_buildin.go @@ -169,7 +169,7 @@ func (view *View) buildInFuncDate(format interface{}, timestamp ...interface{}) if t == 0 { t = gtime.Timestamp() } - return gtime.NewFromTimeStamp(t).Format(gconv.String(format)) + return gtime.NewFromTimeStamp(t).Layout(gconv.String(format)) } // buildInFuncCompare implements build-in template function: compare diff --git a/util/gvalid/internal/builtin/builtin_date_format.go b/util/gvalid/internal/builtin/builtin_date_format.go index b131c5561..ec9eca054 100644 --- a/util/gvalid/internal/builtin/builtin_date_format.go +++ b/util/gvalid/internal/builtin/builtin_date_format.go @@ -43,7 +43,7 @@ func (r RuleDateFormat) Run(in RunInput) error { } return nil } - if _, err := gtime.StrToTimeFormat(in.Value.String(), in.RulePattern); err != nil { + if _, err := gtime.StrToTimeLayout(in.Value.String(), in.RulePattern); err != nil { return errors.New(in.Message) } return nil diff --git a/util/gvalid/internal/builtin/builtin_datetime.go b/util/gvalid/internal/builtin/builtin_datetime.go index 839f4e1c6..af7382fbc 100644 --- a/util/gvalid/internal/builtin/builtin_datetime.go +++ b/util/gvalid/internal/builtin/builtin_datetime.go @@ -43,7 +43,7 @@ func (r RuleDatetime) Run(in RunInput) error { } return nil } - if _, err := gtime.StrToTimeFormat(in.Value.String(), `Y-m-d H:i:s`); err != nil { + if _, err := gtime.StrToTimeLayout(in.Value.String(), `Y-m-d H:i:s`); err != nil { return errors.New(in.Message) } return nil