go语言之Carbon库

  • carbon是一个时间扩展库,它提供了易于使用接口。
  • 简单示例:

carbon库非常便捷,它完全兼容time.Time类型,实际上它日期时间类型Carbon直接将time.Time内嵌到结构中,所以time.Time方法可直接调用。

// The Carbon type represents a Time instance.
// Provides a simple API extension for Time.
type Carbon struct {
	time.Time
	weekStartsAt time.Weekday
	weekEndsAt time.Weekday
	weekendDays []time.Weekday
	stringFormat string
	Translator *Translator
}
timeTimeLoadLocationtime.Datecarbon

1.时区

使用go语言time,创建时区需要先加载时区。

loc, err := time.LoadLocation("Japan")
if err != nil {
 log.Fatal("failed to load location:", err)
}
d := time.Date(2020, time.July,24,20,0,0,0,loc)
fmt.Println(d) // 2020-07-24 20:00:00 +0900 JST

使用carbon就简单很多

loc, err := carbon.Create(2020,time.July,24,20,0,0,0,"Japan")
if err != nil {
	log.Fatal(err)
}
fmt.Println(loc)// 2020-07-24 20:00:00

2.时间运算

now := carbon.Now()
fmt.Println(now)
fmt.Println("1秒后:", now.AddSecond())
fmt.Println("1分钟后:", now.AddMinute())
fmt.Println("1小时后:", now.AddHour())
fmt.Println("3分钟20秒后:", now.AddMinutes(3).AddSeconds(20))
fmt.Println("2小时30分后:", now.AddHours(2).AddMinutes(30))
fmt.Println("3天2小时后:",now.AddDays(3).AddHours(2))
AddQuarters/AddQuarterAddCenturies/AddCenturyAddWeekdays/AddWeekdayAddWeeks/AddWeek
carbonSub*

3.时间比较

carbon计算两个日期之间相差多少秒,份,小时,天:

vancouver, _ := carbon.Today("Asia/Shanghai")
london, _ := carbon.Today("Asia/Hong_Kong")
// 相差秒数
fmt.Println(vancouver.DiffInSeconds(london, true)) // 0
ottawa, _ := carbon.CreateFromDate(2000, 1, 1, "America/Toronto")
vancouver, _ = carbon.CreateFromDate(2000, 1, 1, "America/Vancouver")
fmt.Println(ottawa.DiffInHours(vancouver, true)) // 3
fmt.Println(ottawa.DiffInHours(vancouver, false)) // 3
fmt.Println(vancouver.DiffInHours(ottawa, false)) // -3
t, _ := carbon.CreateFromDate(2012, 1, 31, "UTC")
fmt.Println(t.DiffInDays(t.AddMonth(), true)) // 31
fmt.Println(t.DiffInDays(t.SubMonth(), false)) // -31

t, _ = carbon.CreateFromDate(2012, 4, 30, "UTC")
fmt.Println(t.DiffInDays(t.AddMonth(), true)) // 30
fmt.Println(t.DiffInDays(t.AddWeek(), true)) // 7

t, _ = carbon.CreateFromTime(10, 1, 1, 0, "UTC")
fmt.Println(t.DiffInMinutes(t.AddSeconds(59), true)) // 0
fmt.Println(t.DiffInMinutes(t.AddSeconds(60), true)) // 1
fmt.Println(t.DiffInMinutes(t.AddSeconds(119), true)) // 1
fmt.Println(t.DiffInMinutes(t.AddSeconds(120), true)) // 2

4.格式化

time.TimeFormat%d/%m/%h

package main

import (
 "fmt"
 "time"
)

func main() {
 t := time.Now()
 fmt.Println(t.Format("2006-01-02 10:00:00"))
}
2006-01-02 10:00:00yyyy-mm-dd hh:mm:ss

为了使用方便,Go 内置了一些标准的时间格式:

/ src/time/format.go
const (
 ANSIC  = "Mon Jan _2 15:04:05 2006"
 UnixDate = "Mon Jan _2 15:04:05 MST 2006"
 RubyDate = "Mon Jan 02 15:04:05 -0700 2006"
 RFC822  = "02 Jan 06 15:04 MST"
 RFC822Z  = "02 Jan 06 15:04 -0700"// RFC822 with numeric zone
 RFC850  = "Monday, 02-Jan-06 15:04:05 MST"
 RFC1123  = "Mon, 02 Jan 2006 15:04:05 MST"
 RFC1123Z = "Mon, 02 Jan 2006 15:04:05 -0700"// RFC1123 with numeric zone
 RFC3339  = "2006-01-02T15:04:05Z07:00"
 RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
 Kitchen  = "3:04PM"
 // Handy time stamps.
 Stamp  = "Jan _2 15:04:05"
 StampMilli = "Jan _2 15:04:05.000"
 StampMicro = "Jan _2 15:04:05.000000"
 StampNano = "Jan _2 15:04:05.000000000"
)
carbon

const (
 DefaultFormat  = "2006-01-02 15:04:05"
 DateFormat   = "2006-01-02"
 FormattedDateFormat = "Jan 2, 2006"
 TimeFormat   = "15:04:05"
 HourMinuteFormat = "15:04"
 HourFormat   = "15"
 DayDateTimeFormat = "Mon, Aug 2, 2006 3:04 PM"
 CookieFormat  = "Monday, 02-Jan-2006 15:04:05 MST"
 RFC822Format  = "Mon, 02 Jan 06 15:04:05 -0700"
 RFC1036Format  = "Mon, 02 Jan 06 15:04:05 -0700"
 RFC2822Format  = "Mon, 02 Jan 2006 15:04:05 -0700"
 RSSFormat   = "Mon, 02 Jan 2006 15:04:05 -0700"
)

5.高级特性

所谓修饰器(modifier)就是对一些特定的时间操作,获取开始和结束时间。如当天、月、季度、年、十年、世纪、周的开始和结束时间,还能获得上一个周二、下一个周一、下一个工作日的时间等等:

t := carbon.Now()
	fmt.Println("今天起始:", t.StartOfDay())
	fmt.Println("今天终止:", t.EndOfDay())
	fmt.Println("本月起始:", t.StartOfMonth())
	fmt.Println("本月终止:", t.EndOfMonth())
	fmt.Println("本年起始:", t.StartOfYear())
	fmt.Println("本年终止:", t.EndOfYear())
	fmt.Println("今年起始日期(年月日时分秒):", t.StartOfDecade())
	fmt.Println("今年终止日期(年月日时分秒):", t.EndOfDecade())
	fmt.Println("本世纪起始日期(年月日时分秒):", t.StartOfCentury())
	fmt.Println("本世纪终止日期(年月日时分秒):", t.EndOfCentury())
	fmt.Println("本周起始日期(年月日时分秒):", t.StartOfWeek())
	fmt.Println("本周终止日期(年月日时分秒):", t.EndOfWeek())

6.自定义工作日和周末

func main() {
	t, err := carbon.Create(2020, 02, 11, 0, 0, 0, 0, "Asia/Shanghai")
	if err != nil {
		log.Fatal(err)
	}

	t.SetWeekStartsAt(time.Sunday)
	t.SetWeekEndsAt(time.Saturday)
	t.SetWeekendDays([]time.Weekday{time.Monday, time.Tuesday, time.Wednesday})

	fmt.Printf("今天是 %s, weekend? %t\n", t.Weekday(), t.IsWeekend())
}