-
Notifications
You must be signed in to change notification settings - Fork 9
/
datehelpers.go
62 lines (54 loc) · 2.1 KB
/
datehelpers.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
57
58
59
60
61
62
package keycloakb
import (
"regexp"
"strconv"
"time"
errorhandler "github.com/cloudtrust/common-service/errors"
stats_api "github.com/cloudtrust/keycloak-bridge/api/statistics"
"github.com/cloudtrust/keycloak-bridge/internal/constants"
msg "github.com/cloudtrust/keycloak-bridge/internal/constants"
)
// ConvertMinutesShift converts a string describing a timezone shift to a numeric value
func ConvertMinutesShift(value string) (int, error) {
if ok, err := regexp.MatchString(stats_api.RegExpTimeshift, value); err != nil || !ok {
return 0, errorhandler.CreateInvalidQueryParameterError(msg.Timeshift)
}
res, _ := strconv.Atoi(value)
return res, nil
}
// NextHour returns a time.Time value of the provided time rounded to the next hour of the associated locale
func NextHour(ref time.Time) time.Time {
return time.Date(ref.Year(), ref.Month(), ref.Day(), ref.Hour(), 0, 0, 0, ref.Location()).Add(time.Hour).UTC()
}
// NextDay returns a time.Time value of the provided time rounded to the next month of the associated locale
func NextDay(ref time.Time) time.Time {
return time.Date(ref.Year(), ref.Month(), ref.Day(), 0, 0, 0, 0, ref.Location()).Add(time.Duration(24) * time.Hour).UTC()
}
// ThisMonth returns a time.Time value of the provided time rounded to the beginning of the current month of the associated locale
func ThisMonth(ref time.Time) time.Time {
return time.Date(ref.Year(), ref.Month(), 1, 0, 0, 0, 0, ref.Location()).UTC()
}
// NextMonth returns a time.Time value of the provided time rounded to the next month of the associated locale
func NextMonth(ref time.Time) time.Time {
var year = ref.Year()
var month = ref.Month()
if month == time.December {
month = time.January
year++
} else {
month = time.Month(int(month) + 1)
}
return time.Date(year, month, 1, 0, 0, 0, 0, ref.Location()).UTC()
}
// IsDateInThePast tells if a date is in the past or not
func IsDateInThePast(value *string) *bool {
if value == nil {
return nil
}
var date, err = time.Parse(constants.SupportedDateLayouts[0], *value)
if err != nil {
return nil
}
var expired = date.Before(time.Now())
return &expired
}