Skip to content

Commit 9d98581

Browse files
committedDec 24, 2021
init
1 parent 24630a7 commit 9d98581

9 files changed

+1045
-2
lines changed
 

‎.gitignore

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@
1111
# Output of the go coverage tool, specifically when used with LiteIDE
1212
*.out
1313

14-
# Dependency directories (remove the comment below to include it)
15-
# vendor/
14+
.idea
15+
.vscode

‎clock.go

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package store
2+
3+
import (
4+
"fmt"
5+
"time"
6+
)
7+
8+
// Clock is a wrapper for time.time to allow parsing datetime stamp with time only in
9+
// ISO 8601 format, like "15:04:05"
10+
type Clock struct{ time.Time }
11+
12+
// NewClock returns the Clock in the given location with given hours, minutes and secs
13+
func NewClock(h, m, s, ns int, loc *time.Location) Clock {
14+
return Clock{Time: time.Date(0, time.January, 1, h, m, s, ns, loc)}
15+
}
16+
17+
// ClockFromTime returns the clock extracted from the given time.Time.
18+
func ClockFromTime(t time.Time) Clock {
19+
return Clock{t}
20+
}
21+
22+
// Sub returns the duration between the clock at the date of the other time and current clock
23+
func (c Clock) Sub(other Clock) time.Duration {
24+
return c.Time.Sub(other.Time)
25+
}
26+
27+
// String implements fmt.Stringer to print and log Clock properly
28+
func (c Clock) String() string {
29+
return fmt.Sprintf("%02d:%02d:%02d %s", c.Hour(), c.Minute(), c.Second(), c.Location())
30+
}
31+
32+
// GoString implements fmt.GoStringer to use Clock in %#v formats
33+
func (c Clock) GoString() string {
34+
return fmt.Sprintf("NewClock(%d, %d, %d, %s)", c.Hour(), c.Minute(), c.Second(), c.Location())
35+
}

‎clock_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package store
2+
3+
import (
4+
"testing"
5+
"time"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
func TestClock_Sub(t *testing.T) {
11+
dur := NewClock(13, 12, 11, 10, time.UTC).Sub(NewClock(9, 8, 7, 6, time.UTC))
12+
res := 4*time.Nanosecond + 4*time.Second + 4*time.Minute + 4*time.Hour
13+
assert.Equal(t, res, dur)
14+
}

‎date.go

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package store
2+
3+
import "time"
4+
5+
// Date represents a single date without any information about the Clock.
6+
type Date struct {
7+
Year int
8+
Month time.Month
9+
Day int
10+
}
11+
12+
// Time returns the time.Time that represents this date, with Clock information provided.
13+
func (dt Date) Time(c Clock) time.Time {
14+
return time.Date(
15+
dt.Year,
16+
dt.Month,
17+
dt.Day,
18+
c.Hour(),
19+
c.Minute(),
20+
c.Second(),
21+
c.Nanosecond(),
22+
c.Location(),
23+
)
24+
}
25+
26+
// After checks that the current date is after the other date.
27+
func (dt Date) After(other Date) bool {
28+
if dt.Year == other.Year {
29+
if dt.Month == other.Month {
30+
return dt.Day > other.Day
31+
}
32+
return dt.Month > other.Month
33+
}
34+
return dt.Year > other.Year
35+
}
36+
37+
// Before checks that the current date is before the given date.
38+
func (dt Date) Before(other Date) bool {
39+
if dt.Year == other.Year {
40+
if dt.Month == other.Month {
41+
return dt.Day < other.Day
42+
}
43+
return dt.Month < other.Month
44+
}
45+
return dt.Year < other.Year
46+
}
47+
48+
// BeforeOrEqual checks that the current date is before or equal the other date.
49+
func (dt Date) BeforeOrEqual(other Date) bool {
50+
return dt.Before(other) || dt.Equal(other)
51+
}
52+
53+
// AfterOrEqual checks that the current date is after or equal the other date.
54+
func (dt Date) AfterOrEqual(other Date) bool {
55+
return dt.After(other) || dt.Equal(other)
56+
}
57+
58+
// Equal returns true if the dates are the same.
59+
func (dt Date) Equal(other Date) bool {
60+
return dt.Year == other.Year && dt.Month == other.Month && dt.Day == other.Day
61+
}
62+
63+
// Add some time to the current date.
64+
func (dt Date) Add(y int, m int, d int) Date {
65+
return DateFromTime(time.Date(
66+
dt.Year+y, dt.Month+time.Month(m), dt.Day+d,
67+
0, 0, 0, 0, time.UTC))
68+
}
69+
70+
// DateFromTime returns the Date extracted from the given time.Time
71+
func DateFromTime(t time.Time) Date {
72+
y, m, d := t.Date()
73+
return Date{Year: y, Month: m, Day: d}
74+
}

0 commit comments

Comments
 (0)