the public fields Loc and Lang in the carbon structure are changed to private fields loc and lang

This commit is contained in:
gouguoyin 2021-08-16 09:33:08 +08:00
parent bb313d590e
commit e8b4fcd731
2 changed files with 114 additions and 178 deletions

View File

@ -25,7 +25,7 @@ func (c Carbon) IsNow() bool {
if c.IsInvalid() {
return false
}
return c.ToTimestamp() == c.Now().ToTimestamp()
return c.Timestamp() == c.Now().Timestamp()
}
// IsFuture whether is future time.
@ -34,7 +34,7 @@ func (c Carbon) IsFuture() bool {
if c.IsInvalid() {
return false
}
return c.ToTimestamp() > c.Now().ToTimestamp()
return c.Timestamp() > c.Now().Timestamp()
}
// IsPast whether is past time.
@ -43,7 +43,7 @@ func (c Carbon) IsPast() bool {
if c.IsInvalid() {
return false
}
return c.ToTimestamp() < c.Now().ToTimestamp()
return c.Timestamp() < c.Now().Timestamp()
}
// IsLeapYear whether is a leap year.
@ -52,7 +52,7 @@ func (c Carbon) IsLeapYear() bool {
if c.IsInvalid() {
return false
}
year := c.Time.In(c.Loc).Year()
year := c.Time.In(c.loc).Year()
if year%400 == 0 || (year%4 == 0 && year%100 != 0) {
return true
}
@ -65,7 +65,7 @@ func (c Carbon) IsLongYear() bool {
if c.IsInvalid() {
return false
}
_, w := time.Date(c.Year(), time.December, 31, 0, 0, 0, 0, c.Loc).ISOWeek()
_, w := time.Date(c.Year(), time.December, 31, 0, 0, 0, 0, c.loc).ISOWeek()
return w == weeksPerLongYear
}
@ -75,7 +75,7 @@ func (c Carbon) IsJanuary() bool {
if c.IsInvalid() {
return false
}
return c.Time.In(c.Loc).Month() == time.January
return c.Time.In(c.loc).Month() == time.January
}
// IsFebruary whether is February.
@ -84,7 +84,7 @@ func (c Carbon) IsFebruary() bool {
if c.IsInvalid() {
return false
}
return c.Time.In(c.Loc).Month() == time.February
return c.Time.In(c.loc).Month() == time.February
}
// IsMarch whether is March.
@ -93,7 +93,7 @@ func (c Carbon) IsMarch() bool {
if c.IsInvalid() {
return false
}
return c.Time.In(c.Loc).Month() == time.March
return c.Time.In(c.loc).Month() == time.March
}
// IsApril whether is April.
@ -102,7 +102,7 @@ func (c Carbon) IsApril() bool {
if c.IsInvalid() {
return false
}
return c.Time.In(c.Loc).Month() == time.April
return c.Time.In(c.loc).Month() == time.April
}
// IsMay whether is May.
@ -111,7 +111,7 @@ func (c Carbon) IsMay() bool {
if c.IsInvalid() {
return false
}
return c.Time.In(c.Loc).Month() == time.May
return c.Time.In(c.loc).Month() == time.May
}
// IsJune whether is June.
@ -120,7 +120,7 @@ func (c Carbon) IsJune() bool {
if c.IsInvalid() {
return false
}
return c.Time.In(c.Loc).Month() == time.June
return c.Time.In(c.loc).Month() == time.June
}
// IsJuly whether is July.
@ -129,7 +129,7 @@ func (c Carbon) IsJuly() bool {
if c.IsInvalid() {
return false
}
return c.Time.In(c.Loc).Month() == time.July
return c.Time.In(c.loc).Month() == time.July
}
// IsAugust whether is August.
@ -138,7 +138,7 @@ func (c Carbon) IsAugust() bool {
if c.IsInvalid() {
return false
}
return c.Time.In(c.Loc).Month() == time.August
return c.Time.In(c.loc).Month() == time.August
}
// IsSeptember whether is September.
@ -147,7 +147,7 @@ func (c Carbon) IsSeptember() bool {
if c.IsInvalid() {
return false
}
return c.Time.In(c.Loc).Month() == time.September
return c.Time.In(c.loc).Month() == time.September
}
// IsOctober whether is October.
@ -156,7 +156,7 @@ func (c Carbon) IsOctober() bool {
if c.IsInvalid() {
return false
}
return c.Time.In(c.Loc).Month() == time.October
return c.Time.In(c.loc).Month() == time.October
}
// IsNovember whether is November.
@ -165,7 +165,7 @@ func (c Carbon) IsNovember() bool {
if c.IsInvalid() {
return false
}
return c.Time.In(c.Loc).Month() == time.November
return c.Time.In(c.loc).Month() == time.November
}
// IsDecember whether is December.
@ -174,7 +174,7 @@ func (c Carbon) IsDecember() bool {
if c.IsInvalid() {
return false
}
return c.Time.In(c.Loc).Month() == time.December
return c.Time.In(c.loc).Month() == time.December
}
// IsMonday whether is Monday.
@ -183,7 +183,7 @@ func (c Carbon) IsMonday() bool {
if c.IsInvalid() {
return false
}
return c.Time.In(c.Loc).Weekday() == time.Monday
return c.Time.In(c.loc).Weekday() == time.Monday
}
// IsTuesday whether is Tuesday.
@ -192,7 +192,7 @@ func (c Carbon) IsTuesday() bool {
if c.IsInvalid() {
return false
}
return c.Time.In(c.Loc).Weekday() == time.Tuesday
return c.Time.In(c.loc).Weekday() == time.Tuesday
}
// IsWednesday whether is Wednesday.
@ -201,7 +201,7 @@ func (c Carbon) IsWednesday() bool {
if c.IsInvalid() {
return false
}
return c.Time.In(c.Loc).Weekday() == time.Wednesday
return c.Time.In(c.loc).Weekday() == time.Wednesday
}
// IsThursday whether is Thursday.
@ -210,7 +210,7 @@ func (c Carbon) IsThursday() bool {
if c.IsInvalid() {
return false
}
return c.Time.In(c.Loc).Weekday() == time.Thursday
return c.Time.In(c.loc).Weekday() == time.Thursday
}
// IsFriday whether is Friday.
@ -219,7 +219,7 @@ func (c Carbon) IsFriday() bool {
if c.IsInvalid() {
return false
}
return c.Time.In(c.Loc).Weekday() == time.Friday
return c.Time.In(c.loc).Weekday() == time.Friday
}
// IsSaturday whether is Saturday.
@ -228,7 +228,7 @@ func (c Carbon) IsSaturday() bool {
if c.IsInvalid() {
return false
}
return c.Time.In(c.Loc).Weekday() == time.Saturday
return c.Time.In(c.loc).Weekday() == time.Saturday
}
// IsSunday whether is Sunday.
@ -237,7 +237,7 @@ func (c Carbon) IsSunday() bool {
if c.IsInvalid() {
return false
}
return c.Time.In(c.Loc).Weekday() == time.Sunday
return c.Time.In(c.loc).Weekday() == time.Sunday
}
// IsWeekday whether is weekday.
@ -291,9 +291,7 @@ func (c Carbon) Compare(operator string, t Carbon) bool {
switch operator {
case "=":
return c.Eq(t)
case "<>":
return !c.Eq(t)
case "!=":
case "<>", "!=":
return !c.Eq(t)
case ">":
return c.Gt(t)

View File

@ -4,52 +4,51 @@ import (
"bytes"
"strconv"
"strings"
"time"
)
// ToTimestamp outputs a timestamp with second, it is short for ToTimestampWithSecond.
// 输出秒级时间戳, 是 ToTimestampWithSecond 的简写
// ToTimestamp outputs a timestamp with second(will be removed from v2.0, only keep Timestamp).
// 输出秒级时间戳(将在v2.0版本移除,只保留 Timestamp)
func (c Carbon) ToTimestamp() int64 {
if c.IsInvalid() {
return 0
}
return c.ToTimestampWithSecond()
return c.Timestamp()
}
// ToTimestampWithSecond outputs a timestamp with second.
// 输出秒级时间戳
// ToTimestampWithSecond outputs a timestamp with second(will be removed from v2.0, only keep TimestampWithSecond).
// 输出秒级时间戳(将在v2.0版本移除,只保留 TimestampWithSecond)
func (c Carbon) ToTimestampWithSecond() int64 {
if c.IsInvalid() {
return 0
}
return c.Time.In(c.Loc).Unix()
return c.TimestampWithSecond()
}
// ToTimestampWithMillisecond outputs a timestamp with millisecond.
// 输出毫秒级时间戳
// ToTimestampWithMillisecond outputs a timestamp with millisecond(will be removed from v2.0, only keep TimestampWithMillisecond).
// 输出毫秒级时间戳(将在v2.0版本移除,只保留 TimestampWithMillisecond)
func (c Carbon) ToTimestampWithMillisecond() int64 {
if c.IsInvalid() {
return 0
}
return c.Time.UnixNano() / int64(time.Millisecond)
return c.TimestampWithMillisecond()
}
// ToTimestampWithMicrosecond outputs a timestamp with microsecond.
// 输出微秒级时间戳
// ToTimestampWithMicrosecond outputs a timestamp with microsecond(will be removed from v2.0, only keep TimestampWithMicrosecond).
// 输出微秒级时间戳(将在v2.0版本移除,只保留 TimestampWithMicrosecond)
func (c Carbon) ToTimestampWithMicrosecond() int64 {
if c.IsInvalid() {
return 0
}
return c.Time.UnixNano() / int64(time.Microsecond)
return c.TimestampWithMicrosecond()
}
// ToTimestampWithNanosecond outputs a timestamp with nanosecond.
// 输出纳秒级时间戳
// ToTimestampWithNanosecond outputs a timestamp with nanosecond(will be removed from v2.0, only keep TimestampWithNanosecond).
// 输出纳秒级时间戳(将在v2.0版本移除,只保留 TimestampWithNanosecond)
func (c Carbon) ToTimestampWithNanosecond() int64 {
if c.IsInvalid() {
return 0
}
return c.Time.UnixNano()
return c.TimestampWithNanosecond()
}
// String outputs a string in date and time format, implement Stringer interface.
@ -65,31 +64,27 @@ func (c Carbon) String() string {
// 输出"2006-01-02 15:04:05.999999999 -0700 MST"格式字符串
func (c Carbon) ToString(timezone ...string) string {
if len(timezone) > 0 {
loc, err := getLocationByTimezone(timezone[len(timezone)-1])
c.Loc = loc
c.Error = err
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Time.In(c.Loc).String()
return c.Time.In(c.loc).String()
}
// ToMonthString outputs a string in month format, i18n is supported.
// 输出完整月份字符串支持i18n
func (c Carbon) ToMonthString(timezone ...string) string {
if len(timezone) > 0 {
loc, err := getLocationByTimezone(timezone[len(timezone)-1])
c.Loc = loc
c.Error = err
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
if len(c.Lang.resources) == 0 {
c.Lang.SetLocale(defaultLocale)
if len(c.lang.resources) == 0 {
c.lang.SetLocale(defaultLocale)
}
if months, ok := c.Lang.resources["months"]; ok {
if months, ok := c.lang.resources["months"]; ok {
slice := strings.Split(months, "|")
if len(slice) == 12 {
return slice[c.Month()-1]
@ -102,17 +97,15 @@ func (c Carbon) ToMonthString(timezone ...string) string {
// 输出缩写月份字符串支持i18n
func (c Carbon) ToShortMonthString(timezone ...string) string {
if len(timezone) > 0 {
loc, err := getLocationByTimezone(timezone[len(timezone)-1])
c.Loc = loc
c.Error = err
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
if len(c.Lang.resources) == 0 {
c.Lang.SetLocale(defaultLocale)
if len(c.lang.resources) == 0 {
c.lang.SetLocale(defaultLocale)
}
if months, ok := c.Lang.resources["short_months"]; ok {
if months, ok := c.lang.resources["short_months"]; ok {
slice := strings.Split(months, "|")
if len(slice) == 12 {
return slice[c.Month()-1]
@ -125,17 +118,15 @@ func (c Carbon) ToShortMonthString(timezone ...string) string {
// 输出完整星期字符串支持i18n
func (c Carbon) ToWeekString(timezone ...string) string {
if len(timezone) > 0 {
loc, err := getLocationByTimezone(timezone[len(timezone)-1])
c.Loc = loc
c.Error = err
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
if len(c.Lang.resources) == 0 {
c.Lang.SetLocale(defaultLocale)
if len(c.lang.resources) == 0 {
c.lang.SetLocale(defaultLocale)
}
if months, ok := c.Lang.resources["weeks"]; ok {
if months, ok := c.lang.resources["weeks"]; ok {
slice := strings.Split(months, "|")
if len(slice) == 7 {
return slice[c.Week()]
@ -148,17 +139,15 @@ func (c Carbon) ToWeekString(timezone ...string) string {
// 输出缩写星期字符串支持i18n
func (c Carbon) ToShortWeekString(timezone ...string) string {
if len(timezone) > 0 {
loc, err := getLocationByTimezone(timezone[len(timezone)-1])
c.Loc = loc
c.Error = err
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
if len(c.Lang.resources) == 0 {
c.Lang.SetLocale(defaultLocale)
if len(c.lang.resources) == 0 {
c.lang.SetLocale(defaultLocale)
}
if months, ok := c.Lang.resources["short_weeks"]; ok {
if months, ok := c.lang.resources["short_weeks"]; ok {
slice := strings.Split(months, "|")
if len(slice) == 7 {
return slice[c.Week()]
@ -171,98 +160,84 @@ func (c Carbon) ToShortWeekString(timezone ...string) string {
// 输出天数日期时间字符串
func (c Carbon) ToDayDateTimeString(timezone ...string) string {
if len(timezone) > 0 {
loc, err := getLocationByTimezone(timezone[len(timezone)-1])
c.Loc = loc
c.Error = err
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Time.In(c.Loc).Format(DayDateTimeFormat)
return c.Time.In(c.loc).Format(DayDateTimeFormat)
}
// ToDateTimeString outputs a string in date and time format.
// 输出日期时间字符串
func (c Carbon) ToDateTimeString(timezone ...string) string {
if len(timezone) > 0 {
loc, err := getLocationByTimezone(timezone[len(timezone)-1])
c.Loc = loc
c.Error = err
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Time.In(c.Loc).Format(DateTimeFormat)
return c.Time.In(c.loc).Format(DateTimeFormat)
}
// ToShortDateTimeString outputs a string in short date and time format.
// 输出简写日期时间字符串
func (c Carbon) ToShortDateTimeString(timezone ...string) string {
if len(timezone) > 0 {
loc, err := getLocationByTimezone(timezone[len(timezone)-1])
c.Loc = loc
c.Error = err
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Time.In(c.Loc).Format(ShortDateTimeFormat)
return c.Time.In(c.loc).Format(ShortDateTimeFormat)
}
// ToDateString outputs a string in date format.
// 输出日期字符串
func (c Carbon) ToDateString(timezone ...string) string {
if len(timezone) > 0 {
loc, err := getLocationByTimezone(timezone[len(timezone)-1])
c.Loc = loc
c.Error = err
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Time.In(c.Loc).Format(DateFormat)
return c.Time.In(c.loc).Format(DateFormat)
}
// ToShortDateString outputs a string in short date format.
// 输出简写日期字符串
func (c Carbon) ToShortDateString(timezone ...string) string {
if len(timezone) > 0 {
loc, err := getLocationByTimezone(timezone[len(timezone)-1])
c.Loc = loc
c.Error = err
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Time.In(c.Loc).Format(ShortDateFormat)
return c.Time.In(c.loc).Format(ShortDateFormat)
}
// ToTimeString outputs a string in time format.
// 输出时间字符串
func (c Carbon) ToTimeString(timezone ...string) string {
if len(timezone) > 0 {
loc, err := getLocationByTimezone(timezone[len(timezone)-1])
c.Loc = loc
c.Error = err
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Time.In(c.Loc).Format(TimeFormat)
return c.Time.In(c.loc).Format(TimeFormat)
}
// ToShortTimeString outputs a string in short time format.
// 输出简写时间字符串
func (c Carbon) ToShortTimeString(timezone ...string) string {
if len(timezone) > 0 {
loc, err := getLocationByTimezone(timezone[len(timezone)-1])
c.Loc = loc
c.Error = err
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Time.In(c.Loc).Format(ShortTimeFormat)
return c.Time.In(c.loc).Format(ShortTimeFormat)
}
// ToAtomString outputs a string in ATOM format.
@ -278,42 +253,36 @@ func (c Carbon) ToAtomString(timezone ...string) string {
// 输出 ANSIC 格式字符串
func (c Carbon) ToAnsicString(timezone ...string) string {
if len(timezone) > 0 {
loc, err := getLocationByTimezone(timezone[len(timezone)-1])
c.Loc = loc
c.Error = err
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Time.In(c.Loc).Format(AnsicFormat)
return c.Time.In(c.loc).Format(AnsicFormat)
}
// ToCookieString outputs a string in COOKIE format.
// 输出 COOKIE 格式字符串
func (c Carbon) ToCookieString(timezone ...string) string {
if len(timezone) > 0 {
loc, err := getLocationByTimezone(timezone[len(timezone)-1])
c.Loc = loc
c.Error = err
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Time.In(c.Loc).Format(CookieFormat)
return c.Time.In(c.loc).Format(CookieFormat)
}
// ToRssString outputs a string in RSS format.
// 输出 RSS 格式字符串
func (c Carbon) ToRssString(timezone ...string) string {
if len(timezone) > 0 {
loc, err := getLocationByTimezone(timezone[len(timezone)-1])
c.Loc = loc
c.Error = err
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Time.In(c.Loc).Format(RssFormat)
return c.Time.In(c.loc).Format(RssFormat)
}
// ToW3cString outputs a string in W3C format.
@ -329,196 +298,168 @@ func (c Carbon) ToW3cString(timezone ...string) string {
// 输出 UnixDate 格式字符串
func (c Carbon) ToUnixDateString(timezone ...string) string {
if len(timezone) > 0 {
loc, err := getLocationByTimezone(timezone[len(timezone)-1])
c.Loc = loc
c.Error = err
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Time.In(c.Loc).Format(UnixDateFormat)
return c.Time.In(c.loc).Format(UnixDateFormat)
}
// ToRubyDateString outputs a string in ruby date format.
// 输出 RubyDate 格式字符串
func (c Carbon) ToRubyDateString(timezone ...string) string {
if len(timezone) > 0 {
loc, err := getLocationByTimezone(timezone[len(timezone)-1])
c.Loc = loc
c.Error = err
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Time.In(c.Loc).Format(RubyDateFormat)
return c.Time.In(c.loc).Format(RubyDateFormat)
}
// ToKitchenString outputs a string in KITCHEN format.
// 输出 KITCHEN 格式字符串
func (c Carbon) ToKitchenString(timezone ...string) string {
if len(timezone) > 0 {
loc, err := getLocationByTimezone(timezone[len(timezone)-1])
c.Loc = loc
c.Error = err
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Time.In(c.Loc).Format(KitchenFormat)
return c.Time.In(c.loc).Format(KitchenFormat)
}
// ToIso8601String outputs a string in ISO8601 format.
// 输出 ISO8601 格式字符串
func (c Carbon) ToIso8601String(timezone ...string) string {
if len(timezone) > 0 {
loc, err := getLocationByTimezone(timezone[len(timezone)-1])
c.Loc = loc
c.Error = err
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Time.In(c.Loc).Format(Iso8601Format)
return c.Time.In(c.loc).Format(Iso8601Format)
}
// ToRfc822String outputs a string in RFC822 format.
// 输出 RFC822 格式字符串
func (c Carbon) ToRfc822String(timezone ...string) string {
if len(timezone) > 0 {
loc, err := getLocationByTimezone(timezone[len(timezone)-1])
c.Loc = loc
c.Error = err
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Time.In(c.Loc).Format(RFC822Format)
return c.Time.In(c.loc).Format(RFC822Format)
}
// ToRfc822zString outputs a string in RFC822Z format.
// 输出 RFC822Z 格式字符串
func (c Carbon) ToRfc822zString(timezone ...string) string {
if len(timezone) > 0 {
loc, err := getLocationByTimezone(timezone[len(timezone)-1])
c.Loc = loc
c.Error = err
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Time.In(c.Loc).Format(RFC822ZFormat)
return c.Time.In(c.loc).Format(RFC822ZFormat)
}
// ToRfc850String outputs a string in RFC850 format.
// 输出 RFC850 格式字符串
func (c Carbon) ToRfc850String(timezone ...string) string {
if len(timezone) > 0 {
loc, err := getLocationByTimezone(timezone[len(timezone)-1])
c.Loc = loc
c.Error = err
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Time.In(c.Loc).Format(RFC850Format)
return c.Time.In(c.loc).Format(RFC850Format)
}
// ToRfc1036String outputs a string in RFC1036 format.
// 输出 RFC1036 格式字符串
func (c Carbon) ToRfc1036String(timezone ...string) string {
if len(timezone) > 0 {
loc, err := getLocationByTimezone(timezone[len(timezone)-1])
c.Loc = loc
c.Error = err
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Time.In(c.Loc).Format(RFC1036Format)
return c.Time.In(c.loc).Format(RFC1036Format)
}
// ToRfc1123String outputs a string in RFC1123 format.
// 输出 RFC1123 格式字符串
func (c Carbon) ToRfc1123String(timezone ...string) string {
if len(timezone) > 0 {
loc, err := getLocationByTimezone(timezone[len(timezone)-1])
c.Loc = loc
c.Error = err
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Time.In(c.Loc).Format(RFC1123Format)
return c.Time.In(c.loc).Format(RFC1123Format)
}
// ToRfc1123zString outputs a string in RFC1123z format.
// 输出 RFC1123z 格式字符串
func (c Carbon) ToRfc1123zString(timezone ...string) string {
if len(timezone) > 0 {
loc, err := getLocationByTimezone(timezone[len(timezone)-1])
c.Loc = loc
c.Error = err
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Time.In(c.Loc).Format(RFC1123ZFormat)
return c.Time.In(c.loc).Format(RFC1123ZFormat)
}
// ToRfc2822String outputs a string in RFC2822 format.
// 输出 RFC2822 格式字符串
func (c Carbon) ToRfc2822String(timezone ...string) string {
if len(timezone) > 0 {
loc, err := getLocationByTimezone(timezone[len(timezone)-1])
c.Loc = loc
c.Error = err
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Time.In(c.Loc).Format(RFC2822Format)
return c.Time.In(c.loc).Format(RFC2822Format)
}
// ToRfc3339String outputs a string in RFC3339 format.
// 输出 RFC3339 格式字符串
func (c Carbon) ToRfc3339String(timezone ...string) string {
if len(timezone) > 0 {
loc, err := getLocationByTimezone(timezone[len(timezone)-1])
c.Loc = loc
c.Error = err
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Time.In(c.Loc).Format(RFC3339Format)
return c.Time.In(c.loc).Format(RFC3339Format)
}
// ToRfc7231String outputs a string in RFC7231 format.
// 输出 RFC7231 格式字符串
func (c Carbon) ToRfc7231String(timezone ...string) string {
if len(timezone) > 0 {
loc, err := getLocationByTimezone(timezone[len(timezone)-1])
c.Loc = loc
c.Error = err
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Time.In(c.Loc).Format(RFC7231Format)
return c.Time.In(c.loc).Format(RFC7231Format)
}
// ToLayoutString outputs a string by layout.
// 输出指定布局的时间字符串
func (c Carbon) ToLayoutString(layout string, timezone ...string) string {
if len(timezone) > 0 {
loc, err := getLocationByTimezone(timezone[len(timezone)-1])
c.Loc = loc
c.Error = err
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
return c.Time.In(c.Loc).Format(layout)
return c.Time.In(c.loc).Format(layout)
}
// Layout outputs a string by layout, it is short for ToLayoutString.
@ -534,22 +475,19 @@ func (c Carbon) Layout(layout string, timezone ...string) string {
// 输出指定格式的时间字符串
func (c Carbon) ToFormatString(format string, timezone ...string) string {
if len(timezone) > 0 {
loc, err := getLocationByTimezone(timezone[len(timezone)-1])
c.Loc = loc
c.Error = err
c.loc, c.Error = getLocationByTimezone(timezone[len(timezone)-1])
}
if c.IsInvalid() {
return ""
}
runes := []rune(format)
buffer := bytes.NewBuffer(nil)
for i := 0; i < len(runes); i++ {
if layout, ok := formats[byte(runes[i])]; ok {
buffer.WriteString(c.Time.In(c.Loc).Format(layout))
for i := 0; i < len(format); i++ {
if layout, ok := formats[byte(format[i])]; ok {
buffer.WriteString(c.Time.In(c.loc).Format(layout))
} else {
switch runes[i] {
switch format[i] {
case '\\': // 原样输出,不解析
buffer.WriteRune(runes[i+1])
buffer.WriteByte(format[i+1])
i++
continue
case 'W': // ISO-8601 格式数字表示的年份中的第几周,取值范围 1-52
@ -576,7 +514,7 @@ func (c Carbon) ToFormatString(format string, timezone ...string) string {
case 'G': // 数字表示的小时24 小时格式,没有前导零,取值范围 0-23
buffer.WriteString(strconv.Itoa(c.Hour()))
case 'U': // 秒级时间戳,如 1611818268
buffer.WriteString(strconv.FormatInt(c.ToTimestamp(), 10))
buffer.WriteString(strconv.FormatInt(c.Timestamp(), 10))
case 'u': // 数字表示的毫秒,如 999
buffer.WriteString(strconv.Itoa(c.Millisecond()))
case 'w': // 数字表示的星期中的第几天,取值范围 0-6
@ -592,7 +530,7 @@ func (c Carbon) ToFormatString(format string, timezone ...string) string {
case 'C': // 当前世纪数,取值范围 0-99
buffer.WriteString(strconv.Itoa(c.Century()))
default:
buffer.WriteRune(runes[i])
buffer.WriteByte(format[i])
}
}
}