-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.go
56 lines (49 loc) · 1.7 KB
/
time.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
54
55
56
package scheduling
import (
"fmt"
"strconv"
"time"
)
const (
// I tried to change PM to AM in the format, and then it fails to read PM. but when PM in format, it reads both
// https://stackoverflow.com/questions/44924628/golang-time-parse-1122-pm-something-that-i-can-do-math-with
format = "03:04 PM"
mdNt = "12:00 AM"
)
// TimeStr : custom definition of time as string, indicated of the format above
type TimeStr string
// ToElapsedTm : just converts the string time to
func (ts TimeStr) ToElapsedTm() (int, error) {
mdntTm, _ := time.Parse(format, mdNt) // getting the midnight time
tm, err := time.Parse(format, string(ts))
if err != nil {
return -1, err
}
elapsed := tm.Sub(mdntTm).Seconds()
return int(elapsed), nil
}
// TmStrFromUnixSecs : for the unix seconds given this can convert that into TimeStr
// this application uses specific format of clock so that its compatible to PArsing of time
func TmStrFromUnixSecs(elapsed int) TimeStr {
hr, rem := elapsed/3600, elapsed%3600
min := rem / 60
// fmt.Printf("%d %d\n", hr, min)
ampm := "AM"
if hr >= 12 { // noon 12 is 12:01, while midnight 12 is 00:01
hr = hr - 12
ampm = "PM"
}
return TimeStr(fmt.Sprintf("%02d:%02d %s", hr, min, ampm))
}
// ElapsedSecondsNow : this can for any given day, calculate the seconds that have elapsed since midnight
func ElapsedSecondsNow() int {
hr, min, sec := time.Now().Clock()
return (hr * 3600) + (min * 60) + sec
}
// DisplayDateNow : date and the time suitable for displays
// Like the blogs do it 28-Mar'82 01:00:00
func DisplayDateNow() string {
yr, mn, dy := time.Now().Date()
hr, min, sec := time.Now().Clock()
return fmt.Sprintf("%d-%.3s'%s %02d:%02d:%02d", dy, mn, strconv.Itoa(yr)[2:], hr, min, sec)
}