carbon/outputer.go

902 lines
27 KiB
Go
Raw Normal View History

2021-02-18 14:32:31 +08:00
package carbon
import (
"bytes"
"fmt"
2021-02-18 14:32:31 +08:00
"strconv"
2021-02-23 09:32:55 +08:00
"strings"
"time"
2021-02-18 14:32:31 +08:00
)
2022-05-07 09:20:38 +08:00
// String implements the interface Stringer for Carbon struct.
2021-07-28 15:18:05 +08:00
// 实现 Stringer 接口
func (c Carbon) String() string {
2024-10-11 22:54:20 +08:00
return c.Layout(c.layout, c.Location())
}
// GoString implements fmt.GoStringer and formats c to be printed in Go source code.
// 实现 fmt.GoStringer 接口,并格式化 c 以在 Go 源代码中打印
func (c Carbon) GoString() string {
if c.Error != nil || c.IsZero() {
return ""
}
return c.StdTime().GoString()
}
2022-04-24 22:15:44 +08:00
// ToString outputs a string in "2006-01-02 15:04:05.999999999 -0700 MST" layout.
2022-04-14 10:43:07 +08:00
// 输出 "2006-01-02 15:04:05.999999999 -0700 MST" 格式字符串
func (c Carbon) ToString(timezone ...string) string {
2021-08-05 19:42:55 +08:00
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
2021-07-09 15:12:48 +08:00
}
if c.Error != nil || c.IsZero() {
2021-08-05 19:42:55 +08:00
return ""
}
2024-02-01 11:24:43 +08:00
return c.StdTime().String()
2021-04-05 20:47:22 +08:00
}
2022-04-24 22:15:44 +08:00
// ToMonthString outputs a string in month layout like "January", i18n is supported.
2021-07-28 15:18:05 +08:00
// 输出完整月份字符串支持i18n
func (c Carbon) ToMonthString(timezone ...string) string {
2021-08-05 19:42:55 +08:00
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil || c.IsZero() {
2021-08-05 19:42:55 +08:00
return ""
}
if len(c.lang.resources) == 0 {
c.lang.SetLocale(defaultLocale)
2021-02-23 09:32:55 +08:00
}
2024-01-22 17:08:16 +08:00
c.lang.rw.Lock()
defer c.lang.rw.Unlock()
2024-02-01 11:24:43 +08:00
if resources, ok := c.lang.resources["months"]; ok {
slice := strings.Split(resources, "|")
if len(slice) == MonthsPerYear {
2021-08-02 10:23:53 +08:00
return slice[c.Month()-1]
}
2021-02-23 09:32:55 +08:00
}
return ""
}
2022-04-24 22:15:44 +08:00
// ToShortMonthString outputs a string in short month layout like "Jan", i18n is supported.
2021-07-28 15:18:05 +08:00
// 输出缩写月份字符串支持i18n
func (c Carbon) ToShortMonthString(timezone ...string) string {
2021-08-05 19:42:55 +08:00
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil || c.IsZero() {
2021-08-05 19:42:55 +08:00
return ""
}
if len(c.lang.resources) == 0 {
c.lang.SetLocale(defaultLocale)
2021-02-23 09:32:55 +08:00
}
2024-01-22 17:08:16 +08:00
c.lang.rw.Lock()
defer c.lang.rw.Unlock()
2024-02-01 11:24:43 +08:00
if resources, ok := c.lang.resources["short_months"]; ok {
slice := strings.Split(resources, "|")
if len(slice) == MonthsPerYear {
2021-08-02 10:23:53 +08:00
return slice[c.Month()-1]
}
2021-02-23 09:32:55 +08:00
}
return ""
}
2022-04-24 22:15:44 +08:00
// ToWeekString outputs a string in week layout like "Sunday", i18n is supported.
2021-07-28 15:18:05 +08:00
// 输出完整星期字符串支持i18n
func (c Carbon) ToWeekString(timezone ...string) string {
2021-08-05 19:42:55 +08:00
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil || c.IsZero() {
2021-08-05 19:42:55 +08:00
return ""
}
if len(c.lang.resources) == 0 {
c.lang.SetLocale(defaultLocale)
2021-02-23 09:32:55 +08:00
}
2024-01-22 17:08:16 +08:00
c.lang.rw.Lock()
defer c.lang.rw.Unlock()
2024-02-01 11:24:43 +08:00
if resources, ok := c.lang.resources["weeks"]; ok {
slice := strings.Split(resources, "|")
if len(slice) == DaysPerWeek {
return slice[c.DayOfWeek()%DaysPerWeek]
2021-08-02 10:23:53 +08:00
}
2021-02-23 09:32:55 +08:00
}
return ""
}
2022-04-24 22:15:44 +08:00
// ToShortWeekString outputs a string in short week layout like "Sun", i18n is supported.
2021-07-28 15:18:05 +08:00
// 输出缩写星期字符串支持i18n
func (c Carbon) ToShortWeekString(timezone ...string) string {
2021-08-05 19:42:55 +08:00
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil || c.IsZero() {
2021-08-05 19:42:55 +08:00
return ""
}
if len(c.lang.resources) == 0 {
c.lang.SetLocale(defaultLocale)
2021-02-23 09:32:55 +08:00
}
2024-01-22 17:08:16 +08:00
c.lang.rw.Lock()
defer c.lang.rw.Unlock()
2024-02-01 11:24:43 +08:00
if resources, ok := c.lang.resources["short_weeks"]; ok {
slice := strings.Split(resources, "|")
if len(slice) == DaysPerWeek {
return slice[c.DayOfWeek()%DaysPerWeek]
2021-08-02 10:23:53 +08:00
}
2021-02-23 09:32:55 +08:00
}
return ""
}
2022-04-24 22:15:44 +08:00
// ToDayDateTimeString outputs a string in "Mon, Jan 2, 2006 3:04 PM" layout.
2022-04-14 10:43:07 +08:00
// 输出 "Mon, Jan 2, 2006 3:04 PM" 格式字符串
func (c Carbon) ToDayDateTimeString(timezone ...string) string {
2021-08-05 19:42:55 +08:00
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil || c.IsZero() {
2021-08-05 19:42:55 +08:00
return ""
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Format(DayDateTimeLayout)
}
2022-04-24 22:15:44 +08:00
// ToDateTimeString outputs a string in "2006-01-02 15:04:05" layout.
2022-04-14 10:43:07 +08:00
// 输出 "2006-01-02 15:04:05" 格式字符串
func (c Carbon) ToDateTimeString(timezone ...string) string {
2021-07-31 13:28:08 +08:00
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil || c.IsZero() {
2021-08-05 19:42:55 +08:00
return ""
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Format(DateTimeLayout)
2021-02-18 14:32:31 +08:00
}
2022-04-24 22:15:44 +08:00
// ToDateTimeMilliString outputs a string in "2006-01-02 15:04:05.999" layout.
2022-04-14 10:43:07 +08:00
// 输出 "2006-01-02 15:04:05.999" 格式字符串
func (c Carbon) ToDateTimeMilliString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil || c.IsZero() {
return ""
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Format(DateTimeMilliLayout)
}
2022-04-24 22:15:44 +08:00
// ToDateTimeMicroString outputs a string in "2006-01-02 15:04:05.999999" layout.
2022-04-14 10:43:07 +08:00
// 输出 "2006-01-02 15:04:05.999999" 格式字符串
func (c Carbon) ToDateTimeMicroString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil || c.IsZero() {
return ""
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Format(DateTimeMicroLayout)
}
2022-04-24 22:15:44 +08:00
// ToDateTimeNanoString outputs a string in "2006-01-02 15:04:05.999999999" layout.
2022-04-14 10:43:07 +08:00
// 输出 "2006-01-02 15:04:05.999999999" 格式字符串
func (c Carbon) ToDateTimeNanoString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil || c.IsZero() {
return ""
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Format(DateTimeNanoLayout)
}
2022-04-24 22:15:44 +08:00
// ToShortDateTimeString outputs a string in "20060102150405" layout.
2022-04-14 10:43:07 +08:00
// 输出 "20060102150405" 格式字符串
func (c Carbon) ToShortDateTimeString(timezone ...string) string {
2021-07-31 13:28:08 +08:00
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil || c.IsZero() {
2021-08-05 19:42:55 +08:00
return ""
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Format(ShortDateTimeLayout)
}
2022-04-24 22:15:44 +08:00
// ToShortDateTimeMilliString outputs a string in "20060102150405.999" layout.
2022-04-14 10:43:07 +08:00
// 输出 "20060102150405.999" 格式字符串
func (c Carbon) ToShortDateTimeMilliString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil || c.IsZero() {
return ""
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Format(ShortDateTimeMilliLayout)
}
2022-04-24 22:15:44 +08:00
// ToShortDateTimeMicroString outputs a string in "20060102150405.999999" layout.
2022-04-14 10:43:07 +08:00
// 输出 "20060102150405.999999" 格式字符串
func (c Carbon) ToShortDateTimeMicroString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil || c.IsZero() {
return ""
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Format(ShortDateTimeMicroLayout)
}
2022-04-24 22:15:44 +08:00
// ToShortDateTimeNanoString outputs a string in "20060102150405.999999999" layout.
2022-04-14 10:43:07 +08:00
// 输出 "20060102150405.999999999" 格式字符串
func (c Carbon) ToShortDateTimeNanoString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil || c.IsZero() {
return ""
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Format(ShortDateTimeNanoLayout)
}
2022-04-24 22:15:44 +08:00
// ToDateString outputs a string in "2006-01-02" layout.
2022-04-14 10:43:07 +08:00
// 输出 "2006-01-02" 格式字符串
func (c Carbon) ToDateString(timezone ...string) string {
2021-07-31 13:28:08 +08:00
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil || c.IsZero() {
2021-08-05 19:42:55 +08:00
return ""
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Format(DateLayout)
2021-02-18 14:32:31 +08:00
}
// ToDateMilliString outputs a string in "2006-01-02.999" layout.
// 输出 "2006-01-02.999" 格式字符串
func (c Carbon) ToDateMilliString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil || c.IsZero() {
return ""
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Format(DateMilliLayout)
}
// ToDateMicroString outputs a string in "2006-01-02.999999" layout.
// 输出 "2006-01-02.999999" 格式字符串
func (c Carbon) ToDateMicroString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil || c.IsZero() {
return ""
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Format(DateMicroLayout)
}
// ToDateNanoString outputs a string in "2006-01-02.999999999" layout.
// 输出 "2006-01-02.999999999" 格式字符串
func (c Carbon) ToDateNanoString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil || c.IsZero() {
return ""
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Format(DateNanoLayout)
}
2022-04-24 22:15:44 +08:00
// ToShortDateString outputs a string in "20060102" layout.
2022-04-14 10:43:07 +08:00
// 输出 "20060102" 格式字符串
func (c Carbon) ToShortDateString(timezone ...string) string {
2021-07-31 13:28:08 +08:00
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil || c.IsZero() {
2021-08-05 19:42:55 +08:00
return ""
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Format(ShortDateLayout)
}
// ToShortDateMilliString outputs a string in "20060102.999" layout.
// 输出 "20060102.999" 格式字符串
func (c Carbon) ToShortDateMilliString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil || c.IsZero() {
return ""
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Format(ShortDateMilliLayout)
}
// ToShortDateMicroString outputs a string in "20060102.999999" layout.
// 输出 "20060102.999999" 格式字符串
func (c Carbon) ToShortDateMicroString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil || c.IsZero() {
return ""
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Format(ShortDateMicroLayout)
}
// ToShortDateNanoString outputs a string in "20060102.999999999" layout.
// 输出 "20060102.999999999" 格式字符串
func (c Carbon) ToShortDateNanoString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil || c.IsZero() {
return ""
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Format(ShortDateNanoLayout)
}
2022-04-24 22:15:44 +08:00
// ToTimeString outputs a string in "15:04:05" layout.
2022-04-14 10:43:07 +08:00
// 输出 "15:04:05" 格式字符串
func (c Carbon) ToTimeString(timezone ...string) string {
2021-07-31 13:28:08 +08:00
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil || c.IsZero() {
2021-08-05 19:42:55 +08:00
return ""
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Format(TimeLayout)
2021-02-18 14:32:31 +08:00
}
// ToTimeMilliString outputs a string in "15:04:05.999" layout.
// 输出 "15:04:05.999" 格式字符串
func (c Carbon) ToTimeMilliString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil || c.IsZero() {
return ""
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Format(TimeMilliLayout)
}
// ToTimeMicroString outputs a string in "15:04:05.999999" layout.
// 输出 "15:04:05.999999" 格式字符串
func (c Carbon) ToTimeMicroString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil || c.IsZero() {
return ""
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Format(TimeMicroLayout)
}
// ToTimeNanoString outputs a string in "15:04:05.999999999" layout.
// 输出 "15:04:05.999999999" 格式字符串
func (c Carbon) ToTimeNanoString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil || c.IsZero() {
return ""
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Format(TimeNanoLayout)
}
2022-04-24 22:15:44 +08:00
// ToShortTimeString outputs a string in "150405" layout.
2022-04-14 10:43:07 +08:00
// 输出 "150405" 格式字符串
func (c Carbon) ToShortTimeString(timezone ...string) string {
2021-07-31 13:28:08 +08:00
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
2021-07-09 15:12:48 +08:00
}
if c.Error != nil || c.IsZero() {
2021-08-05 19:42:55 +08:00
return ""
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Format(ShortTimeLayout)
2021-02-18 14:32:31 +08:00
}
// ToShortTimeMilliString outputs a string in "150405.999" layout.
// 输出 "150405.999" 格式字符串
func (c Carbon) ToShortTimeMilliString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil || c.IsZero() {
return ""
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Format(ShortTimeMilliLayout)
}
// ToShortTimeMicroString outputs a string in "150405.999999" layout.
// 输出 "150405.999999" 格式字符串
func (c Carbon) ToShortTimeMicroString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil || c.IsZero() {
return ""
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Format(ShortTimeMicroLayout)
}
// ToShortTimeNanoString outputs a string in "150405.999999999" layout.
// 输出 "150405.999999999" 格式字符串
func (c Carbon) ToShortTimeNanoString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil || c.IsZero() {
return ""
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Format(ShortTimeNanoLayout)
}
2022-04-24 22:15:44 +08:00
// ToAtomString outputs a string in "2006-01-02T15:04:05Z07:00" layout.
2022-04-14 10:43:07 +08:00
// 输出 "2006-01-02T15:04:05Z07:00" 格式字符串
func (c Carbon) ToAtomString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil || c.IsZero() {
return ""
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Format(AtomLayout)
}
2023-12-24 18:35:29 +08:00
// ToAnsicString outputs a string in "Mon Jan _2 15:04:05 2006" layout.
2022-04-14 10:43:07 +08:00
// 输出 "Mon Jan _2 15:04:05 2006" 格式字符串
2023-12-24 18:35:29 +08:00
func (c Carbon) ToAnsicString(timezone ...string) string {
2021-07-31 13:28:08 +08:00
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil || c.IsZero() {
2021-08-05 19:42:55 +08:00
return ""
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Format(ANSICLayout)
2021-02-18 14:32:31 +08:00
}
2022-04-24 22:15:44 +08:00
// ToCookieString outputs a string in "Monday, 02-Jan-2006 15:04:05 MST" layout.
2022-04-14 10:43:07 +08:00
// 输出 "Monday, 02-Jan-2006 15:04:05 MST" 格式字符串
func (c Carbon) ToCookieString(timezone ...string) string {
2021-07-31 13:28:08 +08:00
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
2021-02-18 14:32:31 +08:00
}
if c.Error != nil || c.IsZero() {
2021-08-05 19:42:55 +08:00
return ""
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Format(CookieLayout)
2021-02-18 14:32:31 +08:00
}
2022-04-14 10:43:07 +08:00
// ToRssString outputs a string in "Mon, 02 Jan 2006 15:04:05 -0700" format.
// 输出 "Mon, 02 Jan 2006 15:04:05 -0700" 格式字符串
func (c Carbon) ToRssString(timezone ...string) string {
2021-07-31 13:28:08 +08:00
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
2021-02-18 14:32:31 +08:00
}
if c.Error != nil || c.IsZero() {
2021-08-05 19:42:55 +08:00
return ""
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Format(RssLayout)
2021-02-18 14:32:31 +08:00
}
2022-04-24 22:15:44 +08:00
// ToW3cString outputs a string in "2006-01-02T15:04:05Z07:00" layout.
2022-04-14 10:43:07 +08:00
// 输出 "2006-01-02T15:04:05Z07:00" 格式字符串
func (c Carbon) ToW3cString(timezone ...string) string {
2024-02-20 13:58:03 +08:00
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil || c.IsZero() {
2024-02-20 13:58:03 +08:00
return ""
}
return c.StdTime().Format(W3cLayout)
2021-02-18 14:32:31 +08:00
}
2022-04-24 22:15:44 +08:00
// ToUnixDateString outputs a string in "Mon Jan _2 15:04:05 MST 2006" layout.
2022-04-14 10:43:07 +08:00
// 输出 "Mon Jan _2 15:04:05 MST 2006" 格式字符串
func (c Carbon) ToUnixDateString(timezone ...string) string {
2021-07-31 13:28:08 +08:00
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
2021-02-18 14:32:31 +08:00
}
if c.Error != nil || c.IsZero() {
2021-08-05 19:42:55 +08:00
return ""
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Format(UnixDateLayout)
2021-02-18 14:32:31 +08:00
}
2022-04-24 22:15:44 +08:00
// ToRubyDateString outputs a string in "Mon Jan 02 15:04:05 -0700 2006" layout.
2022-04-14 10:43:07 +08:00
// 输出 "Mon Jan 02 15:04:05 -0700 2006" 格式字符串
func (c Carbon) ToRubyDateString(timezone ...string) string {
2021-07-31 13:28:08 +08:00
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
2021-02-18 14:32:31 +08:00
}
if c.Error != nil || c.IsZero() {
2021-08-05 19:42:55 +08:00
return ""
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Format(RubyDateLayout)
2021-02-18 14:32:31 +08:00
}
2022-04-24 22:15:44 +08:00
// ToKitchenString outputs a string in "3:04PM" layout.
2022-04-14 10:43:07 +08:00
// 输出 "3:04PM" 格式字符串
func (c Carbon) ToKitchenString(timezone ...string) string {
2021-07-31 13:28:08 +08:00
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
2021-02-18 14:32:31 +08:00
}
if c.Error != nil || c.IsZero() {
2021-08-05 19:42:55 +08:00
return ""
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Format(KitchenLayout)
2021-02-18 14:32:31 +08:00
}
2022-04-24 22:15:44 +08:00
// ToIso8601String outputs a string in "2006-01-02T15:04:05-07:00" layout.
2022-04-14 10:43:07 +08:00
// 输出 "2006-01-02T15:04:05-07:00" 格式字符串
func (c Carbon) ToIso8601String(timezone ...string) string {
2021-07-31 13:28:08 +08:00
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil || c.IsZero() {
2021-08-05 19:42:55 +08:00
return ""
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Format(ISO8601Layout)
}
2022-04-24 22:15:44 +08:00
// ToIso8601MilliString outputs a string in "2006-01-02T15:04:05.999-07:00" layout.
// 输出 "2006-01-02T15:04:05.999-07:00" 格式字符串
func (c Carbon) ToIso8601MilliString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil || c.IsZero() {
return ""
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Format(ISO8601MilliLayout)
}
2022-04-24 22:15:44 +08:00
// ToIso8601MicroString outputs a string in "2006-01-02T15:04:05.999999-07:00" layout.
// 输出 "2006-01-02T15:04:05.999999-07:00" 格式字符串
func (c Carbon) ToIso8601MicroString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil || c.IsZero() {
return ""
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Format(ISO8601MicroLayout)
}
2022-04-24 22:15:44 +08:00
// ToIso8601NanoString outputs a string in "2006-01-02T15:04:05.999999999-07:00" layout.
// 输出 "2006-01-02T15:04:05.999999999-07:00" 格式字符串
func (c Carbon) ToIso8601NanoString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil || c.IsZero() {
return ""
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Format(ISO8601NanoLayout)
}
// ToIso8601ZuluString outputs a string in "2006-01-02T15:04:05Z" layout.
// 输出 "2006-01-02T15:04:05Z" 格式字符串
func (c Carbon) ToIso8601ZuluString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil || c.IsZero() {
return ""
}
return c.StdTime().Format(ISO8601ZuluLayout)
}
// ToIso8601ZuluMilliString outputs a string in "2006-01-02T15:04:05.999Z" layout.
// 输出 "2006-01-02T15:04:05.999Z" 格式字符串
func (c Carbon) ToIso8601ZuluMilliString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil || c.IsZero() {
return ""
}
return c.StdTime().Format(ISO8601ZuluMilliLayout)
}
// ToIso8601ZuluMicroString outputs a string in "2006-01-02T15:04:05.999999Z" layout.
// 输出 "2006-01-02T15:04:05.999999Z" 格式字符串
func (c Carbon) ToIso8601ZuluMicroString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil || c.IsZero() {
return ""
}
return c.StdTime().Format(ISO8601ZuluMicroLayout)
}
// ToIso8601ZuluNanoString outputs a string in "2006-01-02T15:04:05.999999999Z" layout.
// 输出 "2006-01-02T15:04:05.999999999Z" 格式字符串
func (c Carbon) ToIso8601ZuluNanoString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil || c.IsZero() {
return ""
}
return c.StdTime().Format(ISO8601ZuluNanoLayout)
}
2022-04-24 22:15:44 +08:00
// ToRfc822String outputs a string in "02 Jan 06 15:04 MST" layout.
2022-04-14 10:43:07 +08:00
// 输出 "02 Jan 06 15:04 MST" 格式字符串
func (c Carbon) ToRfc822String(timezone ...string) string {
2021-07-31 13:28:08 +08:00
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil || c.IsZero() {
2021-08-05 19:42:55 +08:00
return ""
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Format(RFC822Layout)
2021-02-18 14:32:31 +08:00
}
2022-04-24 22:15:44 +08:00
// ToRfc822zString outputs a string in "02 Jan 06 15:04 -0700" layout.
2022-04-14 10:43:07 +08:00
// 输出 "02 Jan 06 15:04 -0700" 格式字符串
func (c Carbon) ToRfc822zString(timezone ...string) string {
2021-07-31 13:28:08 +08:00
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
2021-02-18 14:32:31 +08:00
}
if c.Error != nil || c.IsZero() {
2021-08-05 19:42:55 +08:00
return ""
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Format(RFC822ZLayout)
2021-02-18 14:32:31 +08:00
}
2022-04-24 22:15:44 +08:00
// ToRfc850String outputs a string in "Monday, 02-Jan-06 15:04:05 MST" layout.
2022-04-14 10:43:07 +08:00
// 输出 "Monday, 02-Jan-06 15:04:05 MST" 格式字符串
func (c Carbon) ToRfc850String(timezone ...string) string {
2021-07-31 13:28:08 +08:00
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
2021-02-18 14:32:31 +08:00
}
if c.Error != nil || c.IsZero() {
2021-08-05 19:42:55 +08:00
return ""
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Format(RFC850Layout)
2021-02-18 14:32:31 +08:00
}
2022-04-24 22:15:44 +08:00
// ToRfc1036String outputs a string in "Mon, 02 Jan 06 15:04:05 -0700" layout.
2022-04-14 10:43:07 +08:00
// 输出 "Mon, 02 Jan 06 15:04:05 -0700" 格式字符串
func (c Carbon) ToRfc1036String(timezone ...string) string {
2021-07-31 13:28:08 +08:00
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
2021-02-18 14:32:31 +08:00
}
if c.Error != nil || c.IsZero() {
2021-08-05 19:42:55 +08:00
return ""
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Format(RFC1036Layout)
2021-02-18 14:32:31 +08:00
}
2022-04-24 22:15:44 +08:00
// ToRfc1123String outputs a string in "Mon, 02 Jan 2006 15:04:05 MST" layout.
2022-04-14 10:43:07 +08:00
// 输出 "Mon, 02 Jan 2006 15:04:05 MST" 格式字符串
func (c Carbon) ToRfc1123String(timezone ...string) string {
2021-07-31 13:28:08 +08:00
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
2021-02-18 14:32:31 +08:00
}
if c.Error != nil || c.IsZero() {
2021-08-05 19:42:55 +08:00
return ""
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Format(RFC1123Layout)
2021-02-18 14:32:31 +08:00
}
2022-04-24 22:15:44 +08:00
// ToRfc1123zString outputs a string in "Mon, 02 Jan 2006 15:04:05 -0700" layout.
2022-04-14 10:43:07 +08:00
// 输出 "Mon, 02 Jan 2006 15:04:05 -0700" 格式字符串
func (c Carbon) ToRfc1123zString(timezone ...string) string {
2021-07-31 13:28:08 +08:00
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
2021-02-18 14:32:31 +08:00
}
if c.Error != nil || c.IsZero() {
2021-08-05 19:42:55 +08:00
return ""
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Format(RFC1123ZLayout)
2021-02-18 14:32:31 +08:00
}
2022-04-24 22:15:44 +08:00
// ToRfc2822String outputs a string in "Mon, 02 Jan 2006 15:04:05 -0700" layout.
2022-04-14 10:43:07 +08:00
// 输出 "Mon, 02 Jan 2006 15:04:05 -0700" 格式字符串
func (c Carbon) ToRfc2822String(timezone ...string) string {
2021-07-31 13:28:08 +08:00
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil || c.IsZero() {
2021-08-05 19:42:55 +08:00
return ""
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Format(RFC2822Layout)
}
2022-04-24 22:15:44 +08:00
// ToRfc3339String outputs a string in "2006-01-02T15:04:05Z07:00" layout.
2022-04-14 10:43:07 +08:00
// 输出 "2006-01-02T15:04:05Z07:00" 格式字符串
func (c Carbon) ToRfc3339String(timezone ...string) string {
2021-07-31 13:28:08 +08:00
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil || c.IsZero() {
2021-08-05 19:42:55 +08:00
return ""
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Format(RFC3339Layout)
2021-02-18 14:32:31 +08:00
}
2022-04-24 22:15:44 +08:00
// ToRfc3339MilliString outputs a string in "2006-01-02T15:04:05.999Z07:00" layout.
2022-04-14 10:43:07 +08:00
// 输出 "2006-01-02T15:04:05.999Z07:00" 格式字符串
func (c Carbon) ToRfc3339MilliString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil || c.IsZero() {
return ""
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Format(RFC3339MilliLayout)
}
2022-04-24 22:15:44 +08:00
// ToRfc3339MicroString outputs a string in "2006-01-02T15:04:05.999999Z07:00" layout.
2022-04-14 10:43:07 +08:00
// 输出 "2006-01-02T15:04:05.999999Z07:00" 格式字符串
func (c Carbon) ToRfc3339MicroString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil || c.IsZero() {
return ""
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Format(RFC3339MicroLayout)
}
2022-04-24 22:15:44 +08:00
// ToRfc3339NanoString outputs a string in "2006-01-02T15:04:05.999999999Z07:00" layout.
2022-04-14 10:43:07 +08:00
// 输出 "2006-01-02T15:04:05.999999999Z07:00" 格式字符串
func (c Carbon) ToRfc3339NanoString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil || c.IsZero() {
return ""
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Format(RFC3339NanoLayout)
}
2022-04-24 22:15:44 +08:00
// ToRfc7231String outputs a string in "Mon, 02 Jan 2006 15:04:05 GMT" layout.
2022-04-14 10:43:07 +08:00
// 输出 "Mon, 02 Jan 2006 15:04:05 GMT" 格式字符串
func (c Carbon) ToRfc7231String(timezone ...string) string {
2021-07-31 13:28:08 +08:00
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil || c.IsZero() {
2021-08-05 19:42:55 +08:00
return ""
}
2024-02-01 11:24:43 +08:00
return c.StdTime().Format(RFC7231Layout)
}
// ToFormattedDateString outputs a string in "Jan 2, 2006" layout.
// 输出 "Jan 2, 2006" 格式字符串
func (c Carbon) ToFormattedDateString(timezone ...string) string {
2021-07-31 13:28:08 +08:00
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil || c.IsZero() {
2021-08-05 19:42:55 +08:00
return ""
}
return c.StdTime().Format(FormattedDateLayout)
2021-02-18 14:32:31 +08:00
}
// ToFormattedDayDateString outputs a string in "Mon, Jan 2, 2006" layout.
// 输出 "Jan 2, 2006" 格式字符串
func (c Carbon) ToFormattedDayDateString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil || c.IsZero() {
return ""
}
return c.StdTime().Format(FormattedDayDateLayout)
}
// Layout outputs a string by layout.
// 输出指定布局模板的时间字符串
func (c Carbon) Layout(layout string, timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil || c.IsZero() {
return ""
}
return c.StdTime().Format(layout)
}
// Format outputs a string by format.
// 输出指定格式模板的时间字符串
func (c Carbon) Format(format string, timezone ...string) string {
2021-07-31 13:28:08 +08:00
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[0])
}
if c.Error != nil || c.IsZero() {
2021-08-05 19:42:55 +08:00
return ""
}
buffer := bytes.NewBuffer(nil)
for i := 0; i < len(format); i++ {
if layout, ok := formats[format[i]]; ok {
// support for i18n specific symbols
switch format[i] {
case 'l': // week, such as Monday
buffer.WriteString(c.ToWeekString())
case 'D': // short week, such as Mon
buffer.WriteString(c.ToShortWeekString())
case 'F': // month, such as January
buffer.WriteString(c.ToMonthString())
case 'M': // short month, such as Jan
buffer.WriteString(c.ToShortMonthString())
case 'U': // timestamp with second, such as 1596604455
buffer.WriteString(strconv.FormatInt(c.Timestamp(), 10))
case 'V': // timestamp with millisecond, such as 1596604455000
buffer.WriteString(strconv.FormatInt(c.TimestampMilli(), 10))
case 'X': // timestamp with microsecond, such as 1596604455000000
buffer.WriteString(strconv.FormatInt(c.TimestampMicro(), 10))
case 'Z': // timestamp with nanoseconds, such as 1596604455000000000
buffer.WriteString(strconv.FormatInt(c.TimestampNano(), 10))
default: // common symbols
2024-02-01 11:24:43 +08:00
buffer.WriteString(c.StdTime().Format(layout))
}
} else {
switch format[i] {
case '\\': // raw output, no parse
buffer.WriteByte(format[i+1])
i++
continue
case 'W': // week number of the year in ISO-8601 format, ranging from 01-52
week := fmt.Sprintf("%02d", c.WeekOfYear())
buffer.WriteString(week)
case 'N': // day of the week as a number in ISO-8601 format, ranging from 01-7
week := fmt.Sprintf("%02d", c.DayOfWeek())
buffer.WriteString(week)
case 'S': // abbreviated suffix for the day of the month, such as st, nd, rd, th
suffix := "th"
switch c.Day() {
case 1, 21, 31:
suffix = "st"
case 2, 22:
suffix = "nd"
case 3, 23:
suffix = "rd"
}
buffer.WriteString(suffix)
case 'L': // whether it is a leap year, if it is a leap year, it is 1, otherwise it is 0
if c.IsLeapYear() {
buffer.WriteString("1")
} else {
buffer.WriteString("0")
}
case 'G': // 24-hour format, no padding, ranging from 0-23
buffer.WriteString(strconv.Itoa(c.Hour()))
case 'v': // current millisecond, such as 999
s := c.Layout(".999")
buffer.WriteString(strings.Trim(s, "."))
case 'u': // current microsecond, such as 999999
s := c.Layout(".999999")
buffer.WriteString(strings.Trim(s, "."))
case 'x': // current nanosecond, such as 999999999
s := c.Layout(".999999999")
buffer.WriteString(strings.Trim(s, "."))
case 'w': // day of the week represented by the number, ranging from 0-6
buffer.WriteString(strconv.Itoa(c.DayOfWeek() - 1))
case 't': // number of days in the month, ranging from 28-31
buffer.WriteString(strconv.Itoa(c.DaysInMonth()))
case 'z': // day of the year, ranging from 0-365
buffer.WriteString(strconv.Itoa(c.DayOfYear() - 1))
case 'e': // current location, such as UTCGMTAtlantic/Azores
buffer.WriteString(c.Location())
case 'Q': // current quarter, ranging from 1-4
buffer.WriteString(strconv.Itoa(c.Quarter()))
case 'C': // current century, ranging from 0-99
buffer.WriteString(strconv.Itoa(c.Century()))
default:
buffer.WriteByte(format[i])
}
}
}
return buffer.String()
2021-02-18 14:32:31 +08:00
}
2024-02-01 11:24:43 +08:00
// Deprecated: it will be removed in the future, use StdTime instead.
//
// ToStdTime converts Carbon to standard time.Time.
// 将 Carbon 转换成标准 time.Time
func (c Carbon) ToStdTime() time.Time {
2024-02-01 11:24:43 +08:00
return c.StdTime()
}