2020-09-13 12:44:17 +08:00
|
|
|
package carbon
|
|
|
|
|
|
|
|
import (
|
2021-02-01 21:29:21 +08:00
|
|
|
"bytes"
|
2020-09-13 12:44:17 +08:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2021-07-31 13:30:46 +08:00
|
|
|
// formats common formatting symbols
|
2021-02-01 21:29:21 +08:00
|
|
|
// 常规格式化符号
|
|
|
|
var formats = map[byte]string{
|
|
|
|
'd': "02", // Day: Day of the month, 2 digits with leading zeros. Eg: 01 to 31.
|
|
|
|
'D': "Mon", // Day: A textual representation of a day, three letters. Eg: Mon through Sun.
|
|
|
|
'j': "2", // Day: Day of the month without leading zeros. Eg: 1 to 31.
|
|
|
|
'l': "Monday", // Day: A full textual representation of the day of the week. Eg: Sunday through Saturday.
|
|
|
|
'F': "January", // Month: A full textual representation of a month, such as January or March. Eg: January through December.
|
|
|
|
'm': "01", // Month: Numeric representation of a month, with leading zeros. Eg: 01 through 12.
|
|
|
|
'M': "Jan", // Month: A short textual representation of a month, three letters. Eg: Jan through Dec.
|
|
|
|
'n': "1", // Month: Numeric representation of a month, without leading zeros. Eg: 1 through 12.
|
|
|
|
'Y': "2006", // Year: A full numeric representation of a year, 4 digits. Eg: 1999 or 2003.
|
|
|
|
'y': "06", // Year: A two digit representation of a year. Eg: 99 or 03.
|
2021-07-25 09:04:51 +08:00
|
|
|
'a': "pm", // Time: Lowercase morning or afternoon sign. Eg: am or pm.
|
|
|
|
'A': "PM", // Time: Uppercase morning or afternoon sign. Eg: AM or PM.
|
2021-02-01 21:29:21 +08:00
|
|
|
'g': "3", // Time: 12-hour format of an hour without leading zeros. Eg: 1 through 12.
|
|
|
|
'h': "03", // Time: 12-hour format of an hour with leading zeros. Eg: 01 through 12.
|
|
|
|
'H': "15", // Time: 24-hour format of an hour with leading zeros. Eg: 00 through 23.
|
|
|
|
'i': "04", // Time: Minutes with leading zeros. Eg: 00 to 59.
|
|
|
|
's': "05", // Time: Seconds with leading zeros. Eg: 00 through 59.
|
|
|
|
'O': "-0700", // Zone: Difference to Greenwich time (GMT) in hours. Eg: +0200.
|
|
|
|
'P': "-07:00", // Zone: Difference to Greenwich time (GMT) with colon between hours and minutes. Eg: +02:00.
|
|
|
|
'T': "MST", // Zone: Timezone abbreviation. Eg: UTC, EST, MDT ...
|
|
|
|
'c': "2006-01-02T15:04:05-07:00", // Format: ISO 8601 date. Eg: 2004-02-12T15:19:21+00:00.
|
|
|
|
'r': "Mon, 02 Jan 06 15:04 MST", // Format: RFC 2822 formatted date. Eg: Thu, 21 Dec 2000 16:01:07 +0200.
|
|
|
|
}
|
|
|
|
|
2021-07-31 13:30:46 +08:00
|
|
|
// format2layout convert a format string into a layout string
|
2021-07-28 15:18:05 +08:00
|
|
|
// format 转 layout
|
2020-09-13 12:44:17 +08:00
|
|
|
func format2layout(format string) string {
|
2021-02-01 21:29:21 +08:00
|
|
|
runes := []rune(format)
|
|
|
|
buffer := bytes.NewBuffer(nil)
|
|
|
|
for i := 0; i < len(runes); i++ {
|
|
|
|
if layout, ok := formats[byte(runes[i])]; ok {
|
|
|
|
buffer.WriteString(layout)
|
|
|
|
} else {
|
2021-07-19 09:52:27 +08:00
|
|
|
switch runes[i] {
|
|
|
|
case '\\': // 原样输出,不解析
|
|
|
|
buffer.WriteRune(runes[i+1])
|
|
|
|
i++
|
|
|
|
continue
|
|
|
|
default:
|
|
|
|
buffer.WriteRune(runes[i])
|
|
|
|
}
|
2021-02-01 21:29:21 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return buffer.String()
|
2020-09-13 12:44:17 +08:00
|
|
|
}
|
|
|
|
|
2021-07-28 15:18:05 +08:00
|
|
|
// getLocationByTimezone get a Location instance by a timezone string
|
|
|
|
// 通过时区获取 Location 实例
|
2021-01-26 17:08:27 +08:00
|
|
|
func getLocationByTimezone(timezone string) (*time.Location, error) {
|
2020-10-07 17:15:46 +08:00
|
|
|
loc, err := time.LoadLocation(timezone)
|
|
|
|
if err != nil {
|
2021-07-23 11:02:03 +08:00
|
|
|
err = invalidTimezoneError(timezone)
|
2020-09-13 12:44:17 +08:00
|
|
|
}
|
2021-01-26 17:08:27 +08:00
|
|
|
return loc, err
|
2020-09-13 12:44:17 +08:00
|
|
|
}
|
2020-10-10 16:11:22 +08:00
|
|
|
|
2021-07-28 15:18:05 +08:00
|
|
|
// parseByDuration parse as a Duration instance by a duration string
|
|
|
|
// 通过持续时长解析
|
2021-01-26 17:08:27 +08:00
|
|
|
func parseByDuration(duration string) (time.Duration, error) {
|
|
|
|
td, err := time.ParseDuration(duration)
|
2020-10-10 16:11:22 +08:00
|
|
|
if err != nil {
|
2021-07-23 11:02:03 +08:00
|
|
|
err = invalidDurationError(duration)
|
2020-10-10 16:11:22 +08:00
|
|
|
}
|
2021-01-26 17:08:27 +08:00
|
|
|
return td, err
|
2020-10-10 16:11:22 +08:00
|
|
|
}
|
2021-02-08 09:47:14 +08:00
|
|
|
|
2021-07-28 15:18:05 +08:00
|
|
|
// getAbsValue get absolute value
|
|
|
|
// 获取绝对值
|
2021-02-08 09:47:14 +08:00
|
|
|
func getAbsValue(value int64) int64 {
|
2021-02-18 14:32:31 +08:00
|
|
|
return (value ^ value>>31) - value>>31
|
2021-02-08 09:47:14 +08:00
|
|
|
}
|