1 Star 0 Fork 63

句号/carbon

forked from dromara/carbon 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
outputer.go 23.39 KB
一键复制 编辑 原始数据 按行查看 历史
gouguoyin 提交于 2022-05-07 09:20 . 2.1.6 更新注释信息
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775
package carbon
import (
"bytes"
"strconv"
"strings"
)
// String implements the interface Stringer for Carbon struct.
// 实现 Stringer 接口
func (c Carbon) String() string {
return c.ToDateTimeString()
}
// ToString outputs a string in "2006-01-02 15:04:05.999999999 -0700 MST" layout.
// 输出 "2006-01-02 15:04:05.999999999 -0700 MST" 格式字符串
func (c Carbon) ToString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Carbon2Time().String()
}
// ToMonthString outputs a string in month layout like "January", i18n is supported.
// 输出完整月份字符串,支持i18n
func (c Carbon) ToMonthString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
if len(c.lang.resources) == 0 {
c.lang.SetLocale(defaultLocale)
}
if months, ok := c.lang.resources["months"]; ok {
slice := strings.Split(months, "|")
if len(slice) == 12 {
return slice[c.Month()-1]
}
}
return ""
}
// ToShortMonthString outputs a string in short month layout like "Jan", i18n is supported.
// 输出缩写月份字符串,支持i18n
func (c Carbon) ToShortMonthString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
if len(c.lang.resources) == 0 {
c.lang.SetLocale(defaultLocale)
}
if months, ok := c.lang.resources["short_months"]; ok {
slice := strings.Split(months, "|")
if len(slice) == 12 {
return slice[c.Month()-1]
}
}
return ""
}
// ToWeekString outputs a string in week layout like "Sunday", i18n is supported.
// 输出完整星期字符串,支持i18n
func (c Carbon) ToWeekString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
if len(c.lang.resources) == 0 {
c.lang.SetLocale(defaultLocale)
}
if months, ok := c.lang.resources["weeks"]; ok {
slice := strings.Split(months, "|")
if len(slice) == 7 {
return slice[c.Week()]
}
}
return ""
}
// ToShortWeekString outputs a string in short week layout like "Sun", i18n is supported.
// 输出缩写星期字符串,支持i18n
func (c Carbon) ToShortWeekString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
if len(c.lang.resources) == 0 {
c.lang.SetLocale(defaultLocale)
}
if months, ok := c.lang.resources["short_weeks"]; ok {
slice := strings.Split(months, "|")
if len(slice) == 7 {
return slice[c.Week()]
}
}
return ""
}
// ToDayDateTimeString outputs a string in "Mon, Jan 2, 2006 3:04 PM" layout.
// 输出 "Mon, Jan 2, 2006 3:04 PM" 格式字符串
func (c Carbon) ToDayDateTimeString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Carbon2Time().Format(DayDateTimeLayout)
}
// ToDateTimeString outputs a string in "2006-01-02 15:04:05" layout.
// 输出 "2006-01-02 15:04:05" 格式字符串
func (c Carbon) ToDateTimeString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Carbon2Time().Format(DateTimeLayout)
}
// ToDateTimeMilliString outputs a string in "2006-01-02 15:04:05.999" layout.
// 输出 "2006-01-02 15:04:05.999" 格式字符串
func (c Carbon) ToDateTimeMilliString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Carbon2Time().Format(DateTimeMilliLayout)
}
// ToDateTimeMicroString outputs a string in "2006-01-02 15:04:05.999999" layout.
// 输出 "2006-01-02 15:04:05.999999" 格式字符串
func (c Carbon) ToDateTimeMicroString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Carbon2Time().Format(DateTimeMicroLayout)
}
// ToDateTimeNanoString outputs a string in "2006-01-02 15:04:05.999999999" layout.
// 输出 "2006-01-02 15:04:05.999999999" 格式字符串
func (c Carbon) ToDateTimeNanoString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Carbon2Time().Format(DateTimeNanoLayout)
}
// ToShortDateTimeString outputs a string in "20060102150405" layout.
// 输出 "20060102150405" 格式字符串
func (c Carbon) ToShortDateTimeString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Carbon2Time().Format(ShortDateTimeLayout)
}
// ToShortDateTimeMilliString outputs a string in "20060102150405.999" layout.
// 输出 "20060102150405.999" 格式字符串
func (c Carbon) ToShortDateTimeMilliString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Carbon2Time().Format(ShortDateTimeMilliLayout)
}
// ToShortDateTimeMicroString outputs a string in "20060102150405.999999" layout.
// 输出 "20060102150405.999999" 格式字符串
func (c Carbon) ToShortDateTimeMicroString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Carbon2Time().Format(ShortDateTimeMicroLayout)
}
// ToShortDateTimeNanoString outputs a string in "20060102150405.999999999" layout.
// 输出 "20060102150405.999999999" 格式字符串
func (c Carbon) ToShortDateTimeNanoString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Carbon2Time().Format(ShortDateTimeNanoLayout)
}
// ToDateString outputs a string in "2006-01-02" layout.
// 输出 "2006-01-02" 格式字符串
func (c Carbon) ToDateString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Carbon2Time().Format(DateLayout)
}
// 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[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Carbon2Time().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[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Carbon2Time().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[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Carbon2Time().Format(DateNanoLayout)
}
// ToShortDateString outputs a string in "20060102" layout.
// 输出 "20060102" 格式字符串
func (c Carbon) ToShortDateString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Carbon2Time().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[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Carbon2Time().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[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Carbon2Time().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[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Carbon2Time().Format(ShortDateNanoLayout)
}
// ToTimeString outputs a string in "15:04:05" layout.
// 输出 "15:04:05" 格式字符串
func (c Carbon) ToTimeString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Carbon2Time().Format(TimeLayout)
}
// 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[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Carbon2Time().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[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Carbon2Time().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[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Carbon2Time().Format(TimeNanoLayout)
}
// ToShortTimeString outputs a string in "150405" layout.
// 输出 "150405" 格式字符串
func (c Carbon) ToShortTimeString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Carbon2Time().Format(ShortTimeLayout)
}
// 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[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Carbon2Time().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[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Carbon2Time().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[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Carbon2Time().Format(ShortTimeNanoLayout)
}
// ToAtomString outputs a string in "2006-01-02T15:04:05Z07:00" layout.
// 输出 "2006-01-02T15:04:05Z07:00" 格式字符串
func (c Carbon) ToAtomString(timezone ...string) string {
return c.ToRfc3339String(timezone...)
}
// ToANSICString outputs a string in "Mon Jan _2 15:04:05 2006" layout.
// 输出 "Mon Jan _2 15:04:05 2006" 格式字符串
func (c Carbon) ToANSICString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Carbon2Time().Format(ANSICLayout)
}
// ToCookieString outputs a string in "Monday, 02-Jan-2006 15:04:05 MST" layout.
// 输出 "Monday, 02-Jan-2006 15:04:05 MST" 格式字符串
func (c Carbon) ToCookieString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Carbon2Time().Format(CookieLayout)
}
// 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 {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Carbon2Time().Format(RssLayout)
}
// ToW3cString outputs a string in "2006-01-02T15:04:05Z07:00" layout.
// 输出 "2006-01-02T15:04:05Z07:00" 格式字符串
func (c Carbon) ToW3cString(timezone ...string) string {
return c.ToRfc3339String(timezone...)
}
// ToUnixDateString outputs a string in "Mon Jan _2 15:04:05 MST 2006" layout.
// 输出 "Mon Jan _2 15:04:05 MST 2006" 格式字符串
func (c Carbon) ToUnixDateString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Carbon2Time().Format(UnixDateLayout)
}
// ToRubyDateString outputs a string in "Mon Jan 02 15:04:05 -0700 2006" layout.
// 输出 "Mon Jan 02 15:04:05 -0700 2006" 格式字符串
func (c Carbon) ToRubyDateString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Carbon2Time().Format(RubyDateLayout)
}
// ToKitchenString outputs a string in "3:04PM" layout.
// 输出 "3:04PM" 格式字符串
func (c Carbon) ToKitchenString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Carbon2Time().Format(KitchenLayout)
}
// ToIso8601String outputs a string in "2006-01-02T15:04:05-07:00" layout.
// 输出 "2006-01-02T15:04:05-07:00" 格式字符串
func (c Carbon) ToIso8601String(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Carbon2Time().Format(ISO8601Layout)
}
// 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[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Carbon2Time().Format(ISO8601MilliLayout)
}
// 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[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Carbon2Time().Format(ISO8601MicroLayout)
}
// 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[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Carbon2Time().Format(ISO8601NanoLayout)
}
// ToRfc822String outputs a string in "02 Jan 06 15:04 MST" layout.
// 输出 "02 Jan 06 15:04 MST" 格式字符串
func (c Carbon) ToRfc822String(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Carbon2Time().Format(RFC822Layout)
}
// ToRfc822zString outputs a string in "02 Jan 06 15:04 -0700" layout.
// 输出 "02 Jan 06 15:04 -0700" 格式字符串
func (c Carbon) ToRfc822zString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Carbon2Time().Format(RFC822ZLayout)
}
// ToRfc850String outputs a string in "Monday, 02-Jan-06 15:04:05 MST" layout.
// 输出 "Monday, 02-Jan-06 15:04:05 MST" 格式字符串
func (c Carbon) ToRfc850String(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Carbon2Time().Format(RFC850Layout)
}
// ToRfc1036String outputs a string in "Mon, 02 Jan 06 15:04:05 -0700" layout.
// 输出 "Mon, 02 Jan 06 15:04:05 -0700" 格式字符串
func (c Carbon) ToRfc1036String(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Carbon2Time().Format(RFC1036Layout)
}
// ToRfc1123String outputs a string in "Mon, 02 Jan 2006 15:04:05 MST" layout.
// 输出 "Mon, 02 Jan 2006 15:04:05 MST" 格式字符串
func (c Carbon) ToRfc1123String(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Carbon2Time().Format(RFC1123Layout)
}
// ToRfc1123zString outputs a string in "Mon, 02 Jan 2006 15:04:05 -0700" layout.
// 输出 "Mon, 02 Jan 2006 15:04:05 -0700" 格式字符串
func (c Carbon) ToRfc1123zString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Carbon2Time().Format(RFC1123ZLayout)
}
// ToRfc2822String outputs a string in "Mon, 02 Jan 2006 15:04:05 -0700" layout.
// 输出 "Mon, 02 Jan 2006 15:04:05 -0700" 格式字符串
func (c Carbon) ToRfc2822String(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Carbon2Time().Format(RFC2822Layout)
}
// ToRfc3339String outputs a string in "2006-01-02T15:04:05Z07:00" layout.
// 输出 "2006-01-02T15:04:05Z07:00" 格式字符串
func (c Carbon) ToRfc3339String(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Carbon2Time().Format(RFC3339Layout)
}
// ToRfc3339MilliString outputs a string in "2006-01-02T15:04:05.999Z07:00" layout.
// 输出 "2006-01-02T15:04:05.999Z07:00" 格式字符串
func (c Carbon) ToRfc3339MilliString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Carbon2Time().Format(RFC3339MilliLayout)
}
// ToRfc3339MicroString outputs a string in "2006-01-02T15:04:05.999999Z07:00" layout.
// 输出 "2006-01-02T15:04:05.999999Z07:00" 格式字符串
func (c Carbon) ToRfc3339MicroString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Carbon2Time().Format(RFC3339MicroLayout)
}
// ToRfc3339NanoString outputs a string in "2006-01-02T15:04:05.999999999Z07:00" layout.
// 输出 "2006-01-02T15:04:05.999999999Z07:00" 格式字符串
func (c Carbon) ToRfc3339NanoString(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Carbon2Time().Format(RFC3339NanoLayout)
}
// ToRfc7231String outputs a string in "Mon, 02 Jan 2006 15:04:05 GMT" layout.
// 输出 "Mon, 02 Jan 2006 15:04:05 GMT" 格式字符串
func (c Carbon) ToRfc7231String(timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Carbon2Time().Format(RFC7231Layout)
}
// ToLayoutString outputs a string by layout.
// 输出指定布局模板的时间字符串
func (c Carbon) ToLayoutString(layout string, timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Carbon2Time().Format(layout)
}
// Layout outputs a string by layout, it is shorthand for ToLayoutString.
// 输出指定布局模板的时间字符串, 是 ToLayoutString 的简写
func (c Carbon) Layout(layout string, timezone ...string) string {
return c.ToLayoutString(layout, timezone...)
}
// ToFormatString outputs a string by format.
// 输出指定格式模板的时间字符串
func (c Carbon) ToFormatString(format string, timezone ...string) string {
if len(timezone) > 0 {
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
buffer := bytes.NewBuffer(nil)
for i := 0; i < len(format); i++ {
if layout, ok := formats[format[i]]; ok {
buffer.WriteString(c.Carbon2Time().Format(layout))
} else {
switch format[i] {
case '\\': // 原样输出,不解析
buffer.WriteByte(format[i+1])
i++
continue
case 'W': // ISO-8601 格式数字表示的年份中的第几周,取值范围 1-52
buffer.WriteString(strconv.Itoa(c.WeekOfYear()))
case 'N': // ISO-8601 格式数字表示的星期中的第几天,取值范围 1-7
buffer.WriteString(strconv.Itoa(c.DayOfWeek()))
case 'S': // 月份中第几天的英文缩写后缀,如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': // 是否为闰年,如果是闰年为 1,否则为 0
if c.IsLeapYear() {
buffer.WriteString("1")
} else {
buffer.WriteString("0")
}
case 'G': // 数字表示的小时,24 小时格式,没有前导零,取值范围 0-23
buffer.WriteString(strconv.Itoa(c.Hour()))
case 'U': // 秒级时间戳,如 1611818268
buffer.WriteString(strconv.FormatInt(c.Timestamp(), 10))
case 'u': // 数字表示的毫秒,如 999
buffer.WriteString(strconv.Itoa(c.Millisecond()))
case 'w': // 数字表示的星期中的第几天,取值范围 0-6
buffer.WriteString(strconv.Itoa(c.DayOfWeek() - 1))
case 't': // 指定的月份有几天,取值范围 28-31
buffer.WriteString(strconv.Itoa(c.DaysInMonth()))
case 'z': // 年份中的第几天,取值范围 0-365
buffer.WriteString(strconv.Itoa(c.DayOfYear() - 1))
case 'e': // 当前位置,如 UTC,GMT,Atlantic/Azores
buffer.WriteString(c.Location())
case 'Q': // 当前季度,取值范围 1-4
buffer.WriteString(strconv.Itoa(c.Quarter()))
case 'C': // 当前世纪数,取值范围 0-99
buffer.WriteString(strconv.Itoa(c.Century()))
default:
buffer.WriteByte(format[i])
}
}
}
return buffer.String()
}
// Format outputs a string by format, it is shorthand for ToFormatString.
// 输出指定格式模板的时间字符串, 是 ToFormatString 的简写
func (c Carbon) Format(format string, timezone ...string) string {
return c.ToFormatString(format, timezone...)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/weiyq_0506/carbon.git
git@gitee.com:weiyq_0506/carbon.git
weiyq_0506
carbon
carbon
master

搜索帮助