1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-15 18:22:27 +08:00

doc: update format

This commit is contained in:
dudaodong
2023-07-26 15:28:20 +08:00
parent 326e7881a6
commit 2a303d5e4b
3 changed files with 32 additions and 15 deletions

View File

@@ -3,7 +3,7 @@
// Package datetime implements some functions to format date and time. // Package datetime implements some functions to format date and time.
// Note: // Note:
// 1. `format` param in FormatTimeToStr function should be as flow: // 1. `format` param in FormatTimeToStr function should be as flow (case no sensitive):
// "yyyy-mm-dd hh:mm:ss" // "yyyy-mm-dd hh:mm:ss"
// "yyyy-mm-dd hh:mm" // "yyyy-mm-dd hh:mm"
// "yyyy-mm-dd hh" // "yyyy-mm-dd hh"
@@ -18,14 +18,19 @@
// "yyyy/mm" // "yyyy/mm"
// "mm/dd" // "mm/dd"
// "dd/mm/yy hh:mm:ss" // "dd/mm/yy hh:mm:ss"
// "yyyymmdd"
// "mmddyy"
// "yyyy" // "yyyy"
// "yy"
// "mm" // "mm"
// "hh:mm:ss" // "hh:mm:ss"
// "hh:mm"
// "mm:ss" // "mm:ss"
package datetime package datetime
import ( import (
"fmt" "fmt"
"strings"
"time" "time"
) )
@@ -47,7 +52,10 @@ func init() {
"yyyy/mm": "2006/01", "yyyy/mm": "2006/01",
"mm/dd": "01/02", "mm/dd": "01/02",
"dd/mm/yy hh:mm:ss": "02/01/06 15:04:05", "dd/mm/yy hh:mm:ss": "02/01/06 15:04:05",
"yyyymmdd": "20060102",
"mmddyy": "010206",
"yyyy": "2006", "yyyy": "2006",
"yy": "06",
"mm": "01", "mm": "01",
"hh:mm:ss": "15:04:05", "hh:mm:ss": "15:04:05",
"hh:mm": "15:04", "hh:mm": "15:04",
@@ -126,22 +134,27 @@ func GetNightTimestamp() int64 {
// FormatTimeToStr convert time to string. // FormatTimeToStr convert time to string.
// Play: https://go.dev/play/p/_Ia7M8H_OvE // Play: https://go.dev/play/p/_Ia7M8H_OvE
func FormatTimeToStr(t time.Time, format string, timezone ...string) string { func FormatTimeToStr(t time.Time, format string, timezone ...string) string {
tf, ok := timeFormat[strings.ToLower(format)]
if !ok {
return ""
}
if timezone != nil && timezone[0] != "" { if timezone != nil && timezone[0] != "" {
loc, err := time.LoadLocation(timezone[0]) loc, err := time.LoadLocation(timezone[0])
if err != nil { if err != nil {
return "" return ""
} }
return t.In(loc).Format(timeFormat[format]) return t.In(loc).Format(tf)
} }
return t.Format(timeFormat[format]) return t.Format(tf)
} }
// FormatStrToTime convert string to time. // FormatStrToTime convert string to time.
// Play: https://go.dev/play/p/1h9FwdU8ql4 // Play: https://go.dev/play/p/1h9FwdU8ql4
func FormatStrToTime(str, format string, timezone ...string) (time.Time, error) { func FormatStrToTime(str, format string, timezone ...string) (time.Time, error) {
tf, ok := timeFormat[format] tf, ok := timeFormat[strings.ToLower(format)]
if !ok { if !ok {
return time.Time{}, fmt.Errorf("format %s not found", format) return time.Time{}, fmt.Errorf("format %s not support", format)
} }
if timezone != nil && timezone[0] != "" { if timezone != nil && timezone[0] != "" {
@@ -286,8 +299,8 @@ func IsWeekend(t time.Time) bool {
// NowDateOrTime return current datetime with specific format and timezone. // NowDateOrTime return current datetime with specific format and timezone.
// Play: todo // Play: todo
func NowDateOrTime(format string, timezone ...string) string { func NowDateOrTime(format string, timezone ...string) string {
tf := timeFormat[format] tf, ok := timeFormat[strings.ToLower(format)]
if tf == "" { if !ok {
return "" return ""
} }

View File

@@ -68,7 +68,7 @@ import (
## Note: ## Note:
1. 'format' string param in func FormatTimeToStr and FormatStrToTime function should be one of flows: 1. In below functions, the `format` string param should be one of flows value (case no sensitive):
- yyyy-mm-dd hh:mm:ss - yyyy-mm-dd hh:mm:ss
- yyyy-mm-dd hh:mm - yyyy-mm-dd hh:mm
@@ -79,14 +79,18 @@ import (
- dd-mm-yy hh:mm:ss - dd-mm-yy hh:mm:ss
- yyyy/mm/dd hh:mm:ss - yyyy/mm/dd hh:mm:ss
- yyyy/mm/dd hh:mm - yyyy/mm/dd hh:mm
- yyyy-mm-dd hh - yyyy/mm/dd hh
- yyyy/mm/dd - yyyy/mm/dd
- yyyy/mm - yyyy/mm
- mm/dd - mm/dd
- dd/mm/yy hh:mm:ss - dd/mm/yy hh:mm:ss
- yyyymmdd
- mmddyy
- yyyy - yyyy
- yy
- mm - mm
- hh:mm:ss - hh:mm:ss
- hh:mm
- mm:ss - mm:ss
### <span id="AddDay">AddDay</span> ### <span id="AddDay">AddDay</span>
@@ -1186,7 +1190,6 @@ func main() {
} }
``` ```
### <span id="BetweenSeconds">BetweenSeconds</span> ### <span id="BetweenSeconds">BetweenSeconds</span>
<p>Return the number of seconds between two times.</p> <p>Return the number of seconds between two times.</p>
@@ -1224,7 +1227,6 @@ func main() {
} }
``` ```
### <span id="DayOfYear">DayOfYear</span> ### <span id="DayOfYear">DayOfYear</span>
<p>Returns which day of the year the parameter date `t` is.</p> <p>Returns which day of the year the parameter date `t` is.</p>
@@ -1266,7 +1268,6 @@ func main() {
} }
``` ```
### <span id="IsWeekend">IsWeekend</span> ### <span id="IsWeekend">IsWeekend</span>
<p>Checks if passed time is weekend or not.</p> <p>Checks if passed time is weekend or not.</p>

View File

@@ -61,14 +61,13 @@ import (
- [IsWeekend](#IsWeekend) - [IsWeekend](#IsWeekend)
- [NowDateOrTime](#NowDateOrTime) - [NowDateOrTime](#NowDateOrTime)
<div STYLE="page-break-after: always;"></div> <div STYLE="page-break-after: always;"></div>
## 文档 ## 文档
## 注: ## 注:
1. 方法 FormatTimeToStr 和 FormatStrToTime 中的 format 参数值需要传以下类型之一: 1. 函数中`format`参数值需要传以下值之一 (忽略大小写):
- yyyy-mm-dd hh:mm:ss - yyyy-mm-dd hh:mm:ss
- yyyy-mm-dd hh:mm - yyyy-mm-dd hh:mm
@@ -79,14 +78,18 @@ import (
- dd-mm-yy hh:mm:ss - dd-mm-yy hh:mm:ss
- yyyy/mm/dd hh:mm:ss - yyyy/mm/dd hh:mm:ss
- yyyy/mm/dd hh:mm - yyyy/mm/dd hh:mm
- yyyy-mm-dd hh - yyyy/mm/dd hh
- yyyy/mm/dd - yyyy/mm/dd
- yyyy/mm - yyyy/mm
- mm/dd - mm/dd
- dd/mm/yy hh:mm:ss - dd/mm/yy hh:mm:ss
- yyyymmdd
- mmddyy
- yyyy - yyyy
- yy
- mm - mm
- hh:mm:ss - hh:mm:ss
- hh:mm
- mm:ss - mm:ss
### <span id="AddDay">AddDay</span> ### <span id="AddDay">AddDay</span>