/
time.go
54 lines (43 loc) 路 1.24 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
package utils
import (
"github.com/SemmiDev/fiber-go-clean-arch/api/domain/constants"
"log"
"time"
)
type Time struct {
time.Time
Date time.Time
Now time.Time
Location *time.Location
}
func NewTime(timeConfig ...Time) *Time {
location, err := time.LoadLocation(constants.Timezone)
if err != nil {
log.Fatal(err)
}
var newConfig Time
if len(timeConfig) > 0 {
newConfig = timeConfig[0]
newConfig.Location = location
return &newConfig
}
return &Time{
Now: time.Now(),
Location: location,
}
}
func (t *Time) DaysInMonth(i time.Time) int {
return i.AddDate(0, 1, 0).Add(time.Nanosecond * -1).Day()
}
func (t *Time) StartDate() time.Time {
return time.Date(t.Now.Year(), t.Now.Month(), 1, 0, 0, 0, 0, t.Location)
}
func (t *Time) EndDate() time.Time {
return time.Date(t.Now.Year(), t.Now.Month(), t.DaysInMonth(t.StartDate()), 23, 59, 59, 59, t.Location)
}
func (t *Time) FormatDate() time.Time {
return time.Date(t.Date.Year(), t.Date.Month(), t.Date.Day(), t.Date.Hour(), t.Date.Minute(), t.Date.Second(), t.Date.Nanosecond(), t.Location)
}
func (t *Time) FormatDateWithInput(i time.Time) time.Time {
return time.Date(i.Year(), i.Month(), i.Day(), i.Hour(), i.Minute(), i.Second(), i.Nanosecond(), t.Location)
}