GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Why Go
  • Case Studies
  • Use Cases
  • Security
  • Learn
  • Effective Go
  • Go User Manual
  • Standard library
  • Release Notes
  • API
  • Packages
  • Recorded Talks
  • Meetups
  • Conferences

This view

  • Original page
  • New address

GeezerVison is showing pkg.go.dev — now package - github.com/jinzhu/now - Go Packages

Details

  • checked Valid go.mod file

    The Go module system was introduced in Go 1.11 and is the official dependency management solution for Go.

  • checked Redistributable license

    Redistributable licenses place minimal restrictions on how software can be used, modified, and redistributed.

  • checked Tagged version

    Modules with tagged versions give importers more predictable builds.

  • checked Stable version

    When a project reaches major version v1 it is considered stable.

  • Learn more about best practices

Repository

github.com/jinzhu/now

Links

  • Open Source Insights Logo Open Source Insights

README ¶

Now

Now is a time toolkit for golang

go report card test status MIT license

Install

go get -u github.com/jinzhu/now

Usage

Calculating time based on current time

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

t := time.Date(2013, 02, 18, 17, 51, 49, 123456789, time.Now().Location())
now.With(t).EndOfMonth()   // 2013-02-28 23:59:59.999999999 Thu

Calculating time based on configuration

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'

Monday/Sunday

Don't be bothered with the WeekStartDay setting, you can use Monday, Sunday

now.Monday()              // 2013-11-18 00:00:00 Mon
now.Monday("17:44")       // 2013-11-18 17:44:00 Mon
now.Sunday()              // 2013-11-24 00:00:00 Sun (Next Sunday)
now.Sunday("18:19:24")    // 2013-11-24 18:19:24 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.With(t).Monday()              // 2013-11-18 00:00:00 Mon (Last Monday if today is Sunday)
now.With(t).Monday("17:44")       // 2013-11-18 17:44:00 Mon (Last Monday if today is Sunday)
now.With(t).Sunday()              // 2013-11-24 00:00:00 Sun (Beginning Of Today if today is Sunday)
now.With(t).Sunday("18:19:24")    // 2013-11-24 18:19:24 Sun (Beginning Of Today if today is Sunday)
now.With(t).EndOfSunday()         // 2013-11-24 23:59:59.999999999 Sun (End of Today if today is Sunday)

Parse String to Time

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:21, 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:

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 for things you can do.

Author

jinzhu

  • http://github.com/jinzhu
  • [email protected]
  • http://twitter.com/zhangjinzhu

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

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)
With(t).EndOfMonth() // 2013-02-28 23:59:59.999999999 Thu

Monday()        // 2013-11-18 00:00:00 Mon
Monday("17:44") // 2013-11-18 17:44:00 Mon
Sunday()        // 2013-11-24 00:00:00 Sun
Sunday("17:44") // 2013-11-24 17:44:00 Sun
EndOfSunday()   // 2013-11-24 23:59:59.999999999 Sun

Index ¶

  • Variables
  • func BeginningOfDay() time.Time
  • func BeginningOfHour() time.Time
  • func BeginningOfMinute() time.Time
  • func BeginningOfMonth() time.Time
  • func BeginningOfQuarter() time.Time
  • func BeginningOfWeek() time.Time
  • func BeginningOfYear() time.Time
  • func Between(time1, time2 string) bool
  • func EndOfDay() time.Time
  • func EndOfHour() time.Time
  • func EndOfMinute() time.Time
  • func EndOfMonth() time.Time
  • func EndOfQuarter() time.Time
  • func EndOfSunday() time.Time
  • func EndOfWeek() time.Time
  • func EndOfYear() time.Time
  • func Monday(strs ...string) time.Time
  • func MustParse(strs ...string) time.Time
  • func MustParseInLocation(loc *time.Location, strs ...string) time.Time
  • func Parse(strs ...string) (time.Time, error)
  • func ParseInLocation(loc *time.Location, strs ...string) (time.Time, error)
  • func Quarter() uint
  • func Sunday(strs ...string) time.Time
  • type Config
    • func (config *Config) MustParse(strs ...string) time.Time
    • func (config *Config) Parse(strs ...string) (time.Time, error)
    • func (config *Config) With(t time.Time) *Now
  • type Now
    • func New(t time.Time) *Now
    • func With(t time.Time) *Now
    • func (now *Now) BeginningOfDay() time.Time
    • func (now *Now) BeginningOfHalf() time.Time
    • func (now *Now) BeginningOfHour() time.Time
    • func (now *Now) BeginningOfMinute() time.Time
    • func (now *Now) BeginningOfMonth() time.Time
    • func (now *Now) BeginningOfQuarter() time.Time
    • func (now *Now) BeginningOfWeek() time.Time
    • func (now *Now) BeginningOfYear() time.Time
    • func (now *Now) Between(begin, end string) bool
    • func (now *Now) EndOfDay() time.Time
    • func (now *Now) EndOfHalf() time.Time
    • func (now *Now) EndOfHour() time.Time
    • func (now *Now) EndOfMinute() time.Time
    • func (now *Now) EndOfMonth() time.Time
    • func (now *Now) EndOfQuarter() time.Time
    • func (now *Now) EndOfSunday() time.Time
    • func (now *Now) EndOfWeek() time.Time
    • func (now *Now) EndOfYear() time.Time
    • func (now *Now) Monday(strs ...string) time.Time
    • func (now *Now) MustParse(strs ...string) (t time.Time)
    • func (now *Now) Parse(strs ...string) (t time.Time, err error)
    • func (now *Now) Quarter() uint
    • func (now *Now) Sunday(strs ...string) time.Time

Examples ¶

  • Package

Constants ¶

This section is empty.

Variables ¶

View Source
var TimeFormats = []string{
	"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", "2006-01-02T15:04:05Z0700", "2006-01-02T15:04:05Z07",
	"2006.1.2", "2006.1.2 15:04:05", "2006.01.02", "2006.01.02 15:04:05", "2006.01.02 15:04:05.999999999",
	"1/2/2006", "1/2/2006 15:4:5", "2006/01/02", "20060102", "2006/01/02 15:04:05",
	time.ANSIC, time.UnixDate, time.RubyDate, time.RFC822, time.RFC822Z, time.RFC850,
	time.RFC1123, time.RFC1123Z, time.RFC3339, time.RFC3339Nano,
	time.Kitchen, time.Stamp, time.StampMilli, time.StampMicro, time.StampNano,
}

TimeFormats default time formats will be parsed as

View Source
var WeekStartDay = time.Sunday

WeekStartDay set week start day, default is sunday

Functions ¶

func BeginningOfDay ¶

func BeginningOfDay() time.Time

BeginningOfDay beginning of day

func BeginningOfHour ¶

func BeginningOfHour() time.Time

BeginningOfHour beginning of hour

func BeginningOfMinute ¶

func BeginningOfMinute() time.Time

BeginningOfMinute beginning of minute

func BeginningOfMonth ¶

func BeginningOfMonth() time.Time

BeginningOfMonth beginning of month

func BeginningOfQuarter ¶

func BeginningOfQuarter() time.Time

BeginningOfQuarter beginning of quarter

func BeginningOfWeek ¶

func BeginningOfWeek() time.Time

BeginningOfWeek beginning of week

func BeginningOfYear ¶

func BeginningOfYear() time.Time

BeginningOfYear beginning of year

func Between ¶

func Between(time1, time2 string) bool

Between check now between the begin, end time or not

func EndOfDay ¶

func EndOfDay() time.Time

EndOfDay end of day

func EndOfHour ¶

func EndOfHour() time.Time

EndOfHour end of hour

func EndOfMinute ¶

func EndOfMinute() time.Time

EndOfMinute end of minute

func EndOfMonth ¶

func EndOfMonth() time.Time

EndOfMonth end of month

func EndOfQuarter ¶

func EndOfQuarter() time.Time

EndOfQuarter end of quarter

func EndOfSunday ¶

func EndOfSunday() time.Time

EndOfSunday end of sunday

func EndOfWeek ¶

func EndOfWeek() time.Time

EndOfWeek end of week

func EndOfYear ¶

func EndOfYear() time.Time

EndOfYear end of year

func Monday ¶

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

func MustParse ¶

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

MustParse must parse string to time or will panic

func MustParseInLocation ¶

func MustParseInLocation(loc *time.Location, strs ...string) time.Time

MustParseInLocation must parse string to time in location or will panic

func Parse ¶

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

Parse parse string to time

func ParseInLocation ¶

func ParseInLocation(loc *time.Location, strs ...string) (time.Time, error)

ParseInLocation parse string to time in location

func Quarter ¶ added in v1.1.3

func Quarter() uint

Quarter returns the yearly quarter

func Sunday ¶

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

Sunday sunday

Types ¶

type Config ¶ added in v1.1.0

type Config struct {
	WeekStartDay time.Weekday
	TimeLocation *time.Location
	TimeFormats  []string
}

Config configuration for now package

var DefaultConfig *Config

DefaultConfig default config

func (*Config) MustParse ¶ added in v1.1.0

func (config *Config) MustParse(strs ...string) time.Time

MustParse must parse string to time or will panic

func (*Config) Parse ¶ added in v1.1.0

func (config *Config) Parse(strs ...string) (time.Time, error)

Parse parse string to time based on configuration

func (*Config) With ¶ added in v1.1.1

func (config *Config) With(t time.Time) *Now

New initialize Now based on configuration

type Now ¶

type Now struct {
	time.Time
	*Config
}

Now now struct

func New ¶

func New(t time.Time) *Now

New initialize Now with time

func With ¶ added in v1.1.1

func With(t time.Time) *Now

With initialize Now with time

func (*Now) BeginningOfDay ¶

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

BeginningOfDay beginning of day

func (*Now) BeginningOfHalf ¶ added in v1.0.1

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

BeginningOfHalf beginning of half year

func (*Now) BeginningOfHour ¶

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

BeginningOfHour beginning of hour

func (*Now) BeginningOfMinute ¶

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

BeginningOfMinute beginning of minute

func (*Now) BeginningOfMonth ¶

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

BeginningOfMonth beginning of month

func (*Now) BeginningOfQuarter ¶

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

BeginningOfQuarter beginning of quarter

func (*Now) BeginningOfWeek ¶

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

BeginningOfWeek beginning of week

func (*Now) BeginningOfYear ¶

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

BeginningOfYear BeginningOfYear beginning of year

func (*Now) Between ¶

func (now *Now) Between(begin, end string) bool

Between check time between the begin, end time or not

func (*Now) EndOfDay ¶

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

EndOfDay end of day

func (*Now) EndOfHalf ¶ added in v1.0.1

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

EndOfHalf end of half year

func (*Now) EndOfHour ¶

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

EndOfHour end of hour

func (*Now) EndOfMinute ¶

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

EndOfMinute end of minute

func (*Now) EndOfMonth ¶

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

EndOfMonth end of month

func (*Now) EndOfQuarter ¶

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

EndOfQuarter end of quarter

func (*Now) EndOfSunday ¶

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

EndOfSunday end of sunday

func (*Now) EndOfWeek ¶

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

EndOfWeek end of week

func (*Now) EndOfYear ¶

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

EndOfYear end of year

func (*Now) Monday ¶

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

func (*Now) MustParse ¶

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

MustParse must parse string to time or it will panic

func (*Now) Parse ¶

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

Parse parse string to time

func (*Now) Quarter ¶ added in v1.1.3

func (now *Now) Quarter() uint

Quarter returns the yearly quarter

func (*Now) Sunday ¶

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

Source Files ¶

View all Source files
  • main.go
  • now.go
  • time.go
Click to show internal directories.
Click to hide internal directories.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.