• Stars
    star
    4,349
  • Rank 9,397 (Top 0.2 %)
  • Language
    Go
  • License
    MIT License
  • Created over 10 years ago
  • Updated 12 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Now is a time toolkit for golang

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

License

Released under the MIT License.

More Repositories

1

copier

Copier for golang, copy value from struct to struct and more
Go
5,034
star
2

configor

Golang Configuration tool that support YAML, JSON, TOML, Shell Environment
Go
1,697
star
3

vrome

Vrome: Bringing Vim elegance to Chrome
CoffeeScript
617
star
4

inflection

Pluralizes and singularizes English nouns
Go
475
star
5

zeal-at-point

Search the word at point with Zeal (Emacs)
Emacs Lisp
166
star
6

configure

My dot files for Emacs, Openbox, XMonad, VIM, Golang, Zsh/Bash, tmux, URXVT, ArchLinux, Git, Ruby/Rails, Xbindkey, Vrome...
Emacs Lisp
89
star
7

grb

A tool to simplify working with remote git branches
Go
42
star
8

ipparse

IP Parse,Parse IP
Ruby
20
star
9

capistrano-confirm

Confirm before deploy
Ruby
16
star
10

mail

A Go Email Utility
Go
16
star
11

cacheAjax

jquery plugin to cache ajax request and more.
JavaScript
13
star
12

paygent

Ruby wrapper for paygent
Ruby
9
star
13

backupit

BackupIt: A tool to setup your backup server (backup multi servers once).
Ruby
8
star
14

yac

Yet Another Cheat
Ruby
7
star
15

mycheat

My personal cheat sheet repository for yac
6
star
16

super_list

Ruby
5
star
17

cheat

Public cheat sheet repository for yac
5
star
18

bitpay

Bitpay is a wrapper for chinese payment gateways. (unfinished, but alipay is ready to use...)
Ruby
5
star
19

cookieless

Cookieless is a rack middleware to make your application works with cookie-less devices/browsers without change your application!
Ruby
4
star
20

javascript_localize

javascript localize solution
Ruby
3
star
21

acts_as_tagtree

Tag? Tree? Tags like Tree
Ruby
3
star
22

redgreen

Ruby
2
star
23

jinzhu.github.com

My Github Page
JavaScript
2
star
24

eo

Eo_oE : EasyOperate
Ruby
2
star
25

hz_on_fly

convert hankaku to zenkaku/zenkaku to hankaku on fly
Ruby
2
star
26

acts_as_param

acts as param
Ruby
2
star
27

shanghaionrails

http://jinzhu.github.com/shanghaionrails/
2
star
28

phase_diagram

็›ธๅ›พๅˆ†ๆž
Ruby
2
star
29

testingmachine

Testing Machine is a automated testing tool, make keyword driven testing even easier.
Ruby
1
star
30

jquery-tab-slider

jQuery tab slider
JavaScript
1
star
31

.github

my .github
1
star
32

qLayout

JavaScript
1
star
33

omniauth-t163

omniauth for t163 (working)
Ruby
1
star
34

acts_as_node

Ruby
1
star
35

devise

Ruby
1
star
36

newplugin

newPlugin - New Plugin Generator, make it dead easy to create a new rails plugin.
Ruby
1
star
37

enconverter

Encoding Converter, rack middleware used to convert encoding.
Ruby
1
star
38

jslib

a collection of javascript utils
JavaScript
1
star