Skip to content

Commit

Permalink
Improved documentation and go-fmt.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rick committed Dec 4, 2015
1 parent c9f0d50 commit 6406c10
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 36 deletions.
28 changes: 14 additions & 14 deletions clock.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@
package date

import (
"time"
"fmt"
"strconv"
"time"
)

const zero time.Duration = 0

// Clock specifies a time of day. It extends the existing time.Duration, applying
// that to the time since midnight on some arbitrary day.
// that to the time since midnight (on some arbitrary day in some arbitrary timezone).
//
// It is not intended that Clock be used to represent periods greater than 24 hours nor
// negative values. However, for such lengths of time, a fixed 24 hours per day
// is assumed and a modulo operation Mod() is provided to discard whole multiples of 24 hours.
// is assumed and a modulo operation Mod24 is provided to discard whole multiples of 24 hours.
//
// See https://en.wikipedia.org/wiki/ISO_8601#Times
type Clock time.Duration

const (
// ClockDay is a fixed period of 24 hours. This does not take account of daylight savings, so is not fully general.
// ClockDay is a fixed period of 24 hours. This does not take account of daylight savings, so is not fully general.
ClockDay Clock = Clock(time.Hour * 24)
ClockHour Clock = Clock(time.Hour)
ClockMinute Clock = Clock(time.Minute)
Expand Down Expand Up @@ -126,7 +126,7 @@ func (c Clock) IsInOneDay() bool {
// clock range 0s to 23h59m59s, for which IsInOneDay() returns true.
func (c Clock) Days() int {
if c < 0 {
return int(c / ClockDay) - 1
return int(c/ClockDay) - 1
} else {
return int(c / ClockDay)
}
Expand All @@ -140,7 +140,7 @@ func (c Clock) Mod24() Clock {
return c
}
if c < 0 {
q := 1 - c / ClockDay
q := 1 - c/ClockDay
return c + (q * ClockDay)
}
q := c / ClockDay
Expand Down Expand Up @@ -175,15 +175,15 @@ func clockHours(cm Clock) Clock {
}

func clockMinutes(cm Clock) Clock {
return (cm - clockHours(cm) * ClockHour) / ClockMinute
return (cm - clockHours(cm)*ClockHour) / ClockMinute
}

func clockSeconds(cm Clock) Clock {
return (cm - clockHours(cm) * ClockHour - clockMinutes(cm) * ClockMinute) / ClockSecond
return (cm - clockHours(cm)*ClockHour - clockMinutes(cm)*ClockMinute) / ClockSecond
}

func clockNanosec(cm Clock) Clock {
return cm - clockHours(cm) * ClockHour - clockMinutes(cm) * ClockMinute - clockSeconds(cm) * ClockSecond
return cm - clockHours(cm)*ClockHour - clockMinutes(cm)*ClockMinute - clockSeconds(cm)*ClockSecond
}

// Hh gets the clock-face number of hours as a two-digit string (calculated from the modulo time, see Mod24).
Expand All @@ -192,22 +192,22 @@ func (c Clock) Hh() string {
return fmt.Sprintf("%02d", clockHours(cm))
}

// HhMm gets the clock-face number of hours and minutes as a five-digit string (calculated from the
// modulo time, see Mod24).
// HhMm gets the clock-face number of hours and minutes as a five-character ISO-8601 time string (calculated
// from the modulo time, see Mod24).
func (c Clock) HhMm() string {
cm := c.Mod24()
return fmt.Sprintf("%02d:%02d", clockHours(cm), clockMinutes(cm))
}

// HhMmSs gets the clock-face number of hours, minutes, seconds as an eight-digit string
// HhMmSs gets the clock-face number of hours, minutes, seconds as an eight-character ISO-8601 time string
// (calculated from the modulo time, see Mod24).
func (c Clock) HhMmSs() string {
cm := c.Mod24()
return fmt.Sprintf("%02d:%02d:%02d", clockHours(cm), clockMinutes(cm), clockSeconds(cm))
}

// String gets the clock-face number of hours, minutes, seconds and nanoseconds as an 18-digit string
// (calculated from the modulo time, see Mod24).
// String gets the clock-face number of hours, minutes, seconds and nanoseconds as an 18-character ISO-8601
// time string (calculated from the modulo time, see Mod24).
func (c Clock) String() string {
cm := c.Mod24()
return fmt.Sprintf("%02d:%02d:%02d.%09d", clockHours(cm), clockMinutes(cm), clockSeconds(cm), clockNanosec(cm))
Expand Down
4 changes: 2 additions & 2 deletions clock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
package date

import (
"time"
"testing"
"time"
)

func TestClockHoursMinutesSeconds(t *testing.T) {
Expand Down Expand Up @@ -111,7 +111,7 @@ func TestClockString(t *testing.T) {
{-1, -1, -1, -1, "22", "22:58", "22:58:58", "22:58:58.999999999"},
}
for _, c := range cases {
d := Clock(c.h * time.Hour + c.m * time.Minute + c.s * time.Second + c.ns)
d := Clock(c.h*time.Hour + c.m*time.Minute + c.s*time.Second + c.ns)
if d.Hh() != c.hh {
t.Errorf("%d, %d, %d, %d, got %v, want %v", c.h, c.m, c.s, c.ns, d.Hh(), c.hh)
}
Expand Down
16 changes: 8 additions & 8 deletions date_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
package date

import (
"runtime/debug"
"testing"
"time"
"runtime/debug"
)

func same(d Date, t time.Time) bool {
yd, wd := d.ISOWeek()
yt, wt := t.ISOWeek()
return d.Year() == t.Year() &&
d.Month() == t.Month() &&
d.Day() == t.Day() &&
d.Weekday() == t.Weekday() &&
d.YearDay() == t.YearDay() &&
yd == yt && wd == wt
d.Month() == t.Month() &&
d.Day() == t.Day() &&
d.Weekday() == t.Weekday() &&
d.YearDay() == t.YearDay() &&
yd == yt && wd == wt
}

func TestNew(t *testing.T) {
Expand Down Expand Up @@ -63,7 +63,7 @@ func TestToday(t *testing.T) {
}
cases := []int{-10, -5, -3, 0, 1, 4, 8, 12}
for _, c := range cases {
location := time.FixedZone("zone", c * 60 * 60)
location := time.FixedZone("zone", c*60*60)
today = TodayIn(location)
now = time.Now().In(location)
if !same(today, now) {
Expand Down Expand Up @@ -105,7 +105,7 @@ func TestTime(t *testing.T) {
t.Errorf("TimeLocal(%v) == %v, want %v", d, tLocal.Location(), time.Local)
}
for _, z := range zones {
location := time.FixedZone("zone", z * 60 * 60)
location := time.FixedZone("zone", z*60*60)
tInLoc := d.In(location)
if !same(d, tInLoc) {
t.Errorf("TimeIn(%v) == %v, want date part %v", d, tInLoc, d)
Expand Down
4 changes: 2 additions & 2 deletions timespan/daterange.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ func NewDateRange(start, end Date) DateRange {
// NewYearOf constructs the range encompassing the whole year specified.
func NewYearOf(year int) DateRange {
start := New(year, time.January, 1)
end := New(year + 1, time.January, 1)
end := New(year+1, time.January, 1)
return DateRange{start, PeriodOfDays(end.Sub(start))}
}

// NewMonthOf constructs the range encompassing the whole month specified for a given year.
// It handles leap years correctly.
func NewMonthOf(year int, month time.Month) DateRange {
start := New(year, month, 1)
endT := time.Date(year, month + 1, 1, 0, 0, 0, 0, time.UTC)
endT := time.Date(year, month+1, 1, 0, 0, 0, 0, time.UTC)
end := NewAt(endT)
return DateRange{start, PeriodOfDays(end.Sub(start))}
}
Expand Down
20 changes: 10 additions & 10 deletions timespan/timespan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
package timespan

import (
"github.com/rickb777/date"
"testing"
"time"
"github.com/rickb777/date"
)

const zero time.Duration = 0
Expand All @@ -33,13 +33,13 @@ func TestNewTimeSpan(t *testing.T) {

ts2 := NewTimeSpan(t0327, t0328)
isEq(t, ts2.mark, t0327)
isEq(t, ts2.Duration(), time.Hour * 24)
isEq(t, ts2.Duration(), time.Hour*24)
isEq(t, ts2.IsEmpty(), false)
isEq(t, ts2.End(), t0328)

ts3 := NewTimeSpan(t0329, t0327)
isEq(t, ts3.mark, t0327)
isEq(t, ts3.Duration(), time.Hour * 48)
isEq(t, ts3.Duration(), time.Hour*48)
isEq(t, ts3.IsEmpty(), false)
isEq(t, ts3.End(), t0329)
}
Expand All @@ -58,31 +58,31 @@ func TestTSEnd(t *testing.T) {
func TestTSShiftBy(t *testing.T) {
ts1 := NewTimeSpan(t0327, t0328).ShiftBy(time.Hour * 24)
isEq(t, ts1.mark, t0328)
isEq(t, ts1.Duration(), time.Hour * 24)
isEq(t, ts1.Duration(), time.Hour*24)
isEq(t, ts1.End(), t0329)

ts2 := NewTimeSpan(t0328, t0329).ShiftBy(-time.Hour * 24)
isEq(t, ts2.mark, t0327)
isEq(t, ts2.Duration(), time.Hour * 24)
isEq(t, ts2.Duration(), time.Hour*24)
isEq(t, ts2.End(), t0328)
}

func TestTSExtendBy(t *testing.T) {
ts1 := NewTimeSpan(t0327, t0328).ExtendBy(time.Hour * 24)
isEq(t, ts1.mark, t0327)
isEq(t, ts1.Duration(), time.Hour * 48)
isEq(t, ts1.Duration(), time.Hour*48)
isEq(t, ts1.End(), t0329)

ts2 := NewTimeSpan(t0328, t0329).ExtendBy(-time.Hour * 48)
isEq(t, ts2.mark, t0327)
isEq(t, ts2.Duration(), time.Hour * 24)
isEq(t, ts2.Duration(), time.Hour*24)
isEq(t, ts2.End(), t0328)
}

func TestTSExtendWithoutWrapping(t *testing.T) {
ts1 := NewTimeSpan(t0327, t0328).ExtendWithoutWrapping(time.Hour * 24)
isEq(t, ts1.mark, t0327)
isEq(t, ts1.Duration(), time.Hour * 48)
isEq(t, ts1.Duration(), time.Hour*48)
isEq(t, ts1.End(), t0329)

ts2 := NewTimeSpan(t0328, t0329).ExtendWithoutWrapping(-time.Hour * 48)
Expand Down Expand Up @@ -182,7 +182,7 @@ func TestConversion2(t *testing.T) {
isEq(t, dr.Start(), d0327)
isEq(t, dr.End(), d0328)
isEq(t, ts1, ts2)
isEq(t, ts1.Duration(), time.Hour * 24)
isEq(t, ts1.Duration(), time.Hour*24)
}

func TestConversion3(t *testing.T) {
Expand All @@ -194,5 +194,5 @@ func TestConversion3(t *testing.T) {
isEq(t, dr1.End(), d0330)
isEq(t, dr1, dr2)
isEq(t, ts1, ts2)
isEq(t, ts1.Duration(), time.Hour * 71)
isEq(t, ts1.Duration(), time.Hour*71)
}

0 comments on commit 6406c10

Please sign in to comment.