-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdate.go
53 lines (42 loc) · 1.51 KB
/
date.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"strconv"
"time"
)
const delimiterSlashes = "/"
const delimiterPeriods = "."
var availableDelimiters = map[string]bool{delimiterSlashes: true, delimiterPeriods: true}
// Formatdate takes in a time and returns a numeric month day year with a delimiter in between each.
func formatDate(t time.Time, delimiter string) string {
month := strconv.Itoa(int(t.Month()))
day := strconv.Itoa(t.Day())
year := strconv.Itoa(t.Year())
d := delimiterSlashes
if availableDelimiters[delimiter] {
d = delimiter
}
return month + d + day + d + year
}
// NextTuesday takes in a date and returns that date if it is Tuesday or the following Tuesday.
func nextTuesday(t time.Time) time.Time {
const tuesday = 2
const daysOfWeek = 7
t = t.AddDate(0, 0, (tuesday+(daysOfWeek-int(t.Weekday())))%daysOfWeek)
return t
}
// MonthDayCommaYear takes in a time and returns a full string month day year with a comma after day
// (ex: January 1, 2010).
func monthDayCommaYear(t time.Time) string {
day := strconv.Itoa(t.Day())
month := t.Month().String()
year := strconv.Itoa(t.Year())
return month + " " + day + ", " + year
}
// AddMinutes takes in a time and minutes to add to that time and returns the new time and a string
// representation (ex: 10:44).
func addMinutes(curTime time.Time, minToAdd int) (time.Time, string) {
nextTime := time.Minute * time.Duration(minToAdd)
curTime = curTime.Add(nextTime)
hour, min, _ := curTime.Clock() //Ignore seconds
return curTime, strconv.Itoa(hour) + ":" + strconv.Itoa(min)
}