diff --git a/README.md b/README.md index 0ed1bc0..e897eb4 100644 --- a/README.md +++ b/README.md @@ -348,36 +348,99 @@ import "github.com/duke-git/lancet/v2/datetime" #### Function list: -- [AddDay](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#AddDay) -- [AddHour](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#AddHour) -- [AddMinute](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#AddMinute) -- [BeginOfMinute](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfMinute) -- [BeginOfHour](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfHour) -- [BeginOfDay](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfDay) -- [BeginOfWeek](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfWeek) -- [BeginOfMonth](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfMonth) -- [BeginOfYear](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfYear) -- [EndOfMinute](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfMinute) -- [EndOfHour](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfHour) -- [EndOfDay](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfDay) -- [EndOfWeek](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfWeek) -- [EndOfMonth](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfMonth) -- [EndOfYear](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfYear) -- [GetNowDate](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#GetNowDate) -- [GetNowTime](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#GetNowTime) -- [GetNowDateTime](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#GetNowDateTime) -- [GetZeroHourTimestamp](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#GetZeroHourTimestamp) -- [GetNightTimestamp](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#GetNightTimestamp) -- [FormatTimeToStr](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#FormatTimeToStr) -- [FormatStrToTime](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#FormatStrToTime) -- [NewUnix](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#NewUnix) -- [NewUnixNow](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#NewUnixNow) -- [NewFormat](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#NewFormat) -- [NewISO8601](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#NewISO8601) -- [ToUnix](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#ToUnix) -- [ToFormat](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#ToFormat) -- [ToFormatForTpl](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#ToFormatForTpl) -- [ToIso8601](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#ToIso8601) +- **AddDay** : add or sub day to the time. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#AddDay)] + [[play](https://go.dev/play/p/dIGbs_uTdFa)] +- **AddHour** : add or sub day to the time. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#AddHour)] + [[play](https://go.dev/play/p/rcMjd7OCsi5)] +- **AddMinute** : add or sub day to the time. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#AddMinute)] + [[play](https://go.dev/play/p/nT1heB1KUUK)] +- **BeginOfMinute** : return the date time at the begin of minute of specific date. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfMinute)] + [[play](https://go.dev/play/p/ieOLVJ9CiFT)] +- **BeginOfHour** : return the date time at the begin of hour of specific date. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfHour)] + [[play](https://go.dev/play/p/GhdGFnDWpYs)] +- **BeginOfDay** : return the date time at the begin of day of specific date. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfDay)] + [[play](https://go.dev/play/p/94m_UT6cWs9)] +- **BeginOfWeek** : return the date time at the begin of week of specific date. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfWeek)] + [[play](https://go.dev/play/p/ynjoJPz7VNV)] +- **BeginOfMonth** : return the date time at the begin of month of specific date. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfMonth)] + [[play](https://go.dev/play/p/bWXVFsmmzwL)] +- **BeginOfYear** : return the date time at the begin of year of specific date. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfYear)] + [[play](https://go.dev/play/p/i326DSwLnV8)] +- **EndOfMinute** : return the date time at the end of minute of specific date. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfMinute)] + [[play](https://go.dev/play/p/yrL5wGzPj4z)] +- **EndOfHour** : 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)] +- **EndOfDay** : 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)] +- **EndOfWeek** : 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)] +- **EndOfMonth** : 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)] +- **EndOfYear** : 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)] +- **GetNowDate** : 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)] +- **GetNowTime** : 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)] +- **GetNowDateTime** : 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)] +- **GetZeroHourTimestamp** : 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)] +- **GetNightTimestamp** : 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)] +- **FormatTimeToStr** : 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)] +- **FormatStrToTime** : convert string to time. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#FormatStrToTime)] + [[play](https://go.dev/play/p/1h9FwdU8ql4)] +- **NewUnix** : 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)] +- **NewUnixNow** : 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)] +- **NewFormat** : 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)] +- **NewISO8601** : 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)] +- **ToUnix** : return unix timestamp. + [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#ToUnix)] + [[play](https://go.dev/play/p/_LUiwAdocjy)] +- **ToFormat** : 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)] +- **ToFormatForTpl** : 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)] +- **ToIso8601** : 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. @@ -666,7 +729,7 @@ import "github.com/duke-git/lancet/v2/random" - **RandInt** : generate random int number between min and max. [[doc](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandInt)] [[play](https://go.dev/play/p/pXyyAAI5YxD)] -- **RandString** : generate random string of specified length. +- **RandString** : generate random string of specific length. [[doc](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandString)] [[play](https://go.dev/play/p/W2xvRUXA7Mi)] - **RandUpper** : generate a random upper case string. @@ -675,7 +738,7 @@ import "github.com/duke-git/lancet/v2/random" - **RandLower** : generate a random lower case string. [[doc](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandLower)] [[play](https://go.dev/play/p/XJtZ471cmtI)] -- **RandNumeral** : generate a random numeral string of specified length. +- **RandNumeral** : generate a random numeral string of specific length. [[doc](https://github.com/duke-git/lancet/blob/main/docs/random.md#RandNumeral)] [[play](https://go.dev/play/p/g4JWVpHsJcf)] - **RandNumeralOrLetter** : generate a random numeral or letter string. @@ -885,15 +948,15 @@ import "github.com/duke-git/lancet/v2/strutil" #### Function list: -- **After** : returns the substring after the first occurrence of a specified string in the source string. +- **After** : 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)] [[play](https://go.dev/play/p/RbCOQqCDA7m)] -- **AfterLast** : 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)] +- **AfterLast** : 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)] -- **Before** : returns the substring before the first occurrence of a specified string in the source string. +- **Before** : 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)] [[play](https://go.dev/play/p/JAWTZDS4F5w)] -- **BeforeLast** : returns the substring before the last occurrence of a specified string in the source string. +- **BeforeLast** : 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)] [[play](https://go.dev/play/p/pJfXXAoG_Te)] - **CamelCase** : coverts source string to its camelCase string. @@ -935,7 +998,7 @@ import "github.com/duke-git/lancet/v2/strutil" - **SplitEx** : 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)] [[play](https://go.dev/play/p/Us-ySSbWh-3)] -- **Substring** : returns a substring of the specified length starting at the specified offset position. +- **Substring** : 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)] - **Wrap** : wrap a string with given string. [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Wrap)] diff --git a/README_zh-CN.md b/README_zh-CN.md index a8f0ca9..d0acabf 100644 --- a/README_zh-CN.md +++ b/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) -- [AddHour](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#AddHour) -- [AddMinute](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#AddMinute) -- [BeginOfMinute](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfMinute) -- [BeginOfHour](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfHour) -- [BeginOfDay](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfDay) -- [BeginOfWeek](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfWeek) -- [BeginOfMonth](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfMonth) -- [BeginOfYear](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#BeginOfYear) -- [EndOfMinute](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfMinute) -- [EndOfHour](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfHour) -- [EndOfDay](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfDay) -- [EndOfWeek](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfWeek) -- [EndOfMonth](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfMonth) -- [EndOfYear](https://github.com/duke-git/lancet/blob/main/docs/datetime.md#EndOfYear) -- [GetNowDate](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#GetNowDate) -- [GetNowTime](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#GetNowTime) -- [GetNowDateTime](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#GetNowDateTime) -- [GetZeroHourTimestamp](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#GetZeroHourTimestamp) -- [GetNightTimestamp](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#GetNightTimestamp) -- [FormatTimeToStr](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#FormatTimeToStr) -- [FormatStrToTime](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#FormatStrToTime) -- [NewUnix](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#NewUnix) -- [NewUnixNow](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#NewUnixNow) -- [NewFormat](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#NewFormat) -- [NewISO8601](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#NewISO8601) -- [ToUnix](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToUnix) -- [ToFormat](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToFormat) -- [ToFormatForTpl](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToFormatForTpl) -- [ToIso8601](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToIso8601) +- **AddDay** : 将日期加/减天数。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#AddDay)] + [[play](https://go.dev/play/p/dIGbs_uTdFa)] +- **AddHour** : 将日期加/减小时数。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#AddHour)] + [[play](https://go.dev/play/p/rcMjd7OCsi5)] +- **AddMinute** : 将日期加/减分钟数。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#AddMinute)] + [[play](https://go.dev/play/p/nT1heB1KUUK)] +- **BeginOfMinute** : 返回指定时间的分钟开始时间。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#BeginOfMinute)] + [[play](https://go.dev/play/p/ieOLVJ9CiFT)] +- **BeginOfHour** : 返回指定时间的小时开始时间。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#BeginOfHour)] + [[play](https://go.dev/play/p/GhdGFnDWpYs)] +- **BeginOfDay** : 返回指定时间的当天开始时间。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#BeginOfDay)] + [[play](https://go.dev/play/p/94m_UT6cWs9)] +- **BeginOfWeek** : 返回指定时间的每周开始时间,默认开始时间星期日。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#BeginOfWeek)] + [[play](https://go.dev/play/p/ynjoJPz7VNV)] +- **BeginOfMonth** : 返回指定时间的当月开始时间。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#BeginOfMonth)] + [[play](https://go.dev/play/p/bWXVFsmmzwL)] +- **BeginOfYear** : 返回指定时间的当年开始时间。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#BeginOfYear)] + [[play](https://go.dev/play/p/i326DSwLnV8)] +- **EndOfMinute** : 返回指定时间的分钟结束时间。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#EndOfMinute)] + [[play](https://go.dev/play/p/yrL5wGzPj4z)] +- **EndOfHour** : 返回指定时间的小时结束时间。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#EndOfHour)] + [[play](https://go.dev/play/p/6ce3j_6cVqN)] +- **EndOfDay** : 返回指定时间的当天结束时间。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#EndOfDay)] + [[play](https://go.dev/play/p/eMBOvmq5Ih1)] +- **EndOfWeek** : 返回指定时间的星期结束时间,默认结束时间星期六。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#EndOfWeek)] + [[play](https://go.dev/play/p/i08qKXD9flf)] +- **EndOfMonth** : 返回指定时间的月份结束时间。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#EndOfMonth)] + [[play](https://go.dev/play/p/_GWh10B3Nqi)] +- **EndOfYear** : 返回指定时间的年份结束时间。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#EndOfYear)] + [[play](https://go.dev/play/p/G01cKlMCvNm)] +- **GetNowDate** : 获取当天日期,返回格式: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)] +- **GetNowTime** : 获取当时时间,返回格式: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)] +- **GetNowDateTime** : 获取当时日期和时间,返回格式: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)] +- **GetZeroHourTimestamp** : 获取零时时间戳(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)] +- **GetNightTimestamp** : 获取午夜时间戳(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)] +- **FormatTimeToStr** : 将日期格式化成字符串。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#FormatTimeToStr)] + [[play](https://go.dev/play/p/_Ia7M8H_OvE)] +- **FormatStrToTime** : 将字符串格式化成时间。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#FormatStrToTime)] + [[play](https://go.dev/play/p/1h9FwdU8ql4)] +- **NewUnix** : 创建一个unix时间戳。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#NewUnix)] + [[play](https://go.dev/play/p/psoSuh_kLRt)] +- **NewUnixNow** : 创建一个当前时间的unix时间戳。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#NewUnixNow)] + [[play](https://go.dev/play/p/U4PPx-9D0oz)] +- **NewFormat** : 创建一个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)] +- **NewISO8601** : 创建一个iso8601格式时间字符串的unix时间戳。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#NewISO8601)] + [[play](https://go.dev/play/p/mkhOHQkdeA2)] +- **ToUnix** : 返回unix时间戳。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToUnix)] + [[play](https://go.dev/play/p/_LUiwAdocjy)] +- **ToFormat** : 返回格式'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)] +- **ToFormatForTpl** : 返回tpl格式指定的日期字符串。 + [[doc](https://github.com/duke-git/lancet/blob/main/docs/datetime_zh-CN.md#ToFormatForTpl)] + [[play](https://go.dev/play/p/nyXxXcQJ8L5)] +- **ToIso8601** : 返回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. diff --git a/datetime/conversion.go b/datetime/conversion.go index ddf01fb..4dc80e8 100644 --- a/datetime/conversion.go +++ b/datetime/conversion.go @@ -9,17 +9,20 @@ type theTime struct { 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 { 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 { 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) { timeLayout := "2006-01-02 15:04:05" loc := time.FixedZone("CST", 8*3600) @@ -30,7 +33,8 @@ func NewFormat(t string) (*theTime, error) { 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) { t, err := time.ParseInLocation(time.RFC3339, iso8601, time.UTC) if err != nil { @@ -39,22 +43,26 @@ func NewISO8601(iso8601 string) (*theTime, error) { 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 { 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 { 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 { 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 { return time.Unix(t.unix, 0).Format(time.RFC3339) } diff --git a/datetime/conversion_test.go b/datetime/conversion_test.go index 6319da0..57c294d 100644 --- a/datetime/conversion_test.go +++ b/datetime/conversion_test.go @@ -19,9 +19,6 @@ func TestToUnix(t *testing.T) { func TestToFormat(t *testing.T) { 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") assert.IsNil(err) diff --git a/datetime/datetime.go b/datetime/datetime.go index 7bed8ea..4a3c7b9 100644 --- a/datetime/datetime.go +++ b/datetime/datetime.go @@ -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 { 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 { 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 { 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 { 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 { 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 { 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 { ts := time.Now().Format("2006-01-02") t, _ := time.Parse("2006-01-02", ts) 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 { 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 { 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) { v, ok := timeFormat[format] if !ok { @@ -111,43 +121,50 @@ func FormatStrToTime(str, format string) (time.Time, error) { 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 { y, m, d := t.Date() 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 { y, m, d := t.Date() 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 { y, m, d := t.Date() 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 { y, m, d := t.Date() 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 { y, m, d := t.Date() 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 { y, m, d := t.Date() 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 { var beginFromWeekday = time.Sunday if len(beginFrom) > 0 { @@ -161,7 +178,8 @@ func BeginOfWeek(t time.Time, beginFrom ...time.Weekday) time.Time { 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 { var endWithWeekday = time.Saturday if len(endWith) > 0 { @@ -175,24 +193,28 @@ func EndOfWeek(t time.Time, endWith ...time.Weekday) time.Time { 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 { y, m, _ := t.Date() 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 { 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 { y, _, _ := t.Date() 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 { return BeginOfYear(t).AddDate(1, 0, 0).Add(-time.Nanosecond) } diff --git a/datetime/datetime_example_test.go b/datetime/datetime_example_test.go new file mode 100644 index 0000000..cc05a4f --- /dev/null +++ b/datetime/datetime_example_test.go @@ -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 +} diff --git a/datetime/datetime_test.go b/datetime/datetime_test.go index bb4e2bb..afa7022 100644 --- a/datetime/datetime_test.go +++ b/datetime/datetime_test.go @@ -52,8 +52,8 @@ func TestGetNowDate(t *testing.T) { assert.Equal(expected, GetNowDate()) } -func TestGetNotTime(t *testing.T) { - assert := internal.NewAssert(t, "TestGetNotTime") +func TestGetNowTime(t *testing.T) { + assert := internal.NewAssert(t, "TestGetNowTime") expected := time.Now().Format("15:04:05") assert.Equal(expected, GetNowTime()) } @@ -81,7 +81,6 @@ func TestFormatTimeToStr(t *testing.T) { for i := 0; i < len(cases); i++ { actual := FormatTimeToStr(datetime, cases[i]) assert.Equal(expected[i], actual) - } } diff --git a/docs/datetime_zh-CN.md b/docs/datetime_zh-CN.md index 36b5222..918d7f0 100644 --- a/docs/datetime_zh-CN.md +++ b/docs/datetime_zh-CN.md @@ -377,7 +377,7 @@ func main() { ``` ### EndOfDay -
返回指定时间的当天结束时间.
+返回指定时间的当天结束时间
函数签名: @@ -541,7 +541,7 @@ func main() { ### GetNowDateTime -获取当时日期和时间,返回格式:yyyy-mm-dd hh:mm:ss.
+获取当时日期和时间,返回格式:yyyy-mm-dd hh:mm:ss
函数签名: @@ -568,7 +568,7 @@ func main() { ### GetZeroHourTimestamp -获取零时时间戳(timestamp of 00:00).
+获取零时时间戳(timestamp of 00:00)
函数签名: @@ -595,7 +595,7 @@ func main() { ### GetNightTimestamp -获取午夜时间戳(timestamp of 23:59).
+获取午夜时间戳(timestamp of 23:59)
函数签名: