now extension-go's time toolbox

Unlike golang C#, Javathis high-level language has rich syntactic sugar for developers to call easily. So this has spawned a lot of open source components, and the use of these third-party components can help us avoid a lot of pits in the development process.

Time processing is a problem that all languages ​​have to face. parseAccording to the string conversion to the date type, tostring()the date type is converted to a customized string.

In actual use, parsethere is an uncomfortable way to use it.

Source code

time1, _ := time.Parse("2006-01-02", "2020-02-22")
fmt.Println(time1)

time2, _ := time.Parse("2006/01/02", "2020/02/23")
fmt.Println(time2)

Different string formats need to be configured with different templates to be parsed normally. Is there a way similar to Datetime.parse("Date string") in C#. Generally, the input format can be recognized.

t, _ := now.Parse("2017/01/02")
fmt.Println(t)

t2, _ := now.Parse("2017-10-02 17:30")
fmt.Println(t2)

Very convenient, no need to remember 2006-01-02 15:04:05 this kind of strange date formatting, I really don’t understand. Now that YYYY-MM-dd is running rampant, why the author designed such a strange date formatting method is hard to understand.

This is just a function of the now toolbox, there are many other additional functions waiting for everyone to explore.

Basic use

import "github.com/jinzhu/now"

time.Now() // 2013-11-18 17:51:49.123456789 Mon

now.BeginningOfMinute()        // 2013-11-18 17:51:00 Mon
now.BeginningOfHour()          // 2013-11-18 17:00:00 Mon
now.BeginningOfDay()           // 2013-11-18 00:00:00 Mon
now.BeginningOfWeek()          // 2013-11-17 00:00:00 Sun
now.BeginningOfMonth()         // 2013-11-01 00:00:00 Fri
now.BeginningOfQuarter()       // 2013-10-01 00:00:00 Tue
now.BeginningOfYear()          // 2013-01-01 00:00:00 Tue

Set the starting days of the week

It is customary to use Sunday as the first day of the week in foreign countries, and Monday as the first day of the week in China. The setting of now is very simple, just one line of code

now.WeekStartDay = time.Monday // Set Monday as first day, default is Sunday
now.BeginningOfWeek()          // 2013-11-18 00:00:00 Mon

At this time, every day of the week becomes the 18th

"Is there an easier way? I don't want to configure it every time, maybe I forget it sometimes"

"Of course there is"

now.Monday()              // 2013-11-18 00:00:00 Mon
now.Sunday()              // 2013-11-24 00:00:00 Sun (Next Sunday)
now.EndOfSunday()         // 2013-11-24 23:59:59.999999999 Sun (End of next Sunday)

Directly .Monday() .Sunday().

EndOfSunday()It is to get the last second of the day on Sunday, and it is used when the query condition start date <A <end date.

Calculate time based on defined configuration

It is equivalent to defining some basic content through a configuration information, such as input and output format, time zone, and starting day of the week parameters. All subsequent methods called using this instance will be based on this configuration.

The explanation of the following code is:

Define the time string input/output format as "2006-01-02 15:04:05", use Monday as the first day of the week, and use the system's local time zone for the time zone.

location, err := time.LoadLocation("Asia/Shanghai")

myConfig := &now.Config{
	WeekStartDay: time.Monday,
	TimeLocation: location,
	TimeFormats: []string{"2006-01-02 15:04:05"},
}

t := time.Date(2013, 11, 18, 17, 51, 49, 123456789, time.Now().Location()) // // 2013-11-18 17:51:49.123456789 Mon
myConfig.With(t).BeginningOfWeek()         // 2013-11-18 00:00:00 Mon

myConfig.Parse("2002-10-12 22:14:01")     // 2002-10-12 22:14:01
myConfig.Parse("2002-10-12 22:14")        // returns error 'can't parse string as time: 2002-10-12 22:14'

project address

https://github.com/jinzhu/now

Guess you like

Origin blog.csdn.net/HapplyFox/article/details/113932020