mirror of
https://github.com/duke-git/lancet.git
synced 2026-02-04 12:52:28 +08:00
test&doc: add example and update doc for datetime package
This commit is contained in:
137
README.md
137
README.md
@@ -348,36 +348,99 @@ import "github.com/duke-git/lancet/v2/datetime"
|
|||||||
|
|
||||||
#### Function list:
|
#### Function list:
|
||||||
|
|
||||||
- [AddDay](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#AddDay)
|
- **<big>AddDay</big>** : add or sub day to the time.
|
||||||
- [AddHour](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#AddHour)
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#AddDay)]
|
||||||
- [AddMinute](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#AddMinute)
|
[[play](https://go.dev/play/p/dIGbs_uTdFa)]
|
||||||
- [BeginOfMinute](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfMinute)
|
- **<big>AddHour</big>** : add or sub day to the time.
|
||||||
- [BeginOfHour](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfHour)
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#AddHour)]
|
||||||
- [BeginOfDay](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfDay)
|
[[play](https://go.dev/play/p/rcMjd7OCsi5)]
|
||||||
- [BeginOfWeek](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfWeek)
|
- **<big>AddMinute</big>** : add or sub day to the time.
|
||||||
- [BeginOfMonth](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfMonth)
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#AddMinute)]
|
||||||
- [BeginOfYear](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfYear)
|
[[play](https://go.dev/play/p/nT1heB1KUUK)]
|
||||||
- [EndOfMinute](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfMinute)
|
- **<big>BeginOfMinute</big>** : return the date time at the begin of minute of specific date.
|
||||||
- [EndOfHour](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfHour)
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfMinute)]
|
||||||
- [EndOfDay](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfDay)
|
[[play](https://go.dev/play/p/ieOLVJ9CiFT)]
|
||||||
- [EndOfWeek](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfWeek)
|
- **<big>BeginOfHour</big>** : return the date time at the begin of hour of specific date.
|
||||||
- [EndOfMonth](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfMonth)
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfHour)]
|
||||||
- [EndOfYear](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfYear)
|
[[play](https://go.dev/play/p/GhdGFnDWpYs)]
|
||||||
- [GetNowDate](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#GetNowDate)
|
- **<big>BeginOfDay</big>** : return the date time at the begin of day of specific date.
|
||||||
- [GetNowTime](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#GetNowTime)
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfDay)]
|
||||||
- [GetNowDateTime](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#GetNowDateTime)
|
[[play](https://go.dev/play/p/94m_UT6cWs9)]
|
||||||
- [GetZeroHourTimestamp](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#GetZeroHourTimestamp)
|
- **<big>BeginOfWeek</big>** : return the date time at the begin of week of specific date.
|
||||||
- [GetNightTimestamp](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#GetNightTimestamp)
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfWeek)]
|
||||||
- [FormatTimeToStr](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#FormatTimeToStr)
|
[[play](https://go.dev/play/p/ynjoJPz7VNV)]
|
||||||
- [FormatStrToTime](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#FormatStrToTime)
|
- **<big>BeginOfMonth</big>** : return the date time at the begin of month of specific date.
|
||||||
- [NewUnix](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#NewUnix)
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfMonth)]
|
||||||
- [NewUnixNow](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#NewUnixNow)
|
[[play](https://go.dev/play/p/bWXVFsmmzwL)]
|
||||||
- [NewFormat](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#NewFormat)
|
- **<big>BeginOfYear</big>** : return the date time at the begin of year of specific date.
|
||||||
- [NewISO8601](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#NewISO8601)
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfYear)]
|
||||||
- [ToUnix](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#ToUnix)
|
[[play](https://go.dev/play/p/i326DSwLnV8)]
|
||||||
- [ToFormat](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#ToFormat)
|
- **<big>EndOfMinute</big>** : return the date time at the end of minute of specific date.
|
||||||
- [ToFormatForTpl](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#ToFormatForTpl)
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfMinute)]
|
||||||
- [ToIso8601](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#ToIso8601)
|
[[play](https://go.dev/play/p/yrL5wGzPj4z)]
|
||||||
|
- **<big>EndOfHour</big>** : return the date time at the end of hour of specific date.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfHour)]
|
||||||
|
[[play](https://go.dev/play/p/6ce3j_6cVqN)]
|
||||||
|
- **<big>EndOfDay</big>** : return the date time at the end of day of specific date.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfDay)]
|
||||||
|
[[play](https://go.dev/play/p/eMBOvmq5Ih1)]
|
||||||
|
- **<big>EndOfWeek</big>** : return the date time at the end of week of specific date.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfWeek)]
|
||||||
|
[[play](https://go.dev/play/p/i08qKXD9flf)]
|
||||||
|
- **<big>EndOfMonth</big>** : return the date time at the end of month of specific date.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfMonth)]
|
||||||
|
[[play](https://go.dev/play/p/_GWh10B3Nqi)]
|
||||||
|
- **<big>EndOfYear</big>** : return the date time at the end of year of specific date.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfYear)]
|
||||||
|
[[play](https://go.dev/play/p/G01cKlMCvNm)]
|
||||||
|
- **<big>GetNowDate</big>** : return format yyyy-mm-dd of current date.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#GetNowDate)]
|
||||||
|
[[play](https://go.dev/play/p/PvfkPpcpBBf)]
|
||||||
|
- **<big>GetNowTime</big>** : return format hh-mm-ss of current time.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#GetNowTime)]
|
||||||
|
[[play](https://go.dev/play/p/l7BNxCkTmJS)]
|
||||||
|
- **<big>GetNowDateTime</big>** : return format yyyy-mm-dd hh-mm-ss of current datetime.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#GetNowDateTime)]
|
||||||
|
[[play](https://go.dev/play/p/pI4AqngD0al)]
|
||||||
|
- **<big>GetZeroHourTimestamp</big>** : return timestamp of zero hour (timestamp of 00:00).
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#GetZeroHourTimestamp)]
|
||||||
|
[[play](https://go.dev/play/p/QmL2oIaGE3q)]
|
||||||
|
- **<big>GetNightTimestamp</big>** : return timestamp of zero hour (timestamp of 23:59).
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#GetNightTimestamp)]
|
||||||
|
[[play](https://go.dev/play/p/UolysR3MYP1)]
|
||||||
|
- **<big>FormatTimeToStr</big>** : convert time to string.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#FormatTimeToStr)]
|
||||||
|
[[play](https://go.dev/play/p/_Ia7M8H_OvE)]
|
||||||
|
- **<big>FormatStrToTime</big>** : convert string to time.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#FormatStrToTime)]
|
||||||
|
[[play](https://go.dev/play/p/1h9FwdU8ql4)]
|
||||||
|
- **<big>NewUnix</big>** : return unix timestamp of specific time.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#NewUnix)]
|
||||||
|
[[play](https://go.dev/play/p/psoSuh_kLRt)]
|
||||||
|
- **<big>NewUnixNow</big>** : return unix timestamp of current time.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#NewUnixNow)]
|
||||||
|
[[play](https://go.dev/play/p/U4PPx-9D0oz)]
|
||||||
|
- **<big>NewFormat</big>** : return unix timestamp of specific time string, t should be "yyyy-mm-dd hh:mm:ss".
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#NewFormat)]
|
||||||
|
[[play](https://go.dev/play/p/VkW08ZOaXPZ)]
|
||||||
|
- **<big>NewISO8601</big>** : return unix timestamp of specific iso8601 time string.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#NewISO8601)]
|
||||||
|
[[play](https://go.dev/play/p/mkhOHQkdeA2)]
|
||||||
|
- **<big>ToUnix</big>** : return unix timestamp.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#ToUnix)]
|
||||||
|
[[play](https://go.dev/play/p/_LUiwAdocjy)]
|
||||||
|
- **<big>ToFormat</big>** : return the time string 'yyyy-mm-dd hh:mm:ss' of unix time.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#ToFormat)]
|
||||||
|
[[play](https://go.dev/play/p/VkW08ZOaXPZ)]
|
||||||
|
- **<big>ToFormatForTpl</big>** : return the time string which format is specific tpl.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#ToFormatForTpl)]
|
||||||
|
[[play](https://go.dev/play/p/nyXxXcQJ8L5)]
|
||||||
|
- **<big>ToIso8601</big>** : return iso8601 time string.
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#ToIso8601)]
|
||||||
|
[[play](https://go.dev/play/p/mkhOHQkdeA2)]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### 7. Datastructure package constains some common data structure. eg. list, linklist, stack, queue, set, tree, graph.
|
### 7. Datastructure package constains some common data structure. eg. list, linklist, stack, queue, set, tree, graph.
|
||||||
|
|
||||||
@@ -666,7 +729,7 @@ import "github.com/duke-git/lancet/v2/random"
|
|||||||
- **<big>RandInt</big>** : generate random int number between min and max.
|
- **<big>RandInt</big>** : generate random int number between min and max.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandInt)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandInt)]
|
||||||
[[play](https://go.dev/play/p/pXyyAAI5YxD)]
|
[[play](https://go.dev/play/p/pXyyAAI5YxD)]
|
||||||
- **<big>RandString</big>** : generate random string of specified length.
|
- **<big>RandString</big>** : generate random string of specific length.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandString)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandString)]
|
||||||
[[play](https://go.dev/play/p/W2xvRUXA7Mi)]
|
[[play](https://go.dev/play/p/W2xvRUXA7Mi)]
|
||||||
- **<big>RandUpper</big>** : generate a random upper case string.
|
- **<big>RandUpper</big>** : generate a random upper case string.
|
||||||
@@ -675,7 +738,7 @@ import "github.com/duke-git/lancet/v2/random"
|
|||||||
- **<big>RandLower</big>** : generate a random lower case string.
|
- **<big>RandLower</big>** : generate a random lower case string.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandLower)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandLower)]
|
||||||
[[play](https://go.dev/play/p/XJtZ471cmtI)]
|
[[play](https://go.dev/play/p/XJtZ471cmtI)]
|
||||||
- **<big>RandNumeral</big>** : generate a random numeral string of specified length.
|
- **<big>RandNumeral</big>** : generate a random numeral string of specific length.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandNumeral)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandNumeral)]
|
||||||
[[play](https://go.dev/play/p/g4JWVpHsJcf)]
|
[[play](https://go.dev/play/p/g4JWVpHsJcf)]
|
||||||
- **<big>RandNumeralOrLetter</big>** : generate a random numeral or letter string.
|
- **<big>RandNumeralOrLetter</big>** : generate a random numeral or letter string.
|
||||||
@@ -885,15 +948,15 @@ import "github.com/duke-git/lancet/v2/strutil"
|
|||||||
|
|
||||||
#### Function list:
|
#### Function list:
|
||||||
|
|
||||||
- **<big>After</big>** : returns the substring after the first occurrence of a specified string in the source string.
|
- **<big>After</big>** : returns the substring after the first occurrence of a specific string in the source string.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#After)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#After)]
|
||||||
[[play](https://go.dev/play/p/RbCOQqCDA7m)]
|
[[play](https://go.dev/play/p/RbCOQqCDA7m)]
|
||||||
- **<big>AfterLast</big>** : returns the substring after the last occurrence of a specified string in the source string. [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#AfterLast)]
|
- **<big>AfterLast</big>** : returns the substring after the last occurrence of a specific string in the source string. [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#AfterLast)]
|
||||||
[[play](https://go.dev/play/p/1TegARrb8Yn)]
|
[[play](https://go.dev/play/p/1TegARrb8Yn)]
|
||||||
- **<big>Before</big>** : returns the substring before the first occurrence of a specified string in the source string.
|
- **<big>Before</big>** : returns the substring before the first occurrence of a specific string in the source string.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Before)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Before)]
|
||||||
[[play](https://go.dev/play/p/JAWTZDS4F5w)]
|
[[play](https://go.dev/play/p/JAWTZDS4F5w)]
|
||||||
- **<big>BeforeLast</big>** : returns the substring before the last occurrence of a specified string in the source string.
|
- **<big>BeforeLast</big>** : returns the substring before the last occurrence of a specific string in the source string.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#BeforeLast)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#BeforeLast)]
|
||||||
[[play](https://go.dev/play/p/pJfXXAoG_Te)]
|
[[play](https://go.dev/play/p/pJfXXAoG_Te)]
|
||||||
- **<big>CamelCase</big>** : coverts source string to its camelCase string.
|
- **<big>CamelCase</big>** : coverts source string to its camelCase string.
|
||||||
@@ -935,7 +998,7 @@ import "github.com/duke-git/lancet/v2/strutil"
|
|||||||
- **<big>SplitEx</big>** : split a given string which can control the result slice contains empty string or not.
|
- **<big>SplitEx</big>** : split a given string which can control the result slice contains empty string or not.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#SplitEx)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#SplitEx)]
|
||||||
[[play](https://go.dev/play/p/Us-ySSbWh-3)]
|
[[play](https://go.dev/play/p/Us-ySSbWh-3)]
|
||||||
- **<big>Substring</big>** : returns a substring of the specified length starting at the specified offset position.
|
- **<big>Substring</big>** : returns a substring of the specific length starting at the specific offset position.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Substring)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Substring)]
|
||||||
- **<big>Wrap</big>** : wrap a string with given string.
|
- **<big>Wrap</big>** : wrap a string with given string.
|
||||||
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Wrap)]
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Wrap)]
|
||||||
|
|||||||
122
README_zh-CN.md
122
README_zh-CN.md
@@ -349,36 +349,98 @@ import "github.com/duke-git/lancet/v2/datetime"
|
|||||||
|
|
||||||
#### 函数列表:
|
#### 函数列表:
|
||||||
|
|
||||||
- [AddDay](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#AddDay)
|
- **<big>AddDay</big>** : 将日期加/减天数。
|
||||||
- [AddHour](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#AddHour)
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#AddDay)]
|
||||||
- [AddMinute](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#AddMinute)
|
[[play](https://go.dev/play/p/dIGbs_uTdFa)]
|
||||||
- [BeginOfMinute](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfMinute)
|
- **<big>AddHour</big>** : 将日期加/减小时数。
|
||||||
- [BeginOfHour](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfHour)
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#AddHour)]
|
||||||
- [BeginOfDay](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfDay)
|
[[play](https://go.dev/play/p/rcMjd7OCsi5)]
|
||||||
- [BeginOfWeek](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfWeek)
|
- **<big>AddMinute</big>** : 将日期加/减分钟数。
|
||||||
- [BeginOfMonth](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfMonth)
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#AddMinute)]
|
||||||
- [BeginOfYear](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfYear)
|
[[play](https://go.dev/play/p/nT1heB1KUUK)]
|
||||||
- [EndOfMinute](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfMinute)
|
- **<big>BeginOfMinute</big>** : 返回指定时间的分钟开始时间。
|
||||||
- [EndOfHour](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfHour)
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#BeginOfMinute)]
|
||||||
- [EndOfDay](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfDay)
|
[[play](https://go.dev/play/p/ieOLVJ9CiFT)]
|
||||||
- [EndOfWeek](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfWeek)
|
- **<big>BeginOfHour</big>** : 返回指定时间的小时开始时间。
|
||||||
- [EndOfMonth](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfMonth)
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#BeginOfHour)]
|
||||||
- [EndOfYear](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfYear)
|
[[play](https://go.dev/play/p/GhdGFnDWpYs)]
|
||||||
- [GetNowDate](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#GetNowDate)
|
- **<big>BeginOfDay</big>** : 返回指定时间的当天开始时间。
|
||||||
- [GetNowTime](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#GetNowTime)
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#BeginOfDay)]
|
||||||
- [GetNowDateTime](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#GetNowDateTime)
|
[[play](https://go.dev/play/p/94m_UT6cWs9)]
|
||||||
- [GetZeroHourTimestamp](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#GetZeroHourTimestamp)
|
- **<big>BeginOfWeek</big>** : 返回指定时间的每周开始时间,默认开始时间星期日。
|
||||||
- [GetNightTimestamp](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#GetNightTimestamp)
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#BeginOfWeek)]
|
||||||
- [FormatTimeToStr](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#FormatTimeToStr)
|
[[play](https://go.dev/play/p/ynjoJPz7VNV)]
|
||||||
- [FormatStrToTime](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#FormatStrToTime)
|
- **<big>BeginOfMonth</big>** : 返回指定时间的当月开始时间。
|
||||||
- [NewUnix](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#NewUnix)
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#BeginOfMonth)]
|
||||||
- [NewUnixNow](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#NewUnixNow)
|
[[play](https://go.dev/play/p/bWXVFsmmzwL)]
|
||||||
- [NewFormat](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#NewFormat)
|
- **<big>BeginOfYear</big>** : 返回指定时间的当年开始时间。
|
||||||
- [NewISO8601](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#NewISO8601)
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#BeginOfYear)]
|
||||||
- [ToUnix](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToUnix)
|
[[play](https://go.dev/play/p/i326DSwLnV8)]
|
||||||
- [ToFormat](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToFormat)
|
- **<big>EndOfMinute</big>** : 返回指定时间的分钟结束时间。
|
||||||
- [ToFormatForTpl](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToFormatForTpl)
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#EndOfMinute)]
|
||||||
- [ToIso8601](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToIso8601)
|
[[play](https://go.dev/play/p/yrL5wGzPj4z)]
|
||||||
|
- **<big>EndOfHour</big>** : 返回指定时间的小时结束时间。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#EndOfHour)]
|
||||||
|
[[play](https://go.dev/play/p/6ce3j_6cVqN)]
|
||||||
|
- **<big>EndOfDay</big>** : 返回指定时间的当天结束时间。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#EndOfDay)]
|
||||||
|
[[play](https://go.dev/play/p/eMBOvmq5Ih1)]
|
||||||
|
- **<big>EndOfWeek</big>** : 返回指定时间的星期结束时间,默认结束时间星期六。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#EndOfWeek)]
|
||||||
|
[[play](https://go.dev/play/p/i08qKXD9flf)]
|
||||||
|
- **<big>EndOfMonth</big>** : 返回指定时间的月份结束时间。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#EndOfMonth)]
|
||||||
|
[[play](https://go.dev/play/p/_GWh10B3Nqi)]
|
||||||
|
- **<big>EndOfYear</big>** : 返回指定时间的年份结束时间。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#EndOfYear)]
|
||||||
|
[[play](https://go.dev/play/p/G01cKlMCvNm)]
|
||||||
|
- **<big>GetNowDate</big>** : 获取当天日期,返回格式:yyyy-mm-dd。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#GetNowDate)]
|
||||||
|
[[play](https://go.dev/play/p/PvfkPpcpBBf)]
|
||||||
|
- **<big>GetNowTime</big>** : 获取当时时间,返回格式:hh:mm:ss。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#GetNowTime)]
|
||||||
|
[[play](https://go.dev/play/p/l7BNxCkTmJS)]
|
||||||
|
- **<big>GetNowDateTime</big>** : 获取当时日期和时间,返回格式:yyyy-mm-dd hh:mm:ss。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#GetNowDateTime)]
|
||||||
|
[[play](https://go.dev/play/p/pI4AqngD0al)]
|
||||||
|
- **<big>GetZeroHourTimestamp</big>** : 获取零时时间戳(timestamp of 00:00)。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#GetZeroHourTimestamp)]
|
||||||
|
[[play](https://go.dev/play/p/QmL2oIaGE3q)]
|
||||||
|
- **<big>GetNightTimestamp</big>** : 获取午夜时间戳(timestamp of 23:59)。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#GetNightTimestamp)]
|
||||||
|
[[play](https://go.dev/play/p/UolysR3MYP1)]
|
||||||
|
- **<big>FormatTimeToStr</big>** : 将日期格式化成字符串。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#FormatTimeToStr)]
|
||||||
|
[[play](https://go.dev/play/p/_Ia7M8H_OvE)]
|
||||||
|
- **<big>FormatStrToTime</big>** : 将字符串格式化成时间。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#FormatStrToTime)]
|
||||||
|
[[play](https://go.dev/play/p/1h9FwdU8ql4)]
|
||||||
|
- **<big>NewUnix</big>** : 创建一个unix时间戳。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#NewUnix)]
|
||||||
|
[[play](https://go.dev/play/p/psoSuh_kLRt)]
|
||||||
|
- **<big>NewUnixNow</big>** : 创建一个当前时间的unix时间戳。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#NewUnixNow)]
|
||||||
|
[[play](https://go.dev/play/p/U4PPx-9D0oz)]
|
||||||
|
- **<big>NewFormat</big>** : 创建一个yyyy-mm-dd hh:mm:ss格式时间字符串的unix时间戳。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#NewFormat)]
|
||||||
|
[[play](https://go.dev/play/p/VkW08ZOaXPZ)]
|
||||||
|
- **<big>NewISO8601</big>** : 创建一个iso8601格式时间字符串的unix时间戳。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#NewISO8601)]
|
||||||
|
[[play](https://go.dev/play/p/mkhOHQkdeA2)]
|
||||||
|
- **<big>ToUnix</big>** : 返回unix时间戳。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToUnix)]
|
||||||
|
[[play](https://go.dev/play/p/_LUiwAdocjy)]
|
||||||
|
- **<big>ToFormat</big>** : 返回格式'yyyy-mm-dd hh:mm:ss'的日期字符串。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToFormat)]
|
||||||
|
[[play](https://go.dev/play/p/VkW08ZOaXPZ)]
|
||||||
|
- **<big>ToFormatForTpl</big>** : 返回tpl格式指定的日期字符串。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToFormatForTpl)]
|
||||||
|
[[play](https://go.dev/play/p/nyXxXcQJ8L5)]
|
||||||
|
- **<big>ToIso8601</big>** : 返回iso8601日期字符串。
|
||||||
|
[[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToIso8601)]
|
||||||
|
[[play](https://go.dev/play/p/mkhOHQkdeA2)]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
### 7. datastructure 包含一些普通的数据结构实现。例如:list, linklist, stack, queue, set, tree, graph.
|
### 7. datastructure 包含一些普通的数据结构实现。例如:list, linklist, stack, queue, set, tree, graph.
|
||||||
|
|
||||||
|
|||||||
@@ -9,17 +9,20 @@ type theTime struct {
|
|||||||
unix int64
|
unix int64
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewUnixNow return unix timestamp of current time
|
// NewUnixNow return unix timestamp of current time.
|
||||||
|
// Play: https://go.dev/play/p/U4PPx-9D0oz
|
||||||
func NewUnixNow() *theTime {
|
func NewUnixNow() *theTime {
|
||||||
return &theTime{unix: time.Now().Unix()}
|
return &theTime{unix: time.Now().Unix()}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewUnix return unix timestamp of specified time
|
// NewUnix return unix timestamp of specified time.
|
||||||
|
// Play: https://go.dev/play/p/psoSuh_kLRt
|
||||||
func NewUnix(unix int64) *theTime {
|
func NewUnix(unix int64) *theTime {
|
||||||
return &theTime{unix: unix}
|
return &theTime{unix: unix}
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewFormat return unix timestamp of specified time string, t should be "yyyy-mm-dd hh:mm:ss"
|
// NewFormat return unix timestamp of specified time string, t should be "yyyy-mm-dd hh:mm:ss".
|
||||||
|
// Play: https://go.dev/play/p/VkW08ZOaXPZ
|
||||||
func NewFormat(t string) (*theTime, error) {
|
func NewFormat(t string) (*theTime, error) {
|
||||||
timeLayout := "2006-01-02 15:04:05"
|
timeLayout := "2006-01-02 15:04:05"
|
||||||
loc := time.FixedZone("CST", 8*3600)
|
loc := time.FixedZone("CST", 8*3600)
|
||||||
@@ -30,7 +33,8 @@ func NewFormat(t string) (*theTime, error) {
|
|||||||
return &theTime{unix: tt.Unix()}, nil
|
return &theTime{unix: tt.Unix()}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewISO8601 return unix timestamp of specified iso8601 time string
|
// NewISO8601 return unix timestamp of specified iso8601 time string.
|
||||||
|
// Play: https://go.dev/play/p/mkhOHQkdeA2
|
||||||
func NewISO8601(iso8601 string) (*theTime, error) {
|
func NewISO8601(iso8601 string) (*theTime, error) {
|
||||||
t, err := time.ParseInLocation(time.RFC3339, iso8601, time.UTC)
|
t, err := time.ParseInLocation(time.RFC3339, iso8601, time.UTC)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -39,22 +43,26 @@ func NewISO8601(iso8601 string) (*theTime, error) {
|
|||||||
return &theTime{unix: t.Unix()}, nil
|
return &theTime{unix: t.Unix()}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToUnix return unix timestamp
|
// ToUnix return unix timestamp.
|
||||||
|
// Play: https://go.dev/play/p/_LUiwAdocjy
|
||||||
func (t *theTime) ToUnix() int64 {
|
func (t *theTime) ToUnix() int64 {
|
||||||
return t.unix
|
return t.unix
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToFormat return the time string 'yyyy-mm-dd hh:mm:ss' of unix time
|
// ToFormat return the time string 'yyyy-mm-dd hh:mm:ss' of unix time.
|
||||||
|
// Play: https://go.dev/play/p/VkW08ZOaXPZ
|
||||||
func (t *theTime) ToFormat() string {
|
func (t *theTime) ToFormat() string {
|
||||||
return time.Unix(t.unix, 0).Format("2006-01-02 15:04:05")
|
return time.Unix(t.unix, 0).Format("2006-01-02 15:04:05")
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToFormatForTpl return the time string which format is specified tpl
|
// ToFormatForTpl return the time string which format is specified tpl.
|
||||||
|
// Play: https://go.dev/play/p/nyXxXcQJ8L5
|
||||||
func (t *theTime) ToFormatForTpl(tpl string) string {
|
func (t *theTime) ToFormatForTpl(tpl string) string {
|
||||||
return time.Unix(t.unix, 0).Format(tpl)
|
return time.Unix(t.unix, 0).Format(tpl)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToFormatForTpl return iso8601 time string
|
// ToFormatForTpl return iso8601 time string.
|
||||||
|
// Play: https://go.dev/play/p/mkhOHQkdeA2
|
||||||
func (t *theTime) ToIso8601() string {
|
func (t *theTime) ToIso8601() string {
|
||||||
return time.Unix(t.unix, 0).Format(time.RFC3339)
|
return time.Unix(t.unix, 0).Format(time.RFC3339)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,9 +19,6 @@ func TestToUnix(t *testing.T) {
|
|||||||
func TestToFormat(t *testing.T) {
|
func TestToFormat(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestToFormat")
|
assert := internal.NewAssert(t, "TestToFormat")
|
||||||
|
|
||||||
_, err := NewFormat("2022/03/18 17:04:05")
|
|
||||||
assert.IsNotNil(err)
|
|
||||||
|
|
||||||
tm, err := NewFormat("2022-03-18 17:04:05")
|
tm, err := NewFormat("2022-03-18 17:04:05")
|
||||||
assert.IsNil(err)
|
assert.IsNil(err)
|
||||||
|
|
||||||
|
|||||||
@@ -54,54 +54,64 @@ func init() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddMinute add or sub minute to the time
|
// AddMinute add or sub minute to the time.
|
||||||
|
// Play: https://go.dev/play/p/nT1heB1KUUK
|
||||||
func AddMinute(t time.Time, minute int64) time.Time {
|
func AddMinute(t time.Time, minute int64) time.Time {
|
||||||
return t.Add(time.Minute * time.Duration(minute))
|
return t.Add(time.Minute * time.Duration(minute))
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddHour add or sub hour to the time
|
// AddHour add or sub hour to the time.
|
||||||
|
// Play: https://go.dev/play/p/rcMjd7OCsi5
|
||||||
func AddHour(t time.Time, hour int64) time.Time {
|
func AddHour(t time.Time, hour int64) time.Time {
|
||||||
return t.Add(time.Hour * time.Duration(hour))
|
return t.Add(time.Hour * time.Duration(hour))
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddDay add or sub day to the time
|
// AddDay add or sub day to the time.
|
||||||
|
// Play: https://go.dev/play/p/dIGbs_uTdFa
|
||||||
func AddDay(t time.Time, day int64) time.Time {
|
func AddDay(t time.Time, day int64) time.Time {
|
||||||
return t.Add(24 * time.Hour * time.Duration(day))
|
return t.Add(24 * time.Hour * time.Duration(day))
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetNowDate return format yyyy-mm-dd of current date
|
// GetNowDate return format yyyy-mm-dd of current date.
|
||||||
|
// Play: https://go.dev/play/p/PvfkPpcpBBf
|
||||||
func GetNowDate() string {
|
func GetNowDate() string {
|
||||||
return time.Now().Format("2006-01-02")
|
return time.Now().Format("2006-01-02")
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetNowTime return format hh-mm-ss of current time
|
// GetNowTime return format hh-mm-ss of current time.
|
||||||
|
// Play: https://go.dev/play/p/l7BNxCkTmJS
|
||||||
func GetNowTime() string {
|
func GetNowTime() string {
|
||||||
return time.Now().Format("15:04:05")
|
return time.Now().Format("15:04:05")
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetNowDateTime return format yyyy-mm-dd hh-mm-ss of current datetime
|
// GetNowDateTime return format yyyy-mm-dd hh-mm-ss of current datetime.
|
||||||
|
// Play: https://go.dev/play/p/pI4AqngD0al
|
||||||
func GetNowDateTime() string {
|
func GetNowDateTime() string {
|
||||||
return time.Now().Format("2006-01-02 15:04:05")
|
return time.Now().Format("2006-01-02 15:04:05")
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetZeroHourTimestamp return timestamp of zero hour (timestamp of 00:00)
|
// GetZeroHourTimestamp return timestamp of zero hour (timestamp of 00:00).
|
||||||
|
// Play: https://go.dev/play/p/QmL2oIaGE3q
|
||||||
func GetZeroHourTimestamp() int64 {
|
func GetZeroHourTimestamp() int64 {
|
||||||
ts := time.Now().Format("2006-01-02")
|
ts := time.Now().Format("2006-01-02")
|
||||||
t, _ := time.Parse("2006-01-02", ts)
|
t, _ := time.Parse("2006-01-02", ts)
|
||||||
return t.UTC().Unix() - 8*3600
|
return t.UTC().Unix() - 8*3600
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetNightTimestamp return timestamp of zero hour (timestamp of 23:59)
|
// GetNightTimestamp return timestamp of zero hour (timestamp of 23:59).
|
||||||
|
// Play: https://go.dev/play/p/UolysR3MYP1
|
||||||
func GetNightTimestamp() int64 {
|
func GetNightTimestamp() int64 {
|
||||||
return GetZeroHourTimestamp() + 86400 - 1
|
return GetZeroHourTimestamp() + 86400 - 1
|
||||||
}
|
}
|
||||||
|
|
||||||
// FormatTimeToStr convert time to string
|
// FormatTimeToStr convert time to string.
|
||||||
|
// Play: https://go.dev/play/p/_Ia7M8H_OvE
|
||||||
func FormatTimeToStr(t time.Time, format string) string {
|
func FormatTimeToStr(t time.Time, format string) string {
|
||||||
return t.Format(timeFormat[format])
|
return t.Format(timeFormat[format])
|
||||||
}
|
}
|
||||||
|
|
||||||
// FormatStrToTime convert string to time
|
// FormatStrToTime convert string to time.
|
||||||
|
// Play: https://go.dev/play/p/1h9FwdU8ql4
|
||||||
func FormatStrToTime(str, format string) (time.Time, error) {
|
func FormatStrToTime(str, format string) (time.Time, error) {
|
||||||
v, ok := timeFormat[format]
|
v, ok := timeFormat[format]
|
||||||
if !ok {
|
if !ok {
|
||||||
@@ -111,43 +121,50 @@ func FormatStrToTime(str, format string) (time.Time, error) {
|
|||||||
return time.Parse(v, str)
|
return time.Parse(v, str)
|
||||||
}
|
}
|
||||||
|
|
||||||
// BeginOfMinute return beginning minute time of day
|
// BeginOfMinute return beginning minute time of day.
|
||||||
|
// Play: https://go.dev/play/p/ieOLVJ9CiFT
|
||||||
func BeginOfMinute(t time.Time) time.Time {
|
func BeginOfMinute(t time.Time) time.Time {
|
||||||
y, m, d := t.Date()
|
y, m, d := t.Date()
|
||||||
return time.Date(y, m, d, t.Hour(), t.Minute(), 0, 0, t.Location())
|
return time.Date(y, m, d, t.Hour(), t.Minute(), 0, 0, t.Location())
|
||||||
}
|
}
|
||||||
|
|
||||||
// EndOfMinute return end minute time of day
|
// EndOfMinute return end minute time of day.
|
||||||
|
// Play: https://go.dev/play/p/yrL5wGzPj4z
|
||||||
func EndOfMinute(t time.Time) time.Time {
|
func EndOfMinute(t time.Time) time.Time {
|
||||||
y, m, d := t.Date()
|
y, m, d := t.Date()
|
||||||
return time.Date(y, m, d, t.Hour(), t.Minute(), 59, int(time.Second-time.Nanosecond), t.Location())
|
return time.Date(y, m, d, t.Hour(), t.Minute(), 59, int(time.Second-time.Nanosecond), t.Location())
|
||||||
}
|
}
|
||||||
|
|
||||||
// BeginOfHour return beginning hour time of day
|
// BeginOfHour return beginning hour time of day.
|
||||||
|
// Play: https://go.dev/play/p/GhdGFnDWpYs
|
||||||
func BeginOfHour(t time.Time) time.Time {
|
func BeginOfHour(t time.Time) time.Time {
|
||||||
y, m, d := t.Date()
|
y, m, d := t.Date()
|
||||||
return time.Date(y, m, d, t.Hour(), 0, 0, 0, t.Location())
|
return time.Date(y, m, d, t.Hour(), 0, 0, 0, t.Location())
|
||||||
}
|
}
|
||||||
|
|
||||||
// EndOfHour return end hour time of day
|
// EndOfHour return end hour time of day.
|
||||||
|
// Play: https://go.dev/play/p/6ce3j_6cVqN
|
||||||
func EndOfHour(t time.Time) time.Time {
|
func EndOfHour(t time.Time) time.Time {
|
||||||
y, m, d := t.Date()
|
y, m, d := t.Date()
|
||||||
return time.Date(y, m, d, t.Hour(), 59, 59, int(time.Second-time.Nanosecond), t.Location())
|
return time.Date(y, m, d, t.Hour(), 59, 59, int(time.Second-time.Nanosecond), t.Location())
|
||||||
}
|
}
|
||||||
|
|
||||||
// BeginOfDay return beginning hour time of day
|
// BeginOfDay return beginning hour time of day.
|
||||||
|
// Play: https://go.dev/play/p/94m_UT6cWs9
|
||||||
func BeginOfDay(t time.Time) time.Time {
|
func BeginOfDay(t time.Time) time.Time {
|
||||||
y, m, d := t.Date()
|
y, m, d := t.Date()
|
||||||
return time.Date(y, m, d, 0, 0, 0, 0, t.Location())
|
return time.Date(y, m, d, 0, 0, 0, 0, t.Location())
|
||||||
}
|
}
|
||||||
|
|
||||||
// EndOfDay return end time of day
|
// EndOfDay return end time of day.
|
||||||
|
// Play: https://go.dev/play/p/eMBOvmq5Ih1
|
||||||
func EndOfDay(t time.Time) time.Time {
|
func EndOfDay(t time.Time) time.Time {
|
||||||
y, m, d := t.Date()
|
y, m, d := t.Date()
|
||||||
return time.Date(y, m, d, 23, 59, 59, int(time.Second-time.Nanosecond), t.Location())
|
return time.Date(y, m, d, 23, 59, 59, int(time.Second-time.Nanosecond), t.Location())
|
||||||
}
|
}
|
||||||
|
|
||||||
// BeginOfWeek return beginning week, default week begin from Sunday
|
// BeginOfWeek return beginning week, default week begin from Sunday.
|
||||||
|
// Play: https://go.dev/play/p/ynjoJPz7VNV
|
||||||
func BeginOfWeek(t time.Time, beginFrom ...time.Weekday) time.Time {
|
func BeginOfWeek(t time.Time, beginFrom ...time.Weekday) time.Time {
|
||||||
var beginFromWeekday = time.Sunday
|
var beginFromWeekday = time.Sunday
|
||||||
if len(beginFrom) > 0 {
|
if len(beginFrom) > 0 {
|
||||||
@@ -161,7 +178,8 @@ func BeginOfWeek(t time.Time, beginFrom ...time.Weekday) time.Time {
|
|||||||
return beginOfWeek
|
return beginOfWeek
|
||||||
}
|
}
|
||||||
|
|
||||||
// EndOfWeek return end week time, default week end with Saturday
|
// EndOfWeek return end week time, default week end with Saturday.
|
||||||
|
// Play: https://go.dev/play/p/i08qKXD9flf
|
||||||
func EndOfWeek(t time.Time, endWith ...time.Weekday) time.Time {
|
func EndOfWeek(t time.Time, endWith ...time.Weekday) time.Time {
|
||||||
var endWithWeekday = time.Saturday
|
var endWithWeekday = time.Saturday
|
||||||
if len(endWith) > 0 {
|
if len(endWith) > 0 {
|
||||||
@@ -175,24 +193,28 @@ func EndOfWeek(t time.Time, endWith ...time.Weekday) time.Time {
|
|||||||
return endWithWeek
|
return endWithWeek
|
||||||
}
|
}
|
||||||
|
|
||||||
// BeginOfMonth return beginning of month
|
// BeginOfMonth return beginning of month.
|
||||||
|
// Play: https://go.dev/play/p/bWXVFsmmzwL
|
||||||
func BeginOfMonth(t time.Time) time.Time {
|
func BeginOfMonth(t time.Time) time.Time {
|
||||||
y, m, _ := t.Date()
|
y, m, _ := t.Date()
|
||||||
return time.Date(y, m, 1, 0, 0, 0, 0, t.Location())
|
return time.Date(y, m, 1, 0, 0, 0, 0, t.Location())
|
||||||
}
|
}
|
||||||
|
|
||||||
// EndOfMonth return end of month
|
// EndOfMonth return end of month.
|
||||||
|
// Play: https://go.dev/play/p/_GWh10B3Nqi
|
||||||
func EndOfMonth(t time.Time) time.Time {
|
func EndOfMonth(t time.Time) time.Time {
|
||||||
return BeginOfMonth(t).AddDate(0, 1, 0).Add(-time.Nanosecond)
|
return BeginOfMonth(t).AddDate(0, 1, 0).Add(-time.Nanosecond)
|
||||||
}
|
}
|
||||||
|
|
||||||
// BeginOfYear return beginning of year
|
// BeginOfYear return the date time at the begin of year.
|
||||||
|
// Play: https://go.dev/play/p/i326DSwLnV8
|
||||||
func BeginOfYear(t time.Time) time.Time {
|
func BeginOfYear(t time.Time) time.Time {
|
||||||
y, _, _ := t.Date()
|
y, _, _ := t.Date()
|
||||||
return time.Date(y, time.January, 1, 0, 0, 0, 0, t.Location())
|
return time.Date(y, time.January, 1, 0, 0, 0, 0, t.Location())
|
||||||
}
|
}
|
||||||
|
|
||||||
// EndOfYear return end of year
|
// EndOfYear return the date time at the end of year.
|
||||||
|
// Play: https://go.dev/play/p/G01cKlMCvNm
|
||||||
func EndOfYear(t time.Time) time.Time {
|
func EndOfYear(t time.Time) time.Time {
|
||||||
return BeginOfYear(t).AddDate(1, 0, 0).Add(-time.Nanosecond)
|
return BeginOfYear(t).AddDate(1, 0, 0).Add(-time.Nanosecond)
|
||||||
}
|
}
|
||||||
|
|||||||
323
datetime/datetime_example_test.go
Normal file
323
datetime/datetime_example_test.go
Normal file
@@ -0,0 +1,323 @@
|
|||||||
|
package datetime
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"reflect"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func ExampleAddDay() {
|
||||||
|
now := time.Now()
|
||||||
|
|
||||||
|
tomorrow := AddDay(now, 1)
|
||||||
|
diff1 := tomorrow.Sub(now)
|
||||||
|
|
||||||
|
yesterday := AddDay(now, -1)
|
||||||
|
diff2 := yesterday.Sub(now)
|
||||||
|
|
||||||
|
fmt.Println(diff1)
|
||||||
|
fmt.Println(diff2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 24h0m0s
|
||||||
|
// -24h0m0s
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleAddHour() {
|
||||||
|
now := time.Now()
|
||||||
|
|
||||||
|
after2Hours := AddHour(now, 2)
|
||||||
|
diff1 := after2Hours.Sub(now)
|
||||||
|
|
||||||
|
before2Hours := AddHour(now, -2)
|
||||||
|
diff2 := before2Hours.Sub(now)
|
||||||
|
|
||||||
|
fmt.Println(diff1)
|
||||||
|
fmt.Println(diff2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 2h0m0s
|
||||||
|
// -2h0m0s
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleAddMinute() {
|
||||||
|
now := time.Now()
|
||||||
|
|
||||||
|
after2Minutes := AddMinute(now, 2)
|
||||||
|
diff1 := after2Minutes.Sub(now)
|
||||||
|
|
||||||
|
before2Minutes := AddMinute(now, -2)
|
||||||
|
diff2 := before2Minutes.Sub(now)
|
||||||
|
|
||||||
|
fmt.Println(diff1)
|
||||||
|
fmt.Println(diff2)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 2m0s
|
||||||
|
// -2m0s
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleGetNowDate() {
|
||||||
|
result := GetNowDate()
|
||||||
|
|
||||||
|
expected := time.Now().Format("2006-01-02")
|
||||||
|
|
||||||
|
fmt.Println(result == expected)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleGetNowTime() {
|
||||||
|
result := GetNowTime()
|
||||||
|
|
||||||
|
expected := time.Now().Format("15:04:05")
|
||||||
|
|
||||||
|
fmt.Println(result == expected)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleGetNowDateTime() {
|
||||||
|
result := GetNowDateTime()
|
||||||
|
|
||||||
|
expected := time.Now().Format("2006-01-02 15:04:05")
|
||||||
|
|
||||||
|
fmt.Println(result == expected)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
}
|
||||||
|
|
||||||
|
// func ExampleGetZeroHourTimestamp() {
|
||||||
|
// ts := GetZeroHourTimestamp()
|
||||||
|
|
||||||
|
// fmt.Println(ts)
|
||||||
|
|
||||||
|
// // Output:
|
||||||
|
// // 1673107200
|
||||||
|
// }
|
||||||
|
|
||||||
|
// func ExampleGetNightTimestamp() {
|
||||||
|
// ts := GetNightTimestamp()
|
||||||
|
|
||||||
|
// fmt.Println(ts)
|
||||||
|
|
||||||
|
// // Output:
|
||||||
|
// // 1673193599
|
||||||
|
// }
|
||||||
|
|
||||||
|
func ExampleFormatTimeToStr() {
|
||||||
|
datetime, _ := time.Parse("2006-01-02 15:04:05", "2021-01-02 16:04:08")
|
||||||
|
|
||||||
|
result1 := FormatTimeToStr(datetime, "yyyy-mm-dd hh:mm:ss")
|
||||||
|
result2 := FormatTimeToStr(datetime, "yyyy-mm-dd")
|
||||||
|
result3 := FormatTimeToStr(datetime, "dd-mm-yy hh:mm:ss")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 2021-01-02 16:04:08
|
||||||
|
// 2021-01-02
|
||||||
|
// 02-01-21 16:04:08
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleFormatStrToTime() {
|
||||||
|
result1, _ := FormatStrToTime("2021-01-02 16:04:08", "yyyy-mm-dd hh:mm:ss")
|
||||||
|
result2, _ := FormatStrToTime("2021-01-02", "yyyy-mm-dd")
|
||||||
|
result3, _ := FormatStrToTime("02-01-21 16:04:08", "dd-mm-yy hh:mm:ss")
|
||||||
|
|
||||||
|
fmt.Println(result1)
|
||||||
|
fmt.Println(result2)
|
||||||
|
fmt.Println(result3)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 2021-01-02 16:04:08 +0000 UTC
|
||||||
|
// 2021-01-02 00:00:00 +0000 UTC
|
||||||
|
// 2021-01-02 16:04:08 +0000 UTC
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleBeginOfMinute() {
|
||||||
|
input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.Local)
|
||||||
|
|
||||||
|
result := BeginOfMinute(input)
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 2023-01-08 18:50:00 +0800 CST
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleEndOfMinute() {
|
||||||
|
input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.Local)
|
||||||
|
|
||||||
|
result := EndOfMinute(input)
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 2023-01-08 18:50:59.999999999 +0800 CST
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleBeginOfHour() {
|
||||||
|
input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.Local)
|
||||||
|
|
||||||
|
result := BeginOfHour(input)
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 2023-01-08 18:00:00 +0800 CST
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleEndOfHour() {
|
||||||
|
input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.Local)
|
||||||
|
|
||||||
|
result := EndOfHour(input)
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 2023-01-08 18:59:59.999999999 +0800 CST
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleBeginOfDay() {
|
||||||
|
input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.Local)
|
||||||
|
|
||||||
|
result := BeginOfDay(input)
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 2023-01-08 00:00:00 +0800 CST
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleEndOfDay() {
|
||||||
|
input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.Local)
|
||||||
|
|
||||||
|
result := EndOfDay(input)
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 2023-01-08 23:59:59.999999999 +0800 CST
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleBeginOfWeek() {
|
||||||
|
input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.Local)
|
||||||
|
|
||||||
|
result := BeginOfWeek(input)
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 2023-01-08 00:00:00 +0800 CST
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleEndOfWeek() {
|
||||||
|
input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.Local)
|
||||||
|
|
||||||
|
result := EndOfWeek(input)
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 2023-01-14 23:59:59.999999999 +0800 CST
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleBeginOfMonth() {
|
||||||
|
input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.Local)
|
||||||
|
|
||||||
|
result := BeginOfMonth(input)
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 2023-01-01 00:00:00 +0800 CST
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleEndOfMonth() {
|
||||||
|
input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.Local)
|
||||||
|
|
||||||
|
result := EndOfMonth(input)
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 2023-01-31 23:59:59.999999999 +0800 CST
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleBeginOfYear() {
|
||||||
|
input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.Local)
|
||||||
|
|
||||||
|
result := BeginOfYear(input)
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 2023-01-01 00:00:00 +0800 CST
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleEndOfYear() {
|
||||||
|
input := time.Date(2023, 1, 8, 18, 50, 10, 100, time.Local)
|
||||||
|
|
||||||
|
result := EndOfYear(input)
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 2023-12-31 23:59:59.999999999 +0800 CST
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleNewUnix() {
|
||||||
|
result := NewUnix(1647597438)
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// &{1647597438}
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleNewUnixNow() {
|
||||||
|
tm1 := NewUnixNow()
|
||||||
|
|
||||||
|
unixTimestamp := tm1.ToUnix()
|
||||||
|
|
||||||
|
tm2 := NewUnix(unixTimestamp)
|
||||||
|
|
||||||
|
fmt.Println(reflect.DeepEqual(tm1, tm2))
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// true
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleNewFormat() {
|
||||||
|
tm, err := NewFormat("2022-03-18 17:04:05")
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
result := tm.ToFormat()
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 2022-03-18 17:04:05
|
||||||
|
}
|
||||||
|
|
||||||
|
func ExampleNewISO8601() {
|
||||||
|
tm, err := NewISO8601("2006-01-02T15:04:05.999Z")
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
result := tm.ToIso8601()
|
||||||
|
|
||||||
|
fmt.Println(result)
|
||||||
|
|
||||||
|
// Output:
|
||||||
|
// 2006-01-02T23:04:05+08:00
|
||||||
|
}
|
||||||
@@ -52,8 +52,8 @@ func TestGetNowDate(t *testing.T) {
|
|||||||
assert.Equal(expected, GetNowDate())
|
assert.Equal(expected, GetNowDate())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetNotTime(t *testing.T) {
|
func TestGetNowTime(t *testing.T) {
|
||||||
assert := internal.NewAssert(t, "TestGetNotTime")
|
assert := internal.NewAssert(t, "TestGetNowTime")
|
||||||
expected := time.Now().Format("15:04:05")
|
expected := time.Now().Format("15:04:05")
|
||||||
assert.Equal(expected, GetNowTime())
|
assert.Equal(expected, GetNowTime())
|
||||||
}
|
}
|
||||||
@@ -81,7 +81,6 @@ func TestFormatTimeToStr(t *testing.T) {
|
|||||||
for i := 0; i < len(cases); i++ {
|
for i := 0; i < len(cases); i++ {
|
||||||
actual := FormatTimeToStr(datetime, cases[i])
|
actual := FormatTimeToStr(datetime, cases[i])
|
||||||
assert.Equal(expected[i], actual)
|
assert.Equal(expected[i], actual)
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -377,7 +377,7 @@ func main() {
|
|||||||
```
|
```
|
||||||
|
|
||||||
### <span id="EndOfDay">EndOfDay</span>
|
### <span id="EndOfDay">EndOfDay</span>
|
||||||
<p>返回指定时间的当天结束时间.</p>
|
<p>返回指定时间的当天结束时间</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
@@ -541,7 +541,7 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
### <span id="GetNowDateTime">GetNowDateTime</span>
|
### <span id="GetNowDateTime">GetNowDateTime</span>
|
||||||
<p>获取当时日期和时间,返回格式:yyyy-mm-dd hh:mm:ss.</p>
|
<p>获取当时日期和时间,返回格式:yyyy-mm-dd hh:mm:ss</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
@@ -568,7 +568,7 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
### <span id="GetZeroHourTimestamp">GetZeroHourTimestamp</span>
|
### <span id="GetZeroHourTimestamp">GetZeroHourTimestamp</span>
|
||||||
<p>获取零时时间戳(timestamp of 00:00).</p>
|
<p>获取零时时间戳(timestamp of 00:00)</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
@@ -595,7 +595,7 @@ func main() {
|
|||||||
|
|
||||||
|
|
||||||
### <span id="GetNightTimestamp">GetNightTimestamp</span>
|
### <span id="GetNightTimestamp">GetNightTimestamp</span>
|
||||||
<p>获取午夜时间戳(timestamp of 23:59).</p>
|
<p>获取午夜时间戳(timestamp of 23:59)</p>
|
||||||
|
|
||||||
<b>函数签名:</b>
|
<b>函数签名:</b>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user