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

..03-May-2022-

GuardfileH A D18-Mar-202141 43

LicenseH A D18-Mar-20211.1 KiB2217

README.mdH A D18-Mar-20214.8 KiB13295

go.modH A D18-Mar-202138 42

main.goH A D18-Mar-20214.9 KiB195119

now.goH A D18-Mar-20215.6 KiB213154

now_test.goH A D18-Mar-202114.9 KiB384252

time.goH A D18-Mar-2021201 107

wercker.ymlH A D18-Mar-2021354 2419

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