-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtime_arithmetic.go
More file actions
54 lines (50 loc) · 1.76 KB
/
time_arithmetic.go
File metadata and controls
54 lines (50 loc) · 1.76 KB
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
package gotime
import "time"
// Hours returns the time after adding the specified number of hours to the given time.
// If no time is provided, it uses the current time. Negative values subtract hours.
//
// Example:
// futureTime := gotime.Hours(5) // 5 hours from now
// pastTime := gotime.Hours(-2, someTime) // 2 hours before someTime
// noChange := gotime.Hours(0) // same as time.Now()
func Hours(hours int, dt ...time.Time) time.Time {
var t time.Time
if len(dt) > 0 {
t = dt[0]
} else {
t = time.Now()
}
return t.Add(time.Duration(hours) * time.Hour)
}
// Minutes returns the time after adding the specified number of minutes to the given time.
// If no time is provided, it uses the current time. Negative values subtract minutes.
//
// Example:
// futureTime := gotime.Minutes(30) // 30 minutes from now
// pastTime := gotime.Minutes(-15, someTime) // 15 minutes before someTime
// noChange := gotime.Minutes(0) // same as time.Now()
func Minutes(minutes int, dt ...time.Time) time.Time {
var t time.Time
if len(dt) > 0 {
t = dt[0]
} else {
t = time.Now()
}
return t.Add(time.Duration(minutes) * time.Minute)
}
// Seconds returns the time after adding the specified number of seconds to the given time.
// If no time is provided, it uses the current time. Negative values subtract seconds.
//
// Example:
// futureTime := gotime.Seconds(45) // 45 seconds from now
// pastTime := gotime.Seconds(-30, someTime) // 30 seconds before someTime
// noChange := gotime.Seconds(0) // same as time.Now()
func Seconds(seconds int, dt ...time.Time) time.Time {
var t time.Time
if len(dt) > 0 {
t = dt[0]
} else {
t = time.Now()
}
return t.Add(time.Duration(seconds) * time.Second)
}