• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

GuardfileH A D25-Oct-201941

LicenseH A D25-Oct-20191.1 KiB

README.mdH A D25-Oct-20195 KiB

go.modH A D25-Oct-201938

main.goH A D25-Oct-20194.8 KiB

now.goH A D25-Oct-20195.8 KiB

now_test.goH A D25-Oct-201914.9 KiB

wercker.ymlH A D25-Oct-2019354

README.md

1## Now
2
3Now is a time toolkit for golang
4
5[![wercker status](https://app.wercker.com/status/a350da4eae6cb28a35687ba41afb565a/s/master "wercker status")](https://app.wercker.com/project/byKey/a350da4eae6cb28a35687ba41afb565a)
6
7## Install
8
9```
10go get -u github.com/jinzhu/now
11```
12
13## Usage
14
15Calculating time based on current time
16
17```go
18import "github.com/jinzhu/now"
19
20time.Now() // 2013-11-18 17:51:49.123456789 Mon
21
22now.BeginningOfMinute()        // 2013-11-18 17:51:00 Mon
23now.BeginningOfHour()          // 2013-11-18 17:00:00 Mon
24now.BeginningOfDay()           // 2013-11-18 00:00:00 Mon
25now.BeginningOfWeek()          // 2013-11-17 00:00:00 Sun
26now.BeginningOfMonth()         // 2013-11-01 00:00:00 Fri
27now.BeginningOfQuarter()       // 2013-10-01 00:00:00 Tue
28now.BeginningOfYear()          // 2013-01-01 00:00:00 Tue
29
30now.WeekStartDay = time.Monday // Set Monday as first day, default is Sunday
31now.BeginningOfWeek()          // 2013-11-18 00:00:00 Mon
32
33now.EndOfMinute()              // 2013-11-18 17:51:59.999999999 Mon
34now.EndOfHour()                // 2013-11-18 17:59:59.999999999 Mon
35now.EndOfDay()                 // 2013-11-18 23:59:59.999999999 Mon
36now.EndOfWeek()                // 2013-11-23 23:59:59.999999999 Sat
37now.EndOfMonth()               // 2013-11-30 23:59:59.999999999 Sat
38now.EndOfQuarter()             // 2013-12-31 23:59:59.999999999 Tue
39now.EndOfYear()                // 2013-12-31 23:59:59.999999999 Tue
40
41now.WeekStartDay = time.Monday // Set Monday as first day, default is Sunday
42now.EndOfWeek()                // 2013-11-24 23:59:59.999999999 Sun
43```
44
45Calculating time based on another time
46
47```go
48t := time.Date(2013, 02, 18, 17, 51, 49, 123456789, time.Now().Location())
49now.With(t).EndOfMonth()   // 2013-02-28 23:59:59.999999999 Thu
50```
51
52Calculating time based on configuration
53
54```go
55location, err := time.LoadLocation("Asia/Shanghai")
56
57myConfig := &now.Config{
58	WeekStartDay: time.Monday,
59	TimeLocation: location,
60	TimeFormats: []string{"2006-01-02 15:04:05"},
61}
62
63t := time.Date(2013, 11, 18, 17, 51, 49, 123456789, time.Now().Location()) // // 2013-11-18 17:51:49.123456789 Mon
64myConfig.With(t).BeginningOfWeek()         // 2013-11-18 00:00:00 Mon
65
66myConfig.Parse("2002-10-12 22:14:01")     // 2002-10-12 22:14:01
67myConfig.Parse("2002-10-12 22:14")        // returns error 'can't parse string as time: 2002-10-12 22:14'
68```
69
70### Monday/Sunday
71
72Don't be bothered with the `WeekStartDay` setting, you can use `Monday`, `Sunday`
73
74```go
75now.Monday()              // 2013-11-18 00:00:00 Mon
76now.Sunday()              // 2013-11-24 00:00:00 Sun (Next Sunday)
77now.EndOfSunday()         // 2013-11-24 23:59:59.999999999 Sun (End of next Sunday)
78
79t := time.Date(2013, 11, 24, 17, 51, 49, 123456789, time.Now().Location()) // 2013-11-24 17:51:49.123456789 Sun
80now.With(t).Monday()       // 2013-11-18 00:00:00 Sun (Last Monday if today is Sunday)
81now.With(t).Sunday()       // 2013-11-24 00:00:00 Sun (Beginning Of Today if today is Sunday)
82now.With(t).EndOfSunday()  // 2013-11-24 23:59:59.999999999 Sun (End of Today if today is Sunday)
83```
84
85### Parse String to Time
86
87```go
88time.Now() // 2013-11-18 17:51:49.123456789 Mon
89
90// Parse(string) (time.Time, error)
91t, err := now.Parse("2017")                // 2017-01-01 00:00:00, nil
92t, err := now.Parse("2017-10")             // 2017-10-01 00:00:00, nil
93t, err := now.Parse("2017-10-13")          // 2017-10-13 00:00:00, nil
94t, err := now.Parse("1999-12-12 12")       // 1999-12-12 12:00:00, nil
95t, err := now.Parse("1999-12-12 12:20")    // 1999-12-12 12:20:00, nil
96t, err := now.Parse("1999-12-12 12:20:21") // 1999-12-12 12:20:00, nil
97t, err := now.Parse("10-13")               // 2013-10-13 00:00:00, nil
98t, err := now.Parse("12:20")               // 2013-11-18 12:20:00, nil
99t, err := now.Parse("12:20:13")            // 2013-11-18 12:20:13, nil
100t, err := now.Parse("14")                  // 2013-11-18 14:00:00, nil
101t, err := now.Parse("99:99")               // 2013-11-18 12:20:00, Can't parse string as time: 99:99
102
103// MustParse must parse string to time or it will panic
104now.MustParse("2013-01-13")             // 2013-01-13 00:00:00
105now.MustParse("02-17")                  // 2013-02-17 00:00:00
106now.MustParse("2-17")                   // 2013-02-17 00:00:00
107now.MustParse("8")                      // 2013-11-18 08:00:00
108now.MustParse("2002-10-12 22:14")       // 2002-10-12 22:14:00
109now.MustParse("99:99")                  // panic: Can't parse string as time: 99:99
110```
111
112Extend `now` to support more formats is quite easy, just update `now.TimeFormats` with other time layouts, e.g:
113
114```go
115now.TimeFormats = append(now.TimeFormats, "02 Jan 2006 15:04")
116```
117
118Please send me pull requests if you want a format to be supported officially
119
120## Contributing
121
122You can help to make the project better, check out [http://gorm.io/contribute.html](http://gorm.io/contribute.html) for things you can do.
123
124# Author
125
126**jinzhu**
127
128* <http://github.com/jinzhu>
129* <wosmvp@gmail.com>
130* <http://twitter.com/zhangjinzhu>
131
132## License
133
134Released under the [MIT License](http://www.opensource.org/licenses/MIT).
135