2021-02-18 14:32:31 +08:00
|
|
|
|
package carbon
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
2023-01-07 21:57:43 +08:00
|
|
|
|
"fmt"
|
2021-02-18 14:32:31 +08:00
|
|
|
|
"strconv"
|
2021-02-23 09:32:55 +08:00
|
|
|
|
"strings"
|
2023-09-06 11:27:45 +08:00
|
|
|
|
"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 接口
|
2021-04-11 07:50:55 +08:00
|
|
|
|
func (c Carbon) String() string {
|
|
|
|
|
return c.ToDateTimeString()
|
|
|
|
|
}
|
|
|
|
|
|
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" 格式字符串
|
2021-07-23 10:59:53 +08:00
|
|
|
|
func (c Carbon) ToString(timezone ...string) string {
|
2021-08-05 19:42:55 +08:00
|
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2021-07-09 15:12:48 +08:00
|
|
|
|
}
|
2021-08-05 19:42:55 +08:00
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2023-09-06 14:34:51 +08:00
|
|
|
|
return c.ToStdTime().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
|
2021-07-23 10:59:53 +08:00
|
|
|
|
func (c Carbon) ToMonthString(timezone ...string) string {
|
2021-08-05 19:42:55 +08:00
|
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2021-07-23 10:59:53 +08:00
|
|
|
|
}
|
2021-08-05 19:42:55 +08:00
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2021-08-16 09:33:08 +08:00
|
|
|
|
if len(c.lang.resources) == 0 {
|
|
|
|
|
c.lang.SetLocale(defaultLocale)
|
2021-02-23 09:32:55 +08:00
|
|
|
|
}
|
2021-08-16 09:33:08 +08:00
|
|
|
|
if months, ok := c.lang.resources["months"]; ok {
|
2021-02-23 09:32:55 +08:00
|
|
|
|
slice := strings.Split(months, "|")
|
2021-08-02 10:23:53 +08:00
|
|
|
|
if len(slice) == 12 {
|
|
|
|
|
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
|
2021-07-23 10:59:53 +08:00
|
|
|
|
func (c Carbon) ToShortMonthString(timezone ...string) string {
|
2021-08-05 19:42:55 +08:00
|
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2021-07-23 10:59:53 +08:00
|
|
|
|
}
|
2021-08-05 19:42:55 +08:00
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2021-08-16 09:33:08 +08:00
|
|
|
|
if len(c.lang.resources) == 0 {
|
|
|
|
|
c.lang.SetLocale(defaultLocale)
|
2021-02-23 09:32:55 +08:00
|
|
|
|
}
|
2021-08-16 09:33:08 +08:00
|
|
|
|
if months, ok := c.lang.resources["short_months"]; ok {
|
2021-02-23 09:32:55 +08:00
|
|
|
|
slice := strings.Split(months, "|")
|
2021-08-02 10:23:53 +08:00
|
|
|
|
if len(slice) == 12 {
|
|
|
|
|
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
|
2021-07-23 10:59:53 +08:00
|
|
|
|
func (c Carbon) ToWeekString(timezone ...string) string {
|
2021-08-05 19:42:55 +08:00
|
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2021-07-23 10:59:53 +08:00
|
|
|
|
}
|
2021-08-05 19:42:55 +08:00
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2021-08-16 09:33:08 +08:00
|
|
|
|
if len(c.lang.resources) == 0 {
|
|
|
|
|
c.lang.SetLocale(defaultLocale)
|
2021-02-23 09:32:55 +08:00
|
|
|
|
}
|
2021-08-16 09:33:08 +08:00
|
|
|
|
if months, ok := c.lang.resources["weeks"]; ok {
|
2021-02-23 09:32:55 +08:00
|
|
|
|
slice := strings.Split(months, "|")
|
2021-08-02 10:23:53 +08:00
|
|
|
|
if len(slice) == 7 {
|
|
|
|
|
return slice[c.Week()]
|
|
|
|
|
}
|
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
|
2021-07-23 10:59:53 +08:00
|
|
|
|
func (c Carbon) ToShortWeekString(timezone ...string) string {
|
2021-08-05 19:42:55 +08:00
|
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2021-07-23 10:59:53 +08:00
|
|
|
|
}
|
2021-08-05 19:42:55 +08:00
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2021-08-16 09:33:08 +08:00
|
|
|
|
if len(c.lang.resources) == 0 {
|
|
|
|
|
c.lang.SetLocale(defaultLocale)
|
2021-02-23 09:32:55 +08:00
|
|
|
|
}
|
2021-08-16 09:33:08 +08:00
|
|
|
|
if months, ok := c.lang.resources["short_weeks"]; ok {
|
2021-02-23 09:32:55 +08:00
|
|
|
|
slice := strings.Split(months, "|")
|
2021-08-02 10:23:53 +08:00
|
|
|
|
if len(slice) == 7 {
|
|
|
|
|
return slice[c.Week()]
|
|
|
|
|
}
|
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" 格式字符串
|
2021-07-23 10:59:53 +08:00
|
|
|
|
func (c Carbon) ToDayDateTimeString(timezone ...string) string {
|
2021-08-05 19:42:55 +08:00
|
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2021-06-04 15:22:17 +08:00
|
|
|
|
}
|
2021-08-05 19:42:55 +08:00
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2023-09-06 14:34:51 +08:00
|
|
|
|
return c.ToStdTime().Format(DayDateTimeLayout)
|
2021-06-04 15:22:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
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" 格式字符串
|
2021-07-23 10:59:53 +08:00
|
|
|
|
func (c Carbon) ToDateTimeString(timezone ...string) string {
|
2021-07-31 13:28:08 +08:00
|
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2021-07-23 10:59:53 +08:00
|
|
|
|
}
|
2021-08-05 19:42:55 +08:00
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2023-09-06 14:34:51 +08:00
|
|
|
|
return c.ToStdTime().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" 格式字符串
|
2022-04-12 17:36:40 +08:00
|
|
|
|
func (c Carbon) ToDateTimeMilliString(timezone ...string) string {
|
|
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2022-04-12 17:36:40 +08:00
|
|
|
|
}
|
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2023-09-06 14:34:51 +08:00
|
|
|
|
return c.ToStdTime().Format(DateTimeMilliLayout)
|
2022-04-12 17:36:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
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" 格式字符串
|
2022-04-12 17:36:40 +08:00
|
|
|
|
func (c Carbon) ToDateTimeMicroString(timezone ...string) string {
|
|
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2022-04-12 17:36:40 +08:00
|
|
|
|
}
|
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2023-09-06 14:34:51 +08:00
|
|
|
|
return c.ToStdTime().Format(DateTimeMicroLayout)
|
2022-04-12 17:36:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
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" 格式字符串
|
2022-04-12 17:36:40 +08:00
|
|
|
|
func (c Carbon) ToDateTimeNanoString(timezone ...string) string {
|
|
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2022-04-12 17:36:40 +08:00
|
|
|
|
}
|
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2023-09-06 14:34:51 +08:00
|
|
|
|
return c.ToStdTime().Format(DateTimeNanoLayout)
|
2022-04-12 17:36:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-24 22:15:44 +08:00
|
|
|
|
// ToShortDateTimeString outputs a string in "20060102150405" layout.
|
2022-04-14 10:43:07 +08:00
|
|
|
|
// 输出 "20060102150405" 格式字符串
|
2021-07-23 10:59:53 +08:00
|
|
|
|
func (c Carbon) ToShortDateTimeString(timezone ...string) string {
|
2021-07-31 13:28:08 +08:00
|
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2021-07-23 10:59:53 +08:00
|
|
|
|
}
|
2021-08-05 19:42:55 +08:00
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2023-09-06 14:34:51 +08:00
|
|
|
|
return c.ToStdTime().Format(ShortDateTimeLayout)
|
2021-06-04 15:22:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
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" 格式字符串
|
2022-04-12 17:36:40 +08:00
|
|
|
|
func (c Carbon) ToShortDateTimeMilliString(timezone ...string) string {
|
|
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2022-04-12 17:36:40 +08:00
|
|
|
|
}
|
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2023-09-06 14:34:51 +08:00
|
|
|
|
return c.ToStdTime().Format(ShortDateTimeMilliLayout)
|
2022-04-12 17:36:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
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" 格式字符串
|
2022-04-12 17:36:40 +08:00
|
|
|
|
func (c Carbon) ToShortDateTimeMicroString(timezone ...string) string {
|
|
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2022-04-12 17:36:40 +08:00
|
|
|
|
}
|
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2023-09-06 14:34:51 +08:00
|
|
|
|
return c.ToStdTime().Format(ShortDateTimeMicroLayout)
|
2022-04-12 17:36:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
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" 格式字符串
|
2022-04-12 17:36:40 +08:00
|
|
|
|
func (c Carbon) ToShortDateTimeNanoString(timezone ...string) string {
|
|
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2022-04-12 17:36:40 +08:00
|
|
|
|
}
|
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2023-09-06 14:34:51 +08:00
|
|
|
|
return c.ToStdTime().Format(ShortDateTimeNanoLayout)
|
2022-04-12 17:36:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
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" 格式字符串
|
2021-07-23 10:59:53 +08:00
|
|
|
|
func (c Carbon) ToDateString(timezone ...string) string {
|
2021-07-31 13:28:08 +08:00
|
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2021-07-23 10:59:53 +08:00
|
|
|
|
}
|
2021-08-05 19:42:55 +08:00
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2023-09-06 14:34:51 +08:00
|
|
|
|
return c.ToStdTime().Format(DateLayout)
|
2021-02-18 14:32:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-01 09:08:05 +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 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2022-05-01 09:08:05 +08:00
|
|
|
|
}
|
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2023-09-06 14:34:51 +08:00
|
|
|
|
return c.ToStdTime().Format(DateMilliLayout)
|
2022-05-01 09:08:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2022-05-01 09:08:05 +08:00
|
|
|
|
}
|
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2023-09-06 14:34:51 +08:00
|
|
|
|
return c.ToStdTime().Format(DateMicroLayout)
|
2022-05-01 09:08:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2022-05-01 09:08:05 +08:00
|
|
|
|
}
|
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2023-09-06 14:34:51 +08:00
|
|
|
|
return c.ToStdTime().Format(DateNanoLayout)
|
2022-05-01 09:08:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-24 22:15:44 +08:00
|
|
|
|
// ToShortDateString outputs a string in "20060102" layout.
|
2022-04-14 10:43:07 +08:00
|
|
|
|
// 输出 "20060102" 格式字符串
|
2021-07-23 10:59:53 +08:00
|
|
|
|
func (c Carbon) ToShortDateString(timezone ...string) string {
|
2021-07-31 13:28:08 +08:00
|
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2021-07-23 10:59:53 +08:00
|
|
|
|
}
|
2021-08-05 19:42:55 +08:00
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2023-09-06 14:34:51 +08:00
|
|
|
|
return c.ToStdTime().Format(ShortDateLayout)
|
2021-06-04 15:22:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-01 09:08:05 +08:00
|
|
|
|
// ToShortDateMilliString outputs a string in "20060102.999" layout.
|
|
|
|
|
// 输出 "20060102.999" 格式字符串
|
|
|
|
|
func (c Carbon) ToShortDateMilliString(timezone ...string) string {
|
|
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2022-05-01 09:08:05 +08:00
|
|
|
|
}
|
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2023-09-06 14:34:51 +08:00
|
|
|
|
return c.ToStdTime().Format(ShortDateMilliLayout)
|
2022-05-01 09:08:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ToShortDateMicroString outputs a string in "20060102.999999" layout.
|
|
|
|
|
// 输出 "20060102.999999" 格式字符串
|
|
|
|
|
func (c Carbon) ToShortDateMicroString(timezone ...string) string {
|
|
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2022-05-01 09:08:05 +08:00
|
|
|
|
}
|
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2023-09-06 14:34:51 +08:00
|
|
|
|
return c.ToStdTime().Format(ShortDateMicroLayout)
|
2022-05-01 09:08:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ToShortDateNanoString outputs a string in "20060102.999999999" layout.
|
|
|
|
|
// 输出 "20060102.999999999" 格式字符串
|
|
|
|
|
func (c Carbon) ToShortDateNanoString(timezone ...string) string {
|
|
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2022-05-01 09:08:05 +08:00
|
|
|
|
}
|
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2023-09-06 14:34:51 +08:00
|
|
|
|
return c.ToStdTime().Format(ShortDateNanoLayout)
|
2022-05-01 09:08:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
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" 格式字符串
|
2021-07-23 10:59:53 +08:00
|
|
|
|
func (c Carbon) ToTimeString(timezone ...string) string {
|
2021-07-31 13:28:08 +08:00
|
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2021-07-23 10:59:53 +08:00
|
|
|
|
}
|
2021-08-05 19:42:55 +08:00
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2023-09-06 14:34:51 +08:00
|
|
|
|
return c.ToStdTime().Format(TimeLayout)
|
2021-02-18 14:32:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-01 09:08:05 +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 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2022-05-01 09:08:05 +08:00
|
|
|
|
}
|
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2023-09-06 14:34:51 +08:00
|
|
|
|
return c.ToStdTime().Format(TimeMilliLayout)
|
2022-05-01 09:08:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2022-05-01 09:08:05 +08:00
|
|
|
|
}
|
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2023-09-06 14:34:51 +08:00
|
|
|
|
return c.ToStdTime().Format(TimeMicroLayout)
|
2022-05-01 09:08:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2022-05-01 09:08:05 +08:00
|
|
|
|
}
|
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2023-09-06 14:34:51 +08:00
|
|
|
|
return c.ToStdTime().Format(TimeNanoLayout)
|
2022-05-01 09:08:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-24 22:15:44 +08:00
|
|
|
|
// ToShortTimeString outputs a string in "150405" layout.
|
2022-04-14 10:43:07 +08:00
|
|
|
|
// 输出 "150405" 格式字符串
|
2021-07-23 10:59:53 +08:00
|
|
|
|
func (c Carbon) ToShortTimeString(timezone ...string) string {
|
2021-07-31 13:28:08 +08:00
|
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2021-07-09 15:12:48 +08:00
|
|
|
|
}
|
2021-08-05 19:42:55 +08:00
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2023-09-06 14:34:51 +08:00
|
|
|
|
return c.ToStdTime().Format(ShortTimeLayout)
|
2021-02-18 14:32:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-01 09:08:05 +08:00
|
|
|
|
// ToShortTimeMilliString outputs a string in "150405.999" layout.
|
|
|
|
|
// 输出 "150405.999" 格式字符串
|
|
|
|
|
func (c Carbon) ToShortTimeMilliString(timezone ...string) string {
|
|
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2022-05-01 09:08:05 +08:00
|
|
|
|
}
|
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2023-09-06 14:34:51 +08:00
|
|
|
|
return c.ToStdTime().Format(ShortTimeMilliLayout)
|
2022-05-01 09:08:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ToShortTimeMicroString outputs a string in "150405.999999" layout.
|
|
|
|
|
// 输出 "150405.999999" 格式字符串
|
|
|
|
|
func (c Carbon) ToShortTimeMicroString(timezone ...string) string {
|
|
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2022-05-01 09:08:05 +08:00
|
|
|
|
}
|
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2023-09-06 14:34:51 +08:00
|
|
|
|
return c.ToStdTime().Format(ShortTimeMicroLayout)
|
2022-05-01 09:08:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ToShortTimeNanoString outputs a string in "150405.999999999" layout.
|
|
|
|
|
// 输出 "150405.999999999" 格式字符串
|
|
|
|
|
func (c Carbon) ToShortTimeNanoString(timezone ...string) string {
|
|
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2022-05-01 09:08:05 +08:00
|
|
|
|
}
|
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2023-09-06 14:34:51 +08:00
|
|
|
|
return c.ToStdTime().Format(ShortTimeNanoLayout)
|
2022-05-01 09:08:05 +08:00
|
|
|
|
}
|
|
|
|
|
|
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" 格式字符串
|
2021-07-23 10:59:53 +08:00
|
|
|
|
func (c Carbon) ToAtomString(timezone ...string) string {
|
|
|
|
|
return c.ToRfc3339String(timezone...)
|
2021-06-04 15:22:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-24 22:15:44 +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" 格式字符串
|
2022-04-14 22:43:59 +08:00
|
|
|
|
func (c Carbon) ToANSICString(timezone ...string) string {
|
2021-07-31 13:28:08 +08:00
|
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2021-07-23 10:59:53 +08:00
|
|
|
|
}
|
2021-08-05 19:42:55 +08:00
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2023-09-06 14:34:51 +08:00
|
|
|
|
return c.ToStdTime().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" 格式字符串
|
2021-07-23 10:59:53 +08:00
|
|
|
|
func (c Carbon) ToCookieString(timezone ...string) string {
|
2021-07-31 13:28:08 +08:00
|
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2021-02-18 14:32:31 +08:00
|
|
|
|
}
|
2021-08-05 19:42:55 +08:00
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2023-09-06 14:34:51 +08:00
|
|
|
|
return c.ToStdTime().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" 格式字符串
|
2021-07-23 10:59:53 +08:00
|
|
|
|
func (c Carbon) ToRssString(timezone ...string) string {
|
2021-07-31 13:28:08 +08:00
|
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2021-02-18 14:32:31 +08:00
|
|
|
|
}
|
2021-08-05 19:42:55 +08:00
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2023-09-06 14:34:51 +08:00
|
|
|
|
return c.ToStdTime().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" 格式字符串
|
2021-07-23 10:59:53 +08:00
|
|
|
|
func (c Carbon) ToW3cString(timezone ...string) string {
|
|
|
|
|
return c.ToRfc3339String(timezone...)
|
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" 格式字符串
|
2021-07-23 10:59:53 +08:00
|
|
|
|
func (c Carbon) ToUnixDateString(timezone ...string) string {
|
2021-07-31 13:28:08 +08:00
|
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2021-02-18 14:32:31 +08:00
|
|
|
|
}
|
2021-08-05 19:42:55 +08:00
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2023-09-06 14:34:51 +08:00
|
|
|
|
return c.ToStdTime().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" 格式字符串
|
2021-07-23 10:59:53 +08:00
|
|
|
|
func (c Carbon) ToRubyDateString(timezone ...string) string {
|
2021-07-31 13:28:08 +08:00
|
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2021-02-18 14:32:31 +08:00
|
|
|
|
}
|
2021-08-05 19:42:55 +08:00
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2023-09-06 14:34:51 +08:00
|
|
|
|
return c.ToStdTime().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" 格式字符串
|
2021-07-23 10:59:53 +08:00
|
|
|
|
func (c Carbon) ToKitchenString(timezone ...string) string {
|
2021-07-31 13:28:08 +08:00
|
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2021-02-18 14:32:31 +08:00
|
|
|
|
}
|
2021-08-05 19:42:55 +08:00
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2023-09-06 14:34:51 +08:00
|
|
|
|
return c.ToStdTime().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" 格式字符串
|
2021-07-23 10:59:53 +08:00
|
|
|
|
func (c Carbon) ToIso8601String(timezone ...string) string {
|
2021-07-31 13:28:08 +08:00
|
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2021-07-23 10:59:53 +08:00
|
|
|
|
}
|
2021-08-05 19:42:55 +08:00
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2023-09-06 14:34:51 +08:00
|
|
|
|
return c.ToStdTime().Format(ISO8601Layout)
|
2021-06-04 15:22:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-24 22:15:44 +08:00
|
|
|
|
// ToIso8601MilliString outputs a string in "2006-01-02T15:04:05.999-07:00" layout.
|
2022-04-14 22:43:59 +08:00
|
|
|
|
// 输出 "2006-01-02T15:04:05.999-07:00" 格式字符串
|
|
|
|
|
func (c Carbon) ToIso8601MilliString(timezone ...string) string {
|
|
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2022-04-14 22:43:59 +08:00
|
|
|
|
}
|
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2023-09-06 14:34:51 +08:00
|
|
|
|
return c.ToStdTime().Format(ISO8601MilliLayout)
|
2022-04-14 22:43:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-24 22:15:44 +08:00
|
|
|
|
// ToIso8601MicroString outputs a string in "2006-01-02T15:04:05.999999-07:00" layout.
|
2022-04-14 22:43:59 +08:00
|
|
|
|
// 输出 "2006-01-02T15:04:05.999999-07:00" 格式字符串
|
|
|
|
|
func (c Carbon) ToIso8601MicroString(timezone ...string) string {
|
|
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2022-04-14 22:43:59 +08:00
|
|
|
|
}
|
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2023-09-06 14:34:51 +08:00
|
|
|
|
return c.ToStdTime().Format(ISO8601MicroLayout)
|
2022-04-14 22:43:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-24 22:15:44 +08:00
|
|
|
|
// ToIso8601NanoString outputs a string in "2006-01-02T15:04:05.999999999-07:00" layout.
|
2022-04-14 22:43:59 +08:00
|
|
|
|
// 输出 "2006-01-02T15:04:05.999999999-07:00" 格式字符串
|
|
|
|
|
func (c Carbon) ToIso8601NanoString(timezone ...string) string {
|
|
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2022-04-14 22:43:59 +08:00
|
|
|
|
}
|
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2023-09-06 14:34:51 +08:00
|
|
|
|
return c.ToStdTime().Format(ISO8601NanoLayout)
|
2022-04-14 22:43:59 +08:00
|
|
|
|
}
|
|
|
|
|
|
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" 格式字符串
|
2021-07-23 10:59:53 +08:00
|
|
|
|
func (c Carbon) ToRfc822String(timezone ...string) string {
|
2021-07-31 13:28:08 +08:00
|
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2021-07-23 10:59:53 +08:00
|
|
|
|
}
|
2021-08-05 19:42:55 +08:00
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2023-09-06 14:34:51 +08:00
|
|
|
|
return c.ToStdTime().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" 格式字符串
|
2021-07-23 10:59:53 +08:00
|
|
|
|
func (c Carbon) ToRfc822zString(timezone ...string) string {
|
2021-07-31 13:28:08 +08:00
|
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2021-02-18 14:32:31 +08:00
|
|
|
|
}
|
2021-08-05 19:42:55 +08:00
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2023-09-06 14:34:51 +08:00
|
|
|
|
return c.ToStdTime().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" 格式字符串
|
2021-07-23 10:59:53 +08:00
|
|
|
|
func (c Carbon) ToRfc850String(timezone ...string) string {
|
2021-07-31 13:28:08 +08:00
|
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2021-02-18 14:32:31 +08:00
|
|
|
|
}
|
2021-08-05 19:42:55 +08:00
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2023-09-06 14:34:51 +08:00
|
|
|
|
return c.ToStdTime().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" 格式字符串
|
2021-07-23 10:59:53 +08:00
|
|
|
|
func (c Carbon) ToRfc1036String(timezone ...string) string {
|
2021-07-31 13:28:08 +08:00
|
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2021-02-18 14:32:31 +08:00
|
|
|
|
}
|
2021-08-05 19:42:55 +08:00
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2023-09-06 14:34:51 +08:00
|
|
|
|
return c.ToStdTime().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" 格式字符串
|
2021-07-23 10:59:53 +08:00
|
|
|
|
func (c Carbon) ToRfc1123String(timezone ...string) string {
|
2021-07-31 13:28:08 +08:00
|
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2021-02-18 14:32:31 +08:00
|
|
|
|
}
|
2021-08-05 19:42:55 +08:00
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2023-09-06 14:34:51 +08:00
|
|
|
|
return c.ToStdTime().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" 格式字符串
|
2021-07-28 09:20:33 +08:00
|
|
|
|
func (c Carbon) ToRfc1123zString(timezone ...string) string {
|
2021-07-31 13:28:08 +08:00
|
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2021-02-18 14:32:31 +08:00
|
|
|
|
}
|
2021-08-05 19:42:55 +08:00
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2023-09-06 14:34:51 +08:00
|
|
|
|
return c.ToStdTime().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" 格式字符串
|
2021-07-23 10:59:53 +08:00
|
|
|
|
func (c Carbon) ToRfc2822String(timezone ...string) string {
|
2021-07-31 13:28:08 +08:00
|
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2021-07-23 10:59:53 +08:00
|
|
|
|
}
|
2021-08-05 19:42:55 +08:00
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2023-09-06 14:34:51 +08:00
|
|
|
|
return c.ToStdTime().Format(RFC2822Layout)
|
2021-06-04 15:22:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
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" 格式字符串
|
2021-07-23 10:59:53 +08:00
|
|
|
|
func (c Carbon) ToRfc3339String(timezone ...string) string {
|
2021-07-31 13:28:08 +08:00
|
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2021-07-23 10:59:53 +08:00
|
|
|
|
}
|
2021-08-05 19:42:55 +08:00
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2023-09-06 14:34:51 +08:00
|
|
|
|
return c.ToStdTime().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" 格式字符串
|
2022-04-12 17:36:40 +08:00
|
|
|
|
func (c Carbon) ToRfc3339MilliString(timezone ...string) string {
|
|
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2022-04-12 17:36:40 +08:00
|
|
|
|
}
|
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2023-09-06 14:34:51 +08:00
|
|
|
|
return c.ToStdTime().Format(RFC3339MilliLayout)
|
2022-04-12 17:36:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
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" 格式字符串
|
2022-04-12 17:36:40 +08:00
|
|
|
|
func (c Carbon) ToRfc3339MicroString(timezone ...string) string {
|
|
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2022-04-12 17:36:40 +08:00
|
|
|
|
}
|
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2023-09-06 14:34:51 +08:00
|
|
|
|
return c.ToStdTime().Format(RFC3339MicroLayout)
|
2022-04-12 17:36:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
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" 格式字符串
|
2022-04-12 17:36:40 +08:00
|
|
|
|
func (c Carbon) ToRfc3339NanoString(timezone ...string) string {
|
|
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2022-04-12 17:36:40 +08:00
|
|
|
|
}
|
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2023-09-06 14:34:51 +08:00
|
|
|
|
return c.ToStdTime().Format(RFC3339NanoLayout)
|
2022-04-12 17:36:40 +08:00
|
|
|
|
}
|
|
|
|
|
|
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" 格式字符串
|
2021-07-23 10:59:53 +08:00
|
|
|
|
func (c Carbon) ToRfc7231String(timezone ...string) string {
|
2021-07-31 13:28:08 +08:00
|
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2021-07-23 10:59:53 +08:00
|
|
|
|
}
|
2021-08-05 19:42:55 +08:00
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2023-09-06 14:34:51 +08:00
|
|
|
|
return c.ToStdTime().Format(RFC7231Layout)
|
2021-06-04 15:22:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-10 10:50:27 +08:00
|
|
|
|
// ToLayoutString outputs a string by layout.
|
2022-05-01 09:08:05 +08:00
|
|
|
|
// 输出指定布局模板的时间字符串
|
2021-07-23 10:59:53 +08:00
|
|
|
|
func (c Carbon) ToLayoutString(layout string, timezone ...string) string {
|
2021-07-31 13:28:08 +08:00
|
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2021-07-23 10:59:53 +08:00
|
|
|
|
}
|
2021-08-05 19:42:55 +08:00
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2023-09-06 14:34:51 +08:00
|
|
|
|
return c.ToStdTime().Format(layout)
|
2021-02-18 14:32:31 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-10 14:06:53 +08:00
|
|
|
|
// Layout outputs a string by layout, it is shorthand for ToLayoutString.
|
2022-05-01 09:08:05 +08:00
|
|
|
|
// 输出指定布局模板的时间字符串, 是 ToLayoutString 的简写
|
2021-07-23 10:59:53 +08:00
|
|
|
|
func (c Carbon) Layout(layout string, timezone ...string) string {
|
|
|
|
|
return c.ToLayoutString(layout, timezone...)
|
2021-06-04 15:22:17 +08:00
|
|
|
|
}
|
|
|
|
|
|
2021-08-10 10:50:27 +08:00
|
|
|
|
// ToFormatString outputs a string by format.
|
2022-05-01 09:08:05 +08:00
|
|
|
|
// 输出指定格式模板的时间字符串
|
2021-07-23 10:59:53 +08:00
|
|
|
|
func (c Carbon) ToFormatString(format string, timezone ...string) string {
|
2021-07-31 13:28:08 +08:00
|
|
|
|
if len(timezone) > 0 {
|
2023-11-30 15:06:56 +08:00
|
|
|
|
c.loc, c.Error = getLocationByTimezone(timezone[0])
|
2021-07-23 10:59:53 +08:00
|
|
|
|
}
|
2021-08-05 19:42:55 +08:00
|
|
|
|
if c.IsInvalid() {
|
|
|
|
|
return ""
|
|
|
|
|
}
|
2021-07-23 10:59:53 +08:00
|
|
|
|
buffer := bytes.NewBuffer(nil)
|
2021-08-16 09:33:08 +08:00
|
|
|
|
for i := 0; i < len(format); i++ {
|
2022-04-12 17:36:40 +08:00
|
|
|
|
if layout, ok := formats[format[i]]; ok {
|
2022-05-21 00:33:29 +08:00
|
|
|
|
// support for i18n specific symbols
|
|
|
|
|
switch format[i] {
|
2022-05-24 07:08:42 +08:00
|
|
|
|
case 'l': // week, such as Monday
|
2022-05-21 00:33:29 +08:00
|
|
|
|
buffer.WriteString(c.ToWeekString())
|
2022-05-24 07:08:42 +08:00
|
|
|
|
case 'D': // short week, such as Mon
|
2022-05-21 00:33:29 +08:00
|
|
|
|
buffer.WriteString(c.ToShortWeekString())
|
2022-05-24 07:08:42 +08:00
|
|
|
|
case 'F': // month, such as January
|
2022-05-21 00:33:29 +08:00
|
|
|
|
buffer.WriteString(c.ToMonthString())
|
2022-05-24 07:08:42 +08:00
|
|
|
|
case 'M': // short month, such as Jan
|
2022-05-21 00:33:29 +08:00
|
|
|
|
buffer.WriteString(c.ToShortMonthString())
|
2022-05-24 07:08:42 +08:00
|
|
|
|
default: // common symbols
|
2023-09-06 14:34:51 +08:00
|
|
|
|
buffer.WriteString(c.ToStdTime().Format(layout))
|
2022-05-21 00:33:29 +08:00
|
|
|
|
}
|
2021-07-23 10:59:53 +08:00
|
|
|
|
} else {
|
2021-08-16 09:33:08 +08:00
|
|
|
|
switch format[i] {
|
2022-05-21 00:33:29 +08:00
|
|
|
|
case '\\': // raw output, no parse
|
2021-08-16 09:33:08 +08:00
|
|
|
|
buffer.WriteByte(format[i+1])
|
2021-07-23 10:59:53 +08:00
|
|
|
|
i++
|
|
|
|
|
continue
|
2023-01-07 21:57:43 +08:00
|
|
|
|
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)
|
2022-05-21 00:53:35 +08:00
|
|
|
|
case 'S': // abbreviated suffix for the day of the month, such as st, nd, rd, th
|
2021-07-23 10:59:53 +08:00
|
|
|
|
suffix := "th"
|
|
|
|
|
switch c.Day() {
|
|
|
|
|
case 1, 21, 31:
|
|
|
|
|
suffix = "st"
|
|
|
|
|
case 2, 22:
|
|
|
|
|
suffix = "nd"
|
|
|
|
|
case 3, 23:
|
|
|
|
|
suffix = "rd"
|
|
|
|
|
}
|
|
|
|
|
buffer.WriteString(suffix)
|
2022-05-21 00:33:29 +08:00
|
|
|
|
case 'L': // whether it is a leap year, if it is a leap year, it is 1, otherwise it is 0
|
2021-07-23 10:59:53 +08:00
|
|
|
|
if c.IsLeapYear() {
|
|
|
|
|
buffer.WriteString("1")
|
|
|
|
|
} else {
|
|
|
|
|
buffer.WriteString("0")
|
|
|
|
|
}
|
2022-05-24 07:08:42 +08:00
|
|
|
|
case 'G': // 24-hour format, no padding, ranging from 0-23
|
2021-07-23 10:59:53 +08:00
|
|
|
|
buffer.WriteString(strconv.Itoa(c.Hour()))
|
2022-05-21 00:33:29 +08:00
|
|
|
|
case 'U': // timestamp with second, such as 1611818268
|
2021-08-16 09:33:08 +08:00
|
|
|
|
buffer.WriteString(strconv.FormatInt(c.Timestamp(), 10))
|
2022-05-21 00:33:29 +08:00
|
|
|
|
case 'u': // current millisecond, such as 999
|
2021-07-23 10:59:53 +08:00
|
|
|
|
buffer.WriteString(strconv.Itoa(c.Millisecond()))
|
2022-05-21 01:02:56 +08:00
|
|
|
|
case 'w': // day of the week represented by the number, ranging from 0-6
|
2021-07-23 10:59:53 +08:00
|
|
|
|
buffer.WriteString(strconv.Itoa(c.DayOfWeek() - 1))
|
2022-05-21 01:02:56 +08:00
|
|
|
|
case 't': // number of days in the month, ranging from 28-31
|
2021-07-23 10:59:53 +08:00
|
|
|
|
buffer.WriteString(strconv.Itoa(c.DaysInMonth()))
|
2022-05-24 07:08:42 +08:00
|
|
|
|
case 'z': // day of the year, ranging from 0-365
|
2021-07-23 10:59:53 +08:00
|
|
|
|
buffer.WriteString(strconv.Itoa(c.DayOfYear() - 1))
|
2022-05-21 00:33:29 +08:00
|
|
|
|
case 'e': // current location, such as UTC,GMT,Atlantic/Azores
|
2021-07-23 10:59:53 +08:00
|
|
|
|
buffer.WriteString(c.Location())
|
2022-05-21 01:02:56 +08:00
|
|
|
|
case 'Q': // current quarter, ranging from 1-4
|
2021-07-23 10:59:53 +08:00
|
|
|
|
buffer.WriteString(strconv.Itoa(c.Quarter()))
|
2022-05-21 01:02:56 +08:00
|
|
|
|
case 'C': // current century, ranging from 0-99
|
2021-07-23 10:59:53 +08:00
|
|
|
|
buffer.WriteString(strconv.Itoa(c.Century()))
|
|
|
|
|
default:
|
2021-08-16 09:33:08 +08:00
|
|
|
|
buffer.WriteByte(format[i])
|
2021-07-23 10:59:53 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return buffer.String()
|
2021-02-18 14:32:31 +08:00
|
|
|
|
}
|
2021-06-04 15:22:17 +08:00
|
|
|
|
|
2021-09-10 14:06:53 +08:00
|
|
|
|
// Format outputs a string by format, it is shorthand for ToFormatString.
|
2022-05-01 09:08:05 +08:00
|
|
|
|
// 输出指定格式模板的时间字符串, 是 ToFormatString 的简写
|
2021-07-23 10:59:53 +08:00
|
|
|
|
func (c Carbon) Format(format string, timezone ...string) string {
|
|
|
|
|
return c.ToFormatString(format, timezone...)
|
2021-06-04 15:22:17 +08:00
|
|
|
|
}
|
2023-09-06 11:27:45 +08:00
|
|
|
|
|
|
|
|
|
// ToStdTime converts Carbon to standard time.Time.
|
|
|
|
|
// 将 Carbon 转换成标准 time.Time
|
|
|
|
|
func (c Carbon) ToStdTime() time.Time {
|
|
|
|
|
return c.time.In(c.loc)
|
|
|
|
|
}
|