Create & Init Project...

This commit is contained in:
2019-04-22 18:49:16 +08:00
commit fc4fa37393
25440 changed files with 4054998 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
package(default_visibility = ["//visibility:public"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_test",
"go_library",
)
go_test(
name = "go_default_test",
srcs = ["now_test.go"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
)
go_library(
name = "go_default_library",
srcs = [
"main.go",
"now.go",
],
importpath = "go-common/app/admin/main/up/util/now",
tags = ["automanaged"],
visibility = ["//visibility:public"],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
visibility = ["//visibility:public"],
)

View File

@@ -0,0 +1,3 @@
guard 'gotest' do
watch(%r{\.go$})
end

View File

@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) 2013-NOW Jinzhu <wosmvp@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@@ -0,0 +1,116 @@
## Now
Now is a time toolkit for golang
[![wercker status](https://app.wercker.com/status/a350da4eae6cb28a35687ba41afb565a/s/master "wercker status")](https://app.wercker.com/project/byKey/a350da4eae6cb28a35687ba41afb565a)
## Install
```
go get -u github.com/jinzhu/now
```
## Usage
Calculating time based on current time
```go
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
now.WeekStartDay = time.Monday // Set Monday as first day, default is Sunday
now.BeginningOfWeek() // 2013-11-18 00:00:00 Mon
now.EndOfMinute() // 2013-11-18 17:51:59.999999999 Mon
now.EndOfHour() // 2013-11-18 17:59:59.999999999 Mon
now.EndOfDay() // 2013-11-18 23:59:59.999999999 Mon
now.EndOfWeek() // 2013-11-23 23:59:59.999999999 Sat
now.EndOfMonth() // 2013-11-30 23:59:59.999999999 Sat
now.EndOfQuarter() // 2013-12-31 23:59:59.999999999 Tue
now.EndOfYear() // 2013-12-31 23:59:59.999999999 Tue
now.WeekStartDay = time.Monday // Set Monday as first day, default is Sunday
now.EndOfWeek() // 2013-11-24 23:59:59.999999999 Sun
```
Calculating time based on another time
```go
t := time.Date(2013, 02, 18, 17, 51, 49, 123456789, time.Now().Location())
now.New(t).EndOfMonth() // 2013-02-28 23:59:59.999999999 Thu
```
### Monday/Sunday
Don't be bothered with the `WeekStartDay` setting, you can use `Monday`, `Sunday`
```go
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)
t := time.Date(2013, 11, 24, 17, 51, 49, 123456789, time.Now().Location()) // 2013-11-24 17:51:49.123456789 Sun
now.New(t).Monday() // 2013-11-18 00:00:00 Sun (Last Monday if today is Sunday)
now.New(t).Sunday() // 2013-11-24 00:00:00 Sun (Beginning Of Today if today is Sunday)
now.New(t).EndOfSunday() // 2013-11-24 23:59:59.999999999 Sun (End of Today if today is Sunday)
```
### Parse String to Time
```go
time.Now() // 2013-11-18 17:51:49.123456789 Mon
// Parse(string) (time.Time, error)
t, err := now.Parse("2017") // 2017-01-01 00:00:00, nil
t, err := now.Parse("2017-10") // 2017-10-01 00:00:00, nil
t, err := now.Parse("2017-10-13") // 2017-10-13 00:00:00, nil
t, err := now.Parse("1999-12-12 12") // 1999-12-12 12:00:00, nil
t, err := now.Parse("1999-12-12 12:20") // 1999-12-12 12:20:00, nil
t, err := now.Parse("1999-12-12 12:20:21") // 1999-12-12 12:20:00, nil
t, err := now.Parse("10-13") // 2013-10-13 00:00:00, nil
t, err := now.Parse("12:20") // 2013-11-18 12:20:00, nil
t, err := now.Parse("12:20:13") // 2013-11-18 12:20:13, nil
t, err := now.Parse("14") // 2013-11-18 14:00:00, nil
t, err := now.Parse("99:99") // 2013-11-18 12:20:00, Can't parse string as time: 99:99
// MustParse must parse string to time or it will panic
now.MustParse("2013-01-13") // 2013-01-13 00:00:00
now.MustParse("02-17") // 2013-02-17 00:00:00
now.MustParse("2-17") // 2013-02-17 00:00:00
now.MustParse("8") // 2013-11-18 08:00:00
now.MustParse("2002-10-12 22:14") // 2002-10-12 22:14:00
now.MustParse("99:99") // panic: Can't parse string as time: 99:99
```
Extend `now` to support more formats is quite easy, just update `now.TimeFormats` with other time layouts, e.g:
```go
now.TimeFormats = append(now.TimeFormats, "02 Jan 2006 15:04")
```
Please send me pull requests if you want a format to be supported officially
## Contributing
You can help to make the project better, check out [http://gorm.io/contribute.html](http://gorm.io/contribute.html) for things you can do.
# Author
**jinzhu**
* <http://github.com/jinzhu>
* <wosmvp@gmail.com>
* <http://twitter.com/zhangjinzhu>
## License
Released under the [MIT License](http://www.opensource.org/licenses/MIT).

View File

@@ -0,0 +1,138 @@
// Package now is a time toolkit for golang.
//
// More details README here: https://github.com/jinzhu/now
//
// import "github.com/jinzhu/now"
//
// now.BeginningOfMinute() // 2013-11-18 17:51:00 Mon
// now.BeginningOfDay() // 2013-11-18 00:00:00 Mon
// now.EndOfDay() // 2013-11-18 23:59:59.999999999 Mon
package now
import "time"
// WeekStartDay set week start day, default is sunday
var WeekStartDay = time.Sunday
// TimeFormats default time formats will be parsed as
var TimeFormats = []string{"1/2/2006", "1/2/2006 15:4:5", "2006", "2006-1", "2006-1-2", "2006-1-2 15", "2006-1-2 15:4", "2006-1-2 15:4:5", "1-2", "15:4:5", "15:4", "15", "15:4:5 Jan 2, 2006 MST", "2006-01-02 15:04:05.999999999 -0700 MST"}
// Now now struct
type Now struct {
time.Time
}
// New initialize Now with time
func New(t time.Time) *Now {
return &Now{t}
}
// BeginningOfMinute beginning of minute
func BeginningOfMinute() time.Time {
return New(time.Now()).BeginningOfMinute()
}
// BeginningOfHour beginning of hour
func BeginningOfHour() time.Time {
return New(time.Now()).BeginningOfHour()
}
// BeginningOfDay beginning of day
func BeginningOfDay() time.Time {
return New(time.Now()).BeginningOfDay()
}
// BeginningOfWeek beginning of week
func BeginningOfWeek() time.Time {
return New(time.Now()).BeginningOfWeek()
}
// BeginningOfMonth beginning of month
func BeginningOfMonth() time.Time {
return New(time.Now()).BeginningOfMonth()
}
// BeginningOfQuarter beginning of quarter
func BeginningOfQuarter() time.Time {
return New(time.Now()).BeginningOfQuarter()
}
// BeginningOfYear beginning of year
func BeginningOfYear() time.Time {
return New(time.Now()).BeginningOfYear()
}
// EndOfMinute end of minute
func EndOfMinute() time.Time {
return New(time.Now()).EndOfMinute()
}
// EndOfHour end of hour
func EndOfHour() time.Time {
return New(time.Now()).EndOfHour()
}
// EndOfDay end of day
func EndOfDay() time.Time {
return New(time.Now()).EndOfDay()
}
// EndOfWeek end of week
func EndOfWeek() time.Time {
return New(time.Now()).EndOfWeek()
}
// EndOfMonth end of month
func EndOfMonth() time.Time {
return New(time.Now()).EndOfMonth()
}
// EndOfQuarter end of quarter
func EndOfQuarter() time.Time {
return New(time.Now()).EndOfQuarter()
}
// EndOfYear end of year
func EndOfYear() time.Time {
return New(time.Now()).EndOfYear()
}
// Monday monday
func Monday() time.Time {
return New(time.Now()).Monday()
}
// Sunday sunday
func Sunday() time.Time {
return New(time.Now()).Sunday()
}
// EndOfSunday end of sunday
func EndOfSunday() time.Time {
return New(time.Now()).EndOfSunday()
}
// Parse parse string to time
func Parse(strs ...string) (time.Time, error) {
return New(time.Now()).Parse(strs...)
}
// ParseInLocation parse string to time in location
func ParseInLocation(loc *time.Location, strs ...string) (time.Time, error) {
return New(time.Now().In(loc)).Parse(strs...)
}
// MustParse must parse string to time or will panic
func MustParse(strs ...string) time.Time {
return New(time.Now()).MustParse(strs...)
}
// MustParseInLocation must parse string to time in location or will panic
func MustParseInLocation(loc *time.Location, strs ...string) time.Time {
return New(time.Now().In(loc)).MustParse(strs...)
}
// Between check now between the begin, end time or not
func Between(time1, time2 string) bool {
return New(time.Now()).Between(time1, time2)
}

View File

@@ -0,0 +1,203 @@
package now
import (
"errors"
"regexp"
"time"
)
// BeginningOfMinute beginning of minute
func (now *Now) BeginningOfMinute() time.Time {
return now.Truncate(time.Minute)
}
// BeginningOfHour beginning of hour
func (now *Now) BeginningOfHour() time.Time {
y, m, d := now.Date()
return time.Date(y, m, d, now.Time.Hour(), 0, 0, 0, now.Time.Location())
}
// BeginningOfDay beginning of day
func (now *Now) BeginningOfDay() time.Time {
y, m, d := now.Date()
return time.Date(y, m, d, 0, 0, 0, 0, now.Time.Location())
}
// BeginningOfWeek beginning of week
func (now *Now) BeginningOfWeek() time.Time {
t := now.BeginningOfDay()
weekday := int(t.Weekday())
if WeekStartDay != time.Sunday {
weekStartDayInt := int(WeekStartDay)
if weekday < weekStartDayInt {
weekday = weekday + 7 - weekStartDayInt
} else {
weekday = weekday - weekStartDayInt
}
}
return t.AddDate(0, 0, -weekday)
}
// BeginningOfMonth beginning of month
func (now *Now) BeginningOfMonth() time.Time {
y, m, _ := now.Date()
return time.Date(y, m, 1, 0, 0, 0, 0, now.Location())
}
// BeginningOfQuarter beginning of quarter
func (now *Now) BeginningOfQuarter() time.Time {
month := now.BeginningOfMonth()
offset := (int(month.Month()) - 1) % 3
return month.AddDate(0, -offset, 0)
}
// BeginningOfYear BeginningOfYear beginning of year
func (now *Now) BeginningOfYear() time.Time {
y, _, _ := now.Date()
return time.Date(y, time.January, 1, 0, 0, 0, 0, now.Location())
}
// EndOfMinute end of minute
func (now *Now) EndOfMinute() time.Time {
return now.BeginningOfMinute().Add(time.Minute - time.Nanosecond)
}
// EndOfHour end of hour
func (now *Now) EndOfHour() time.Time {
return now.BeginningOfHour().Add(time.Hour - time.Nanosecond)
}
// EndOfDay end of day
func (now *Now) EndOfDay() time.Time {
y, m, d := now.Date()
return time.Date(y, m, d, 23, 59, 59, int(time.Second-time.Nanosecond), now.Location())
}
// EndOfWeek end of week
func (now *Now) EndOfWeek() time.Time {
return now.BeginningOfWeek().AddDate(0, 0, 7).Add(-time.Nanosecond)
}
// EndOfMonth end of month
func (now *Now) EndOfMonth() time.Time {
return now.BeginningOfMonth().AddDate(0, 1, 0).Add(-time.Nanosecond)
}
// EndOfQuarter end of quarter
func (now *Now) EndOfQuarter() time.Time {
return now.BeginningOfQuarter().AddDate(0, 3, 0).Add(-time.Nanosecond)
}
// EndOfYear end of year
func (now *Now) EndOfYear() time.Time {
return now.BeginningOfYear().AddDate(1, 0, 0).Add(-time.Nanosecond)
}
// Monday monday
func (now *Now) Monday() time.Time {
t := now.BeginningOfDay()
weekday := int(t.Weekday())
if weekday == 0 {
weekday = 7
}
return t.AddDate(0, 0, -weekday+1)
}
// Sunday sunday
func (now *Now) Sunday() time.Time {
t := now.BeginningOfDay()
weekday := int(t.Weekday())
if weekday == 0 {
return t
}
return t.AddDate(0, 0, (7 - weekday))
}
// EndOfSunday end of sunday
func (now *Now) EndOfSunday() time.Time {
return New(now.Sunday()).EndOfDay()
}
func parseWithFormat(str string) (t time.Time, err error) {
for _, format := range TimeFormats {
t, err = time.Parse(format, str)
if err == nil {
return
}
}
err = errors.New("Can't parse string as time: " + str)
return
}
var hasTimeRegexp = regexp.MustCompile(`(\s+|^\s*)\d{1,2}((:\d{1,2})*|((:\d{1,2}){2}\.(\d{3}|\d{6}|\d{9})))\s*$`) // match 15:04:05, 15:04:05.000, 15:04:05.000000 15, 2017-01-01 15:04, etc
var onlyTimeRegexp = regexp.MustCompile(`^\s*\d{1,2}((:\d{1,2})*|((:\d{1,2}){2}\.(\d{3}|\d{6}|\d{9})))\s*$`) // match 15:04:05, 15, 15:04:05.000, 15:04:05.000000, etc
// Parse parse string to time
func (now *Now) Parse(strs ...string) (t time.Time, err error) {
var (
setCurrentTime bool
parseTime []int
currentTime = []int{now.Nanosecond(), now.Second(), now.Minute(), now.Hour(), now.Day(), int(now.Month()), now.Year()}
currentLocation = now.Location()
onlyTimeInStr = true
)
for _, str := range strs {
hasTimeInStr := hasTimeRegexp.MatchString(str) // match 15:04:05, 15
onlyTimeInStr = hasTimeInStr && onlyTimeInStr && onlyTimeRegexp.MatchString(str)
if t, err = parseWithFormat(str); err == nil {
location := t.Location()
if location.String() == "UTC" {
location = currentLocation
}
parseTime = []int{t.Nanosecond(), t.Second(), t.Minute(), t.Hour(), t.Day(), int(t.Month()), t.Year()}
for i, v := range parseTime {
// Don't reset hour, minute, second if current time str including time
if hasTimeInStr && i <= 3 {
continue
}
// If value is zero, replace it with current time
if v == 0 {
if setCurrentTime {
parseTime[i] = currentTime[i]
}
} else {
setCurrentTime = true
}
// if current time only includes time, should change day, month to current time
if onlyTimeInStr {
if i == 4 || i == 5 {
parseTime[i] = currentTime[i]
continue
}
}
}
t = time.Date(parseTime[6], time.Month(parseTime[5]), parseTime[4], parseTime[3], parseTime[2], parseTime[1], parseTime[0], location)
currentTime = []int{t.Nanosecond(), t.Second(), t.Minute(), t.Hour(), t.Day(), int(t.Month()), t.Year()}
}
}
return
}
// MustParse must parse string to time or it will panic
func (now *Now) MustParse(strs ...string) (t time.Time) {
t, err := now.Parse(strs...)
if err != nil {
panic(err)
}
return t
}
// Between check time between the begin, end time or not
func (now *Now) Between(begin, end string) bool {
beginTime := now.MustParse(begin)
endTime := now.MustParse(end)
return now.After(beginTime) && now.Before(endTime)
}

View File

@@ -0,0 +1,344 @@
package now
import (
"testing"
"time"
)
var (
format = "2006-01-02 15:04:05.999999999"
locationCaracas *time.Location
locationBerlin *time.Location
timeCaracas time.Time
)
func init() {
var err error
if locationCaracas, err = time.LoadLocation("America/Caracas"); err != nil {
panic(err)
}
if locationBerlin, err = time.LoadLocation("Europe/Berlin"); err != nil {
panic(err)
}
timeCaracas = time.Date(2016, 1, 1, 12, 10, 0, 0, locationCaracas)
}
func assertT(t *testing.T) func(time.Time, string, string) {
return func(actual time.Time, expected string, msg string) {
actualStr := actual.Format(format)
if actualStr != expected {
t.Errorf("Failed %s: actual: %v, expected: %v", msg, actualStr, expected)
}
}
}
func TestBeginningOf(t *testing.T) {
assert := assertT(t)
n := time.Date(2013, 11, 18, 17, 51, 49, 123456789, time.UTC)
assert(New(n).BeginningOfMinute(), "2013-11-18 17:51:00", "BeginningOfMinute")
WeekStartDay = time.Monday
assert(New(n).BeginningOfWeek(), "2013-11-18 00:00:00", "BeginningOfWeek, FirstDayMonday")
WeekStartDay = time.Tuesday
assert(New(n).BeginningOfWeek(), "2013-11-12 00:00:00", "BeginningOfWeek, FirstDayTuesday")
WeekStartDay = time.Wednesday
assert(New(n).BeginningOfWeek(), "2013-11-13 00:00:00", "BeginningOfWeek, FirstDayWednesday")
WeekStartDay = time.Thursday
assert(New(n).BeginningOfWeek(), "2013-11-14 00:00:00", "BeginningOfWeek, FirstDayThursday")
WeekStartDay = time.Friday
assert(New(n).BeginningOfWeek(), "2013-11-15 00:00:00", "BeginningOfWeek, FirstDayFriday")
WeekStartDay = time.Saturday
assert(New(n).BeginningOfWeek(), "2013-11-16 00:00:00", "BeginningOfWeek, FirstDaySaturday")
WeekStartDay = time.Sunday
assert(New(n).BeginningOfWeek(), "2013-11-17 00:00:00", "BeginningOfWeek, FirstDaySunday")
assert(New(n).BeginningOfHour(), "2013-11-18 17:00:00", "BeginningOfHour")
// Truncate with hour bug
assert(New(timeCaracas).BeginningOfHour(), "2016-01-01 12:00:00", "BeginningOfHour Caracas")
assert(New(n).BeginningOfDay(), "2013-11-18 00:00:00", "BeginningOfDay")
location, err := time.LoadLocation("Japan")
if err != nil {
t.Fatalf("Error loading location: %v", err)
}
beginningOfDay := time.Date(2015, 05, 01, 0, 0, 0, 0, location)
assert(New(beginningOfDay).BeginningOfDay(), "2015-05-01 00:00:00", "BeginningOfDay")
// DST
dstBeginningOfDay := time.Date(2017, 10, 29, 10, 0, 0, 0, locationBerlin)
assert(New(dstBeginningOfDay).BeginningOfDay(), "2017-10-29 00:00:00", "BeginningOfDay DST")
assert(New(n).BeginningOfWeek(), "2013-11-17 00:00:00", "BeginningOfWeek")
dstBegginingOfWeek := time.Date(2017, 10, 30, 12, 0, 0, 0, locationBerlin)
assert(New(dstBegginingOfWeek).BeginningOfWeek(), "2017-10-29 00:00:00", "BeginningOfWeek")
dstBegginingOfWeek = time.Date(2017, 10, 29, 12, 0, 0, 0, locationBerlin)
assert(New(dstBegginingOfWeek).BeginningOfWeek(), "2017-10-29 00:00:00", "BeginningOfWeek")
WeekStartDay = time.Monday
assert(New(n).BeginningOfWeek(), "2013-11-18 00:00:00", "BeginningOfWeek, FirstDayMonday")
dstBegginingOfWeek = time.Date(2017, 10, 24, 12, 0, 0, 0, locationBerlin)
assert(New(dstBegginingOfWeek).BeginningOfWeek(), "2017-10-23 00:00:00", "BeginningOfWeek, FirstDayMonday")
dstBegginingOfWeek = time.Date(2017, 10, 29, 12, 0, 0, 0, locationBerlin)
assert(New(dstBegginingOfWeek).BeginningOfWeek(), "2017-10-23 00:00:00", "BeginningOfWeek, FirstDayMonday")
WeekStartDay = time.Sunday
assert(New(n).BeginningOfMonth(), "2013-11-01 00:00:00", "BeginningOfMonth")
// DST
dstBeginningOfMonth := time.Date(2017, 10, 31, 0, 0, 0, 0, locationBerlin)
assert(New(dstBeginningOfMonth).BeginningOfMonth(), "2017-10-01 00:00:00", "BeginningOfMonth DST")
assert(New(n).BeginningOfQuarter(), "2013-10-01 00:00:00", "BeginningOfQuarter")
// DST
assert(New(dstBeginningOfMonth).BeginningOfQuarter(), "2017-10-01 00:00:00", "BeginningOfQuarter DST")
dstBeginningOfQuarter := time.Date(2017, 11, 24, 0, 0, 0, 0, locationBerlin)
assert(New(dstBeginningOfQuarter).BeginningOfQuarter(), "2017-10-01 00:00:00", "BeginningOfQuarter DST")
assert(New(n.AddDate(0, -1, 0)).BeginningOfQuarter(), "2013-10-01 00:00:00", "BeginningOfQuarter")
assert(New(n.AddDate(0, 1, 0)).BeginningOfQuarter(), "2013-10-01 00:00:00", "BeginningOfQuarter")
// DST
assert(New(dstBeginningOfQuarter).BeginningOfYear(), "2017-01-01 00:00:00", "BeginningOfYear DST")
assert(New(timeCaracas).BeginningOfYear(), "2016-01-01 00:00:00", "BeginningOfYear Caracas")
}
func TestEndOf(t *testing.T) {
assert := assertT(t)
n := time.Date(2013, 11, 18, 17, 51, 49, 123456789, time.UTC)
assert(New(n).EndOfMinute(), "2013-11-18 17:51:59.999999999", "EndOfMinute")
assert(New(n).EndOfHour(), "2013-11-18 17:59:59.999999999", "EndOfHour")
assert(New(timeCaracas).EndOfHour(), "2016-01-01 12:59:59.999999999", "EndOfHour Caracas")
assert(New(n).EndOfDay(), "2013-11-18 23:59:59.999999999", "EndOfDay")
dstEndOfDay := time.Date(2017, 10, 29, 1, 0, 0, 0, locationBerlin)
assert(New(dstEndOfDay).EndOfDay(), "2017-10-29 23:59:59.999999999", "EndOfDay DST")
WeekStartDay = time.Tuesday
assert(New(n).EndOfWeek(), "2013-11-18 23:59:59.999999999", "EndOfWeek, FirstDayTuesday")
WeekStartDay = time.Wednesday
assert(New(n).EndOfWeek(), "2013-11-19 23:59:59.999999999", "EndOfWeek, FirstDayWednesday")
WeekStartDay = time.Thursday
assert(New(n).EndOfWeek(), "2013-11-20 23:59:59.999999999", "EndOfWeek, FirstDayThursday")
WeekStartDay = time.Friday
assert(New(n).EndOfWeek(), "2013-11-21 23:59:59.999999999", "EndOfWeek, FirstDayFriday")
WeekStartDay = time.Saturday
assert(New(n).EndOfWeek(), "2013-11-22 23:59:59.999999999", "EndOfWeek, FirstDaySaturday")
WeekStartDay = time.Sunday
assert(New(n).EndOfWeek(), "2013-11-23 23:59:59.999999999", "EndOfWeek, FirstDaySunday")
WeekStartDay = time.Monday
assert(New(n).EndOfWeek(), "2013-11-24 23:59:59.999999999", "EndOfWeek, FirstDayMonday")
dstEndOfWeek := time.Date(2017, 10, 24, 12, 0, 0, 0, locationBerlin)
assert(New(dstEndOfWeek).EndOfWeek(), "2017-10-29 23:59:59.999999999", "EndOfWeek, FirstDayMonday")
dstEndOfWeek = time.Date(2017, 10, 29, 12, 0, 0, 0, locationBerlin)
assert(New(dstEndOfWeek).EndOfWeek(), "2017-10-29 23:59:59.999999999", "EndOfWeek, FirstDayMonday")
WeekStartDay = time.Sunday
assert(New(n).EndOfWeek(), "2013-11-23 23:59:59.999999999", "EndOfWeek")
dstEndOfWeek = time.Date(2017, 10, 29, 0, 0, 0, 0, locationBerlin)
assert(New(dstEndOfWeek).EndOfWeek(), "2017-11-04 23:59:59.999999999", "EndOfWeek")
dstEndOfWeek = time.Date(2017, 10, 29, 12, 0, 0, 0, locationBerlin)
assert(New(dstEndOfWeek).EndOfWeek(), "2017-11-04 23:59:59.999999999", "EndOfWeek")
assert(New(n).EndOfMonth(), "2013-11-30 23:59:59.999999999", "EndOfMonth")
assert(New(n).EndOfQuarter(), "2013-12-31 23:59:59.999999999", "EndOfQuarter")
assert(New(n.AddDate(0, -1, 0)).EndOfQuarter(), "2013-12-31 23:59:59.999999999", "EndOfQuarter")
assert(New(n.AddDate(0, 1, 0)).EndOfQuarter(), "2013-12-31 23:59:59.999999999", "EndOfQuarter")
assert(New(n).EndOfYear(), "2013-12-31 23:59:59.999999999", "EndOfYear")
n1 := time.Date(2013, 02, 18, 17, 51, 49, 123456789, time.UTC)
assert(New(n1).EndOfMonth(), "2013-02-28 23:59:59.999999999", "EndOfMonth for 2013/02")
n2 := time.Date(1900, 02, 18, 17, 51, 49, 123456789, time.UTC)
assert(New(n2).EndOfMonth(), "1900-02-28 23:59:59.999999999", "EndOfMonth")
}
func TestMondayAndSunday(t *testing.T) {
assert := assertT(t)
n := time.Date(2013, 11, 19, 17, 51, 49, 123456789, time.UTC)
n2 := time.Date(2013, 11, 24, 17, 51, 49, 123456789, time.UTC)
nDst := time.Date(2017, 10, 29, 10, 0, 0, 0, locationBerlin)
assert(New(n).Monday(), "2013-11-18 00:00:00", "Monday")
assert(New(n2).Monday(), "2013-11-18 00:00:00", "Monday")
assert(New(timeCaracas).Monday(), "2015-12-28 00:00:00", "Monday Caracas")
assert(New(nDst).Monday(), "2017-10-23 00:00:00", "Monday DST")
assert(New(n).Sunday(), "2013-11-24 00:00:00", "Sunday")
assert(New(n2).Sunday(), "2013-11-24 00:00:00", "Sunday")
assert(New(timeCaracas).Sunday(), "2016-01-03 00:00:00", "Sunday Caracas")
assert(New(nDst).Sunday(), "2017-10-29 00:00:00", "Sunday DST")
assert(New(n).EndOfSunday(), "2013-11-24 23:59:59.999999999", "EndOfSunday")
assert(New(timeCaracas).EndOfSunday(), "2016-01-03 23:59:59.999999999", "EndOfSunday Caracas")
assert(New(nDst).EndOfSunday(), "2017-10-29 23:59:59.999999999", "EndOfSunday DST")
assert(New(n).BeginningOfWeek(), "2013-11-17 00:00:00", "BeginningOfWeek, FirstDayMonday")
WeekStartDay = time.Monday
assert(New(n).BeginningOfWeek(), "2013-11-18 00:00:00", "BeginningOfWeek, FirstDayMonday")
}
func TestParse(t *testing.T) {
assert := assertT(t)
n := time.Date(2013, 11, 18, 17, 51, 49, 123456789, time.UTC)
assert(New(n).MustParse("2002"), "2002-01-01 00:00:00", "Parse 2002")
assert(New(n).MustParse("2002-10"), "2002-10-01 00:00:00", "Parse 2002-10")
assert(New(n).MustParse("2002-10-12"), "2002-10-12 00:00:00", "Parse 2002-10-12")
assert(New(n).MustParse("2002-10-12 22"), "2002-10-12 22:00:00", "Parse 2002-10-12 22")
assert(New(n).MustParse("2002-10-12 22:14"), "2002-10-12 22:14:00", "Parse 2002-10-12 22:14")
assert(New(n).MustParse("2002-10-12 2:4"), "2002-10-12 02:04:00", "Parse 2002-10-12 2:4")
assert(New(n).MustParse("2002-10-12 02:04"), "2002-10-12 02:04:00", "Parse 2002-10-12 02:04")
assert(New(n).MustParse("2002-10-12 22:14:56"), "2002-10-12 22:14:56", "Parse 2002-10-12 22:14:56")
assert(New(n).MustParse("2002-10-12 00:14:56"), "2002-10-12 00:14:56", "Parse 2002-10-12 00:14:56")
assert(New(n).MustParse("2013-12-19 23:28:09.999999999 +0800 CST"), "2013-12-19 23:28:09.999999999", "Parse two strings 2013-12-19 23:28:09.999999999 +0800 CST")
assert(New(n).MustParse("10-12"), "2013-10-12 00:00:00", "Parse 10-12")
assert(New(n).MustParse("18"), "2013-11-18 18:00:00", "Parse 18 as hour")
assert(New(n).MustParse("18:20"), "2013-11-18 18:20:00", "Parse 18:20")
assert(New(n).MustParse("00:01"), "2013-11-18 00:01:00", "Parse 00:01")
assert(New(n).MustParse("00:00:00"), "2013-11-18 00:00:00", "Parse 00:00:00")
assert(New(n).MustParse("18:20:39"), "2013-11-18 18:20:39", "Parse 18:20:39")
assert(New(n).MustParse("18:20:39", "2011-01-01"), "2011-01-01 18:20:39", "Parse two strings 18:20:39, 2011-01-01")
assert(New(n).MustParse("2011-1-1", "18:20:39"), "2011-01-01 18:20:39", "Parse two strings 2011-01-01, 18:20:39")
assert(New(n).MustParse("2011-01-01", "18"), "2011-01-01 18:00:00", "Parse two strings 2011-01-01, 18")
TimeFormats = append(TimeFormats, "02 Jan 15:04")
assert(New(n).MustParse("04 Feb 12:09"), "2013-02-04 12:09:00", "Parse 04 Feb 12:09 with specified format")
assert(New(n).MustParse("23:28:9 Dec 19, 2013 PST"), "2013-12-19 23:28:09", "Parse 23:28:9 Dec 19, 2013 PST")
if New(n).MustParse("23:28:9 Dec 19, 2013 PST").Location().String() != "PST" {
t.Errorf("Parse 23:28:9 Dec 19, 2013 PST shouldn't lose time zone")
}
n2 := New(n).MustParse("23:28:9 Dec 19, 2013 PST")
if New(n2).MustParse("10:20").Location().String() != "PST" {
t.Errorf("Parse 10:20 shouldn't change time zone")
}
TimeFormats = append(TimeFormats, "2006-01-02T15:04:05.0")
if MustParseInLocation(time.UTC, "2018-02-13T15:17:06.0").String() != "2018-02-13 15:17:06 +0000 UTC" {
t.Errorf("ParseInLocation 2018-02-13T15:17:06.0")
}
TimeFormats = append(TimeFormats, "2006-01-02 15:04:05.000")
assert(New(n).MustParse("2018-04-20 21:22:23.473"), "2018-04-20 21:22:23.473", "Parse 2018/04/20 21:22:23.473")
TimeFormats = append(TimeFormats, "15:04:05.000")
assert(New(n).MustParse("13:00:01.365"), "2013-11-18 13:00:01.365", "Parse 13:00:01.365")
TimeFormats = append(TimeFormats, "2006-01-02 15:04:05.000000")
assert(New(n).MustParse("2010-01-01 07:24:23.131384"), "2010-01-01 07:24:23.131384", "Parse 2010-01-01 07:24:23.131384")
assert(New(n).MustParse("00:00:00.182736"), "2013-11-18 00:00:00.182736", "Parse 00:00:00.182736")
}
func TestBetween(t *testing.T) {
tm := time.Date(2015, 06, 30, 17, 51, 49, 123456789, time.Now().Location())
if !New(tm).Between("23:28:9 Dec 19, 2013 PST", "23:28:9 Dec 19, 2015 PST") {
t.Errorf("Between")
}
if !New(tm).Between("2015-05-12 12:20", "2015-06-30 17:51:50") {
t.Errorf("Between")
}
}
func Example() {
time.Now() // 2013-11-18 17:51:49.123456789 Mon
BeginningOfMinute() // 2013-11-18 17:51:00 Mon
BeginningOfHour() // 2013-11-18 17:00:00 Mon
BeginningOfDay() // 2013-11-18 00:00:00 Mon
BeginningOfWeek() // 2013-11-17 00:00:00 Sun
WeekStartDay = time.Monday // Set Monday as first day
BeginningOfWeek() // 2013-11-18 00:00:00 Mon
BeginningOfMonth() // 2013-11-01 00:00:00 Fri
BeginningOfQuarter() // 2013-10-01 00:00:00 Tue
BeginningOfYear() // 2013-01-01 00:00:00 Tue
EndOfMinute() // 2013-11-18 17:51:59.999999999 Mon
EndOfHour() // 2013-11-18 17:59:59.999999999 Mon
EndOfDay() // 2013-11-18 23:59:59.999999999 Mon
EndOfWeek() // 2013-11-23 23:59:59.999999999 Sat
WeekStartDay = time.Monday // Set Monday as first day
EndOfWeek() // 2013-11-24 23:59:59.999999999 Sun
EndOfMonth() // 2013-11-30 23:59:59.999999999 Sat
EndOfQuarter() // 2013-12-31 23:59:59.999999999 Tue
EndOfYear() // 2013-12-31 23:59:59.999999999 Tue
// Use another time
t := time.Date(2013, 02, 18, 17, 51, 49, 123456789, time.UTC)
New(t).EndOfMonth() // 2013-02-28 23:59:59.999999999 Thu
Monday() // 2013-11-18 00:00:00 Mon
Sunday() // 2013-11-24 00:00:00 Sun
EndOfSunday() // 2013-11-24 23:59:59.999999999 Sun
}

View File

@@ -0,0 +1,23 @@
box: golang
build:
steps:
- setup-go-workspace
# Gets the dependencies
- script:
name: go get
code: |
go get
# Build the project
- script:
name: go build
code: |
go build ./...
# Test the project
- script:
name: go test
code: |
go test ./...