now

package
v0.0.0-...-e7b3743 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 9, 2020 License: MIT Imports: 3 Imported by: 0

README

Now

Now is a time toolkit for golang

Why the project named Now?
now.BeginningOfDay()

now is quite readable, aha?

But now is so common I can't search the project with my favorite search engine

Install

go get -u github.com/jinzhu/now

Usage

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.FirstDayMonday = true // Set Monday as first day, default is Sunday
now.BeginningOfWeek()     // 2013-11-18 00:00:00 Mon
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.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.FirstDayMonday = true // Set Monday as first day, default is Sunday
now.EndOfWeek()           // 2013-11-24 23:59:59.999999999 Sun
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


// Use another time
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


// Don't want be bothered with the First Day setting, Use Monday, Sunday
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
time.Now() // 2013-11-18 17:51:49.123456789 Mon

// Parse(string) (time.Time, error)
t, err := now.Parse("12:20")            // 2013-11-18 12:20:00, nil
t, err := now.Parse("1999-12-12 12:20") // 1999-12-12 12:20:00, nil
t, err := now.Parse("99:99")            // 2013-11-18 12:20:00, Can't parse string as time: 99:99

// MustParse(string) time.Time
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 TimeFormats variable with time.Format like time layout

now.TimeFormats = append(now.TimeFormats, "02 Jan 2006 15:04")

Please send me pull requests if you want a format to be supported officially

Author

jinzhu

License

Released under the MIT License.

Documentation

Overview

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
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

FirstDayMonday = true // 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

FirstDayMonday = true // 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
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var FirstDayMonday bool
View Source
var TimeFormats = []string{"1/2/2006", "1/2/2006 15:4:5", "2006-1-2 15:4:5", "2006-1-2 15:4", "2006-1-2", "1-2", "15:4:5", "15:4", "15", "15:4:5 Jan 2, 2006 MST"}

Functions

func BeginningOfDay

func BeginningOfDay() time.Time

func BeginningOfHour

func BeginningOfHour() time.Time

func BeginningOfMinute

func BeginningOfMinute() time.Time

func BeginningOfMonth

func BeginningOfMonth() time.Time

func BeginningOfQuarter

func BeginningOfQuarter() time.Time

func BeginningOfWeek

func BeginningOfWeek() time.Time

func BeginningOfYear

func BeginningOfYear() time.Time

func Between

func Between(time1, time2 string) bool

func EndOfDay

func EndOfDay() time.Time

func EndOfHour

func EndOfHour() time.Time

func EndOfMinute

func EndOfMinute() time.Time

func EndOfMonth

func EndOfMonth() time.Time

func EndOfQuarter

func EndOfQuarter() time.Time

func EndOfSunday

func EndOfSunday() time.Time

func EndOfWeek

func EndOfWeek() time.Time

func EndOfYear

func EndOfYear() time.Time

func Monday

func Monday() time.Time

func MustParse

func MustParse(strs ...string) time.Time

func Parse

func Parse(strs ...string) (time.Time, error)

func Sunday

func Sunday() time.Time

Types

type Now

type Now struct {
	time.Time
}

func New

func New(t time.Time) *Now

func (*Now) BeginningOfDay

func (now *Now) BeginningOfDay() time.Time

func (*Now) BeginningOfHour

func (now *Now) BeginningOfHour() time.Time

func (*Now) BeginningOfMinute

func (now *Now) BeginningOfMinute() time.Time

func (*Now) BeginningOfMonth

func (now *Now) BeginningOfMonth() time.Time

func (*Now) BeginningOfQuarter

func (now *Now) BeginningOfQuarter() time.Time

func (*Now) BeginningOfWeek

func (now *Now) BeginningOfWeek() time.Time

func (*Now) BeginningOfYear

func (now *Now) BeginningOfYear() time.Time

func (*Now) Between

func (now *Now) Between(time1, time2 string) bool

func (*Now) EndOfDay

func (now *Now) EndOfDay() time.Time

func (*Now) EndOfHour

func (now *Now) EndOfHour() time.Time

func (*Now) EndOfMinute

func (now *Now) EndOfMinute() time.Time

func (*Now) EndOfMonth

func (now *Now) EndOfMonth() time.Time

func (*Now) EndOfQuarter

func (now *Now) EndOfQuarter() time.Time

func (*Now) EndOfSunday

func (now *Now) EndOfSunday() time.Time

func (*Now) EndOfWeek

func (now *Now) EndOfWeek() time.Time

func (*Now) EndOfYear

func (now *Now) EndOfYear() time.Time

func (*Now) Monday

func (now *Now) Monday() time.Time

func (*Now) MustParse

func (now *Now) MustParse(strs ...string) (t time.Time)

func (*Now) Parse

func (now *Now) Parse(strs ...string) (t time.Time, err error)

func (*Now) Sunday

func (now *Now) Sunday() time.Time

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL