Skip to content

Commit

Permalink
Initialize
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhu committed Nov 18, 2013
0 parents commit d0df93d
Show file tree
Hide file tree
Showing 5 changed files with 271 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
guard 'gotest' do
watch(%r{\.go$})
end
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
= Now

Now is a time toolkit for golang

== Why the project named Now?

```go
now.BeginningOfDay()
```
`now` is quite readable, aha?

== But `now` is so common I can't find it with my favorite search engine

* Star it in github [https://github.com/jinzhu/now](https://github.com/jinzhu/now)
* Search it with [http://godoc.org](http://godoc.org)

== Usage

```go
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
now.BeginningOfWeek() // 2013-11-18 00:00:00 Mon
now.BeginningOfMonth() // 2013-11-01 00:00:00 Fri
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
now.EndOfWeek() // 2013-11-24 23:59:59.999999999 Sun
now.EndOfMonth() // 2013-11-30 23:59:59.999999999 Sat
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
```
67 changes: 67 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// Package now is a time toolkit for golang.
package now

import "time"

var FirstDayMonday bool

This comment has been minimized.

Copy link
@jeffallen

jeffallen Nov 18, 2013

Just use "var FirstDayMondy = false" and then you don't need the init(). And you don't need to initialize it at all, because the zero value of a bool is false.

This comment has been minimized.

Copy link
@jinzhu

jinzhu Nov 19, 2013

Author Owner

Yes, thank you for point it out. Just cleaned it.


func init() {
FirstDayMonday = false
}

type Now struct {
time time.Time
format string
}

func New(t time.Time) *Now {
return &Now{time: t}
}

func BeginningOfMinute() time.Time {
return New(time.Now()).BeginningOfMinute()
}

func BeginningOfHour() time.Time {
return New(time.Now()).BeginningOfHour()
}

func BeginningOfDay() time.Time {
return New(time.Now()).BeginningOfDay()
}

func BeginningOfWeek() time.Time {
return New(time.Now()).BeginningOfWeek()
}

func BeginningOfMonth() time.Time {
return New(time.Now()).BeginningOfMonth()
}

func BeginningOfYear() time.Time {
return New(time.Now()).BeginningOfYear()
}

func EndOfMinute() time.Time {
return New(time.Now()).EndOfMinute()
}

func EndOfHour() time.Time {
return New(time.Now()).EndOfHour()
}

func EndOfDay() time.Time {
return New(time.Now()).EndOfDay()
}

func EndOfWeek() time.Time {
return New(time.Now()).EndOfWeek()
}

func EndOfMonth() time.Time {
return New(time.Now()).EndOfMonth()
}

func EndOfYear() time.Time {
return New(time.Now()).EndOfYear()
}
66 changes: 66 additions & 0 deletions now.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package now

import "time"

func (now *Now) BeginningOfMinute() time.Time {
return now.time.Truncate(time.Minute)
}

func (now *Now) BeginningOfHour() time.Time {
return now.time.Truncate(time.Hour)
}

func (now *Now) BeginningOfDay() time.Time {
d := time.Duration(-now.time.Hour()) * time.Hour
return now.time.Truncate(time.Hour).Add(d)
}

func (now *Now) BeginningOfWeek() time.Time {
t := now.BeginningOfDay()
weekday := int(t.Weekday())
if FirstDayMonday {
if weekday == 0 {
weekday = 7
}
weekday = weekday - 1
}

d := time.Duration(-weekday) * 24 * time.Hour
return t.Truncate(time.Hour).Add(d)
}

func (now *Now) BeginningOfMonth() time.Time {
t := now.BeginningOfDay()
d := time.Duration(-int(t.Day())+1) * 24 * time.Hour
return t.Truncate(time.Hour).Add(d)
}

func (now *Now) BeginningOfYear() time.Time {
t := now.BeginningOfDay()
d := time.Duration(-int(t.YearDay())+1) * 24 * time.Hour
return t.Truncate(time.Hour).Add(d)
}

func (now *Now) EndOfMinute() time.Time {
return now.BeginningOfMinute().Add(time.Minute - time.Nanosecond)
}

func (now *Now) EndOfHour() time.Time {
return now.BeginningOfHour().Add(time.Hour - time.Nanosecond)
}

func (now *Now) EndOfDay() time.Time {
return now.BeginningOfDay().Add(24*time.Hour - time.Nanosecond)
}

func (now *Now) EndOfWeek() time.Time {
return now.BeginningOfWeek().AddDate(0, 0, 7).Add(-time.Nanosecond)
}

func (now *Now) EndOfMonth() time.Time {
return now.BeginningOfMonth().AddDate(0, 1, 0).Add(-time.Nanosecond)
}

func (now *Now) EndOfYear() time.Time {
return now.BeginningOfYear().AddDate(1, 0, 0).Add(-time.Nanosecond)
}
90 changes: 90 additions & 0 deletions now_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package now

import (
"testing"
"time"
)

var format string

func init() {
format = "2006-01-02 15:04:05.999999999"
}

func TestBeginningOf(t *testing.T) {
n := time.Date(2013, 11, 18, 17, 51, 49, 123456789, time.Now().Location())

if New(n).BeginningOfMinute().Format(format) != "2013-11-18 17:51:00" {
t.Errorf("BeginningOfMinute")
}

if New(n).BeginningOfHour().Format(format) != "2013-11-18 17:00:00" {
t.Errorf("BeginningOfHour")
}

if New(n).BeginningOfDay().Format(format) != "2013-11-18 00:00:00" {
t.Errorf("BeginningOfDay")
}

FirstDayMonday = true
if New(n).BeginningOfWeek().Format(format) != "2013-11-18 00:00:00" {
t.Errorf("BeginningOfWeek, FirstDayMonday")
}

FirstDayMonday = false
if New(n).BeginningOfWeek().Format(format) != "2013-11-17 00:00:00" {
t.Errorf("BeginningOfWeek")
}

if New(n).BeginningOfMonth().Format(format) != "2013-11-01 00:00:00" {
t.Errorf("BeginningOfMonth")
}

if New(n).BeginningOfYear().Format(format) != "2013-01-01 00:00:00" {
t.Errorf("BeginningOfYear")
}
}

func TestEndOf(t *testing.T) {
n := time.Date(2013, 11, 18, 17, 51, 49, 123456789, time.Now().Location())

if New(n).EndOfMinute().Format(format) != "2013-11-18 17:51:59.999999999" {
t.Errorf("EndOfMinute")
}

if New(n).EndOfHour().Format(format) != "2013-11-18 17:59:59.999999999" {
t.Errorf("EndOfHour")
}

if New(n).EndOfDay().Format(format) != "2013-11-18 23:59:59.999999999" {
t.Errorf("EndOfDay")
}

FirstDayMonday = true
if New(n).EndOfWeek().Format(format) != "2013-11-24 23:59:59.999999999" {
t.Errorf("EndOfWeek, FirstDayMonday")
}

FirstDayMonday = false
if New(n).EndOfWeek().Format(format) != "2013-11-23 23:59:59.999999999" {
t.Errorf("EndOfWeek")
}

if New(n).EndOfMonth().Format(format) != "2013-11-30 23:59:59.999999999" {
t.Errorf("EndOfMonth")
}

if New(n).EndOfYear().Format(format) != "2013-12-31 23:59:59.999999999" {
t.Errorf("EndOfYear")
}

n1 := time.Date(2013, 02, 18, 17, 51, 49, 123456789, time.Now().Location())
if New(n1).EndOfMonth().Format(format) != "2013-02-28 23:59:59.999999999" {
t.Errorf("EndOfMonth for 2013/02")
}

n2 := time.Date(1900, 02, 18, 17, 51, 49, 123456789, time.Now().Location())
if New(n2).EndOfMonth().Format(format) != "1900-02-28 23:59:59.999999999" {
t.Errorf("EndOfMonth")
}
}

0 comments on commit d0df93d

Please sign in to comment.