generated from koddr/template-go
/
expires_datetime_switcher.go
51 lines (48 loc) · 1.09 KB
/
expires_datetime_switcher.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
package helpers
import (
"errors"
"time"
"github.com/secretium/secretium/internal/messages"
)
// ExpiresDatetimeSwitcher returns a new datetime with a duration.
func ExpiresDatetimeSwitcher(now time.Time, duration string) (time.Time, error) {
// Parse the duration.
switch duration {
case "5m":
// 5 minutes.
return now.Add(time.Minute * 5), nil
case "15m":
// 15 minutes.
return now.Add(time.Minute * 15), nil
case "30m":
// 30 minutes.
return now.Add(time.Minute * 30), nil
case "1h":
// 1 hour.
return now.Add(time.Hour * 1), nil
case "3h":
// 3 hours.
return now.Add(time.Hour * 3), nil
case "12h":
// 12 hours.
return now.Add(time.Hour * 12), nil
case "1d":
// 1 day.
return now.Add(time.Hour * 24), nil
case "3d":
// 3 days.
return now.Add(time.Hour * 24 * 3), nil
case "7d":
// 7 days.
return now.Add(time.Hour * 24 * 7), nil
case "14d":
// 14 days.
return now.Add(time.Hour * 24 * 14), nil
case "30d":
// 30 days.
return now.Add(time.Hour * 24 * 30), nil
default:
// Error.
return now, errors.New(messages.ErrSecretExpiresAtNotValid)
}
}