Skip to content

Commit

Permalink
Can set start day (#7)
Browse files Browse the repository at this point in the history
* started using firstdayofweek

* removed redundant variable
  • Loading branch information
kofoworola authored Apr 30, 2019
1 parent 0e0c619 commit 02c16f7
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 69 deletions.
113 changes: 59 additions & 54 deletions godate.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,55 +7,56 @@ import (
"time"
)

type GoDate struct {
type goDate struct {
Time time.Time
TimeZone *time.Location
FirstDayOfWeek time.Weekday
}

//IsBefore checks if the GoDate is before the passed GoDate
func (d *GoDate) IsBefore(compare *GoDate) bool {
//IsBefore checks if the goDate is before the passed goDate
func (d *goDate) IsBefore(compare *goDate) bool {
return d.Time.Before(compare.Time)
}

//IsAfter checks if the GoDate is before the passed GoDate
func (d *GoDate) IsAfter(compare *GoDate) bool {
//IsAfter checks if the goDate is before the passed goDate
func (d *goDate) IsAfter(compare *goDate) bool {
return d.Time.After(compare.Time)
}

//Sub subtracts the 'count' from the GoDate using the unit passed
func (d GoDate) Sub(count int, unit Unit) *GoDate {
//Sub subtracts the 'count' from the goDate using the unit passed
func (d goDate) Sub(count int, unit Unit) *goDate {
return d.Add(-count, unit)
}

//Add adds the 'count' from the GoDate using the unit passed
func (d GoDate) Add(count int, unit Unit) *GoDate {
//Add adds the 'count' from the goDate using the unit passed
func (d goDate) Add(count int, unit Unit) *goDate {
d.Time = d.Time.Add(time.Duration(unit * Unit(count)))
return &d
}

//Get the difference as a duration type
func (d *GoDate) DifferenceAsDuration(compare *GoDate) time.Duration {
func (d *goDate) DifferenceAsDuration(compare *goDate) time.Duration {
return d.Time.Sub(compare.Time)
}

//Difference Returns the difference between the Godate and another in the specified unit
//If the difference is negative then the 'compare' date occurs after the date
//Else it occurs before the the date
func (d GoDate) Difference(compare *GoDate, unit Unit) int {
func (d goDate) Difference(compare *goDate, unit Unit) int {
difference := d.DifferenceAsFloat(compare, unit)
return int(difference)
}

//Get the difference as a float
func (d GoDate) DifferenceAsFloat(compare *GoDate, unit Unit) float64 {
func (d goDate) DifferenceAsFloat(compare *goDate, unit Unit) float64 {
duration := d.DifferenceAsDuration(compare)
return float64(duration) / float64(time.Duration(unit))
}

//Gets the difference between the relative to the date value in the form of
//1 month before
//1 month after
func (d GoDate) DifferenceForHumans(compare *GoDate) string {
func (d goDate) DifferenceForHumans(compare *goDate) string {
differenceString, differenceInt := d.AbsDifferenceForHumans(compare)
if differenceInt > 0 {
return differenceString + " before"
Expand All @@ -67,7 +68,7 @@ func (d GoDate) DifferenceForHumans(compare *GoDate) string {
//Gets the difference between the relative to current time value in the form of
//1 month ago
//1 month from now
func (d GoDate) DifferenceFromNowForHumans() string {
func (d goDate) DifferenceFromNowForHumans() string {
now := Now(d.TimeZone)
differenceString, differenceInt := now.AbsDifferenceForHumans(&d)
if differenceInt > 0 {
Expand All @@ -80,7 +81,7 @@ func (d GoDate) DifferenceFromNowForHumans() string {
//Get the abs difference relative to compare time in the form
//1 month
//2 days
func (d GoDate) AbsDifferenceForHumans(compare *GoDate) (string, int) {
func (d goDate) AbsDifferenceForHumans(compare *goDate) (string, int) {
sentence := make([]string, 2, 2)
duration := Unit(math.Abs(float64(d.DifferenceAsDuration(compare))))
var unit Unit
Expand Down Expand Up @@ -108,112 +109,116 @@ func (d GoDate) AbsDifferenceForHumans(compare *GoDate) (string, int) {
return strings.Join(sentence, " "), difference
}

func (d *GoDate) StartOfHour() *GoDate {
func (d *goDate) StartOfHour() *goDate {
y, m, day := d.Time.Date()
return &GoDate{time.Date(y, m, day, d.Time.Hour(), 0, 0, 0, d.TimeZone), d.TimeZone}
return &goDate{time.Date(y, m, day, d.Time.Hour(), 0, 0, 0, d.TimeZone), d.TimeZone,0}
}

func (d *GoDate) StartOfDay() *GoDate {
func (d *goDate) StartOfDay() *goDate {
y, m, day := d.Time.Date()
return &GoDate{time.Date(y, m, day, 0, 0, 0, 0, d.TimeZone), d.TimeZone}
return &goDate{time.Date(y, m, day, 0, 0, 0, 0, d.TimeZone), d.TimeZone,0}
}

func (d *GoDate) StartOfWeek() *GoDate {
func (d *goDate) StartOfWeek() *goDate {
day := d.StartOfDay().Time.Weekday()
if day != FirstDayOfWeek {
return d.Sub(int(day-FirstDayOfWeek), DAY).StartOfDay()
if firstDay := d.FirstDayOfWeek; day != firstDay {
return d.Sub(int(day-firstDay), DAY).StartOfDay()
} else{
return d.StartOfDay()
}
}

func (d *GoDate) StartOfMonth() *GoDate {
func (d *goDate) StartOfMonth() *goDate {
y, m, _ := d.Time.Date()
return &GoDate{time.Date(y, m, 1, 0, 0, 0, 0, d.TimeZone), d.TimeZone}
return &goDate{time.Date(y, m, 1, 0, 0, 0, 0, d.TimeZone), d.TimeZone,0}
}

func (d *GoDate) StartOfQuarter() *GoDate {
func (d *goDate) StartOfQuarter() *goDate {
startMonth := d.StartOfMonth()
off := (startMonth.Time.Month() - 1) % 3
return startMonth.Sub(int(off), MONTH)
}

func (d *GoDate) StartOfYear() *GoDate {
func (d *goDate) StartOfYear() *goDate {
y, _, _ := d.Time.Date()
return &GoDate{time.Date(y, 1, 1, 0, 0, 0, 0, d.TimeZone), d.TimeZone}
return &goDate{time.Date(y, 1, 1, 0, 0, 0, 0, d.TimeZone), d.TimeZone,0}
}

func (d *GoDate) EndOfHour() *GoDate {
func (d *goDate) EndOfHour() *goDate {
nextHour := d.StartOfHour().Add(1, HOUR)
return &GoDate{nextHour.Time.Add(-time.Millisecond), d.TimeZone}
return &goDate{nextHour.Time.Add(-time.Millisecond), d.TimeZone,0}
}

func (d *GoDate) EndOfDay() *GoDate {
func (d *goDate) EndOfDay() *goDate {
nextDay := d.StartOfDay().Add(1, DAY)
return &GoDate{nextDay.Time.Add(-time.Millisecond), d.TimeZone}
return &goDate{nextDay.Time.Add(-time.Millisecond), d.TimeZone,0}
}

func (d *GoDate) EndOfWeek() *GoDate {
func (d *goDate) EndOfWeek() *goDate {
nextWeek := d.StartOfWeek().Add(1, WEEK)
return &GoDate{nextWeek.Time.Add(-time.Millisecond), d.TimeZone}
return &goDate{nextWeek.Time.Add(-time.Millisecond), d.TimeZone,0}
}

func (d *GoDate) EndOfMonth() *GoDate {
func (d *goDate) EndOfMonth() *goDate {
nextWeek := d.StartOfMonth().Add(1, MONTH)
return &GoDate{nextWeek.Time.Add(-time.Millisecond), d.TimeZone}
return &goDate{nextWeek.Time.Add(-time.Millisecond), d.TimeZone,0}
}

func (d *GoDate) EndOfQuarter() *GoDate {
func (d *goDate) EndOfQuarter() *goDate {
nextWeek := d.StartOfQuarter().Add(3, MONTH)
return &GoDate{nextWeek.Time.Add(-time.Millisecond), d.TimeZone}
return &goDate{nextWeek.Time.Add(-time.Millisecond), d.TimeZone,0}
}

func (d *GoDate) EndOfYear() *GoDate {
func (d *goDate) EndOfYear() *goDate {
nextWeek := d.StartOfYear().Add(1, MONTH)
return &GoDate{nextWeek.Time.Add(-time.Millisecond), d.TimeZone}
return &goDate{nextWeek.Time.Add(-time.Millisecond), d.TimeZone,0}
}

//MidDay gets the midday time usually 12:00 PM of the current day
func (d *GoDate) MidDay() *GoDate{
func (d *goDate) MidDay() *goDate {
y, m, day := d.Time.Date()
return &GoDate{time.Date(y, m, day, 12, 0, 0, 0, d.TimeZone), d.TimeZone}
return &goDate{time.Date(y, m, day, 12, 0, 0, 0, d.TimeZone), d.TimeZone,0}
}

//ToDateTimeString Formats and returns the GoDate in the form 2006-01-02 15:04:05
func (d *GoDate) ToDateTimeString() string{
//ToDateTimeString Formats and returns the goDate in the form 2006-01-02 15:04:05
func (d *goDate) ToDateTimeString() string{
return d.Format("2006-01-02 15:04:05")
}

//ToDateString Formats and returns the GoDate in the form 2006-01-02
func (d *GoDate) ToDateString() string{
//ToDateString Formats and returns the goDate in the form 2006-01-02
func (d *goDate) ToDateString() string{
return d.Format("2006-01-02")
}

//ToFormattedDateString Formats and returns the GoDate in the form Jan 02, 2006
func (d *GoDate) ToFormattedDateString() string{
//ToFormattedDateString Formats and returns the goDate in the form Jan 02, 2006
func (d *goDate) ToFormattedDateString() string{
return d.Format("Jan 02, 2006")
}

//ToTimeString Formats and returns the GoDate in the form 15:04:05
func (d *GoDate) ToTimeString() string{
//ToTimeString Formats and returns the goDate in the form 15:04:05
func (d *goDate) ToTimeString() string{
return d.Format("15:04:05")
}

//ToDayTimeString Formats and returns the GoDate in the form Mon, Jan 2, 2006 03:04 PM
func (d *GoDate) ToDayTimeString() string{
//ToDayTimeString Formats and returns the goDate in the form Mon, Jan 2, 2006 03:04 PM
func (d *goDate) ToDayTimeString() string{
return d.Format("Mon, Jan 2, 2006 03:04 PM")
}

//Check if this is the weekend
func (d *GoDate) IsWeekend() bool {
func (d *goDate) IsWeekend() bool {
day := d.Time.Weekday()
return day == time.Saturday || day == time.Sunday
}

func (d *GoDate) Format(format string) string{
func (d *goDate) Format(format string) string{
return d.Time.Format(format)
}

func (d GoDate) String() string{
func (d *goDate) SetFirstDay(weekday time.Weekday){
d.FirstDayOfWeek = weekday
}

func (d goDate) String() string{
return d.Format("Mon Jan 2 15:04:05 -0700 MST 2006")
}
14 changes: 7 additions & 7 deletions godate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,40 +73,40 @@ func TestGoDate_StartOfDay(t *testing.T) {
}

func TestGoDate_StartOfWeek(t *testing.T) {
date := GoDate{time.Date(2019,4,27,0,0,0,0,time.UTC),time.UTC}
FirstDayOfWeek = time.Sunday
if date.StartOfWeek().Time.Day() != 21{
date := goDate{time.Date(2019,4,27,0,0,0,0,time.UTC),time.UTC,0}
date.SetFirstDay(time.Tuesday)
if date.StartOfWeek().Time.Day() != 23{
t.Error("Got " + parse(date.StartOfWeek().Time))
}
}

func TestGoDate_ToDateString(t *testing.T) {
day,_ := time.Parse("2006-01-02","2019-04-29")
dayGoDate := GoDate{day,time.UTC}
dayGoDate := goDate{day,time.UTC,0}
if dateString := dayGoDate.ToDateString(); dateString != "2019-04-29"{
t.Error("Got " + dateString)
}
}

func TestGoDate_ToFormattedDateString(t *testing.T) {
day,_ := time.Parse("2006-01-02","2019-04-29")
dayGoDate := GoDate{day,time.UTC}
dayGoDate := goDate{day,time.UTC,0}
if dateString := dayGoDate.ToFormattedDateString(); dateString != "Apr 29, 2019"{
t.Error("Got " + dateString)
}
}

func TestGoDate_ToTimeString(t *testing.T) {
day,_ := time.Parse("2006-01-02","2019-04-29")
dayGoDate := (&GoDate{day,time.UTC}).StartOfDay()
dayGoDate := (&goDate{day,time.UTC,0}).StartOfDay()
if timeString := dayGoDate.ToTimeString(); timeString != "00:00:00"{
t.Error("Got " + timeString)
}
}

func TestGoDate_ToDayTimeString(t *testing.T) {
day,_ := time.Parse("2006-01-02","2019-04-29")
dayGoDate := (&GoDate{day,time.UTC}).MidDay().Add(1,HOUR)
dayGoDate := (&goDate{day,time.UTC,0}).MidDay().Add(1,HOUR)
if dateString := dayGoDate.ToDayTimeString(); dateString != "Mon, Apr 29, 2019 01:00 PM"{
t.Error("Got " + dateString)
}
Expand Down
14 changes: 6 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"time"
)

var FirstDayOfWeek = time.Monday

type Unit time.Duration

//UNIT CONSTANTS
Expand Down Expand Up @@ -33,20 +31,20 @@ func (u Unit) String() string{
return UnitStrings[u]
}

func Create(time time.Time) *GoDate{
return &GoDate{time,time.Location()}
func Create(time time.Time) *goDate {
return &goDate{time,time.Location(),0}
}

func Now(location *time.Location) *GoDate {
return &GoDate{time.Now().In(location),location}
func Now(location *time.Location) *goDate {
return &goDate{time.Now().In(location),location,0}
}

func Tomorrow(location *time.Location) *GoDate {
func Tomorrow(location *time.Location) *goDate {
tomorrow := Now(location).Add(1,DAY)
return tomorrow
}

func Yesterday(location *time.Location) *GoDate {
func Yesterday(location *time.Location) *goDate {
yesterday := Now(location).Sub(1,DAY)
return yesterday
}

0 comments on commit 02c16f7

Please sign in to comment.