Go language Carbon library
- carbon is a time extension library that provides an easy-to-use interface.
- Simple example:
package main import ( "fmt" "/uniplaces/carbon" "time" ) func main() { // Print the current time now :=().DateTimeString() ("Local time:", now) Japantoday, _ := ("Japan") ("Japan Time:", Japantoday) // tomorrow tomorrow := ().AddDay() (tomorrow) // Last week's Today subweek := ().SubWeek() (subweek) nextOlympics, _ := (2016, ,5,"Europe/London") ("2016 Olympic Games:",nextOlympics) nextOlympics = (4) ("The next Olympic Games:", ()) if ().IsWeekend() { ("A day of rest") } else { ("Working Day") } }
The carbon library is very convenient and is fully compatible with types. In fact, its date-time type Carbon will be embedded directly into the structure, so the method can be called directly.
// The Carbon type represents a Time instance. // Provides a simple API extension for Time. type Carbon struct { weekStartsAt weekEndsAt weekendDays [] stringFormat string Translator *Translator }
Secondly, the creation operation is simplified. Standard librarytime
Create aTime
If the object is not a local or UTC time zone, you need to call it first.LoadLocation
Load the corresponding time zone. Then pass the time zone object toMethod creation.
carbon
You can directly pass the time zone name.
1. Time zone
Using go language time, you need to load the time zone first.
loc, err := ("Japan") if err != nil { ("failed to load location:", err) } d := (2020, ,24,20,0,0,0,loc) (d) // 2020-07-24 20:00:00 +0900 JST
Using carbon is much easier
loc, err := (2020,,24,20,0,0,0,"Japan") if err != nil { (err) } (loc)// 2020-07-24 20:00:00
2. Time operation
now := () (now) ("After 1 second:", ()) ("After 1 minute:", ()) ("After 1 hour:", ()) ("3 minutes and 20 seconds later:", (3).AddSeconds(20)) ("After 2 hours and 30 minutes:", (2).AddMinutes(30)) ("3 days and 2 hours later:",(3).AddHours(2))
- Methods to increase quarters:
AddQuarters/AddQuarter
, plural form introduces a parameter representing multiples, and the singular form multiples are 1; - Methods to add to the century:
AddCenturies/AddCentury
; - How to increase working days:
AddWeekdays/AddWeekday
, this method will skip non-working days; - How to increase the week:
AddWeeks/AddWeek
。
In fact, passing a negative number to the above method means reduction, in additioncarbon
Also provided is a correspondingSub*
method.
3. Time comparison
carbon calculates how many seconds, hours, and days are different between two dates:
vancouver, _ := ("Asia/Shanghai") london, _ := ("Asia/Hong_Kong") // The difference in seconds((london, true)) // 0 ottawa, _ := (2000, 1, 1, "America/Toronto") vancouver, _ = (2000, 1, 1, "America/Vancouver") ((vancouver, true)) // 3 ((vancouver, false)) // 3 ((ottawa, false)) // -3 t, _ := (2012, 1, 31, "UTC") (((), true)) // 31 (((), false)) // -31 t, _ = (2012, 4, 30, "UTC") (((), true)) // 30 (((), true)) // 7 t, _ = (10, 1, 1, 0, "UTC") (((59), true)) // 0 (((60), true)) // 1 (((119), true)) // 1 (((120), true)) // 2
4. Format
We knowProvides a
Format
Methods, compared to other programming languages, use formatters to describe formats (requires memory%d/%m/%h
), Go provides a simpler and more intuitive way to use layout. That is, we pass in a date string to indicate what we want to format. Go will replace the corresponding part of the string with the current time:
package main import ( "fmt" "time" ) func main() { t := () (("2006-01-02 10:00:00")) }
We only need to pass one2006-01-02 10:00:00
Indicates that the format we want isyyyy-mm-dd hh:mm:ss
, saves us from the trouble of memory
For ease of use, Go has built-in standard time formats:
/ src/time/ 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" )
In addition to the above formats,carbon
Some other formats are also available:
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. Advanced Features
The so-called modifier is to obtain the start and end times for some specific time operations. For example, the start and end time of the day, month, quarter, year, ten years, century, week, you can also get the time of the previous Tuesday, the next Monday, the next working day, etc.:
t := () ("Start today:", ()) ("Terminate today:", ()) ("Start this month:", ()) ("End this month:", ()) ("Start this year:", ()) ("End of this year:", ()) ("This year's start date (year, month, day, hour, minute, and second):", ()) ("This year's end date (year, month, day, hour, minute, and second):", ()) ("The start date of this century (year, month, day, hour, minute, and second):", ()) ("End date of this century (year, month, day, hour, minute, second):", ()) ("Start date of the week(Year, month, day, hour, minute, second):", ()) ("End date of this week(Year, month, day, hour, minute, second):", ())
6. Customize weekdays and weekends
func main() { t, err := (2020, 02, 11, 0, 0, 0, 0, "Asia/Shanghai") if err != nil { (err) } () () ([]{, , }) ("Today is %s, weekend? %t\n", (), ()) }
This is the end of this article about Carbon library time processing in Go language. For more related content on Go Carbon library time processing, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!