-
-
Notifications
You must be signed in to change notification settings - Fork 261
Expand file tree
/
Copy pathcreator.go
More file actions
executable file
·158 lines (138 loc) · 6.3 KB
/
creator.go
File metadata and controls
executable file
·158 lines (138 loc) · 6.3 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package carbon
import (
"time"
)
// CreateFromStdTime creates a Carbon instance from standard time.Time.
func CreateFromStdTime(stdTime StdTime, timezone ...string) *Carbon {
if len(timezone) == 0 {
return NewCarbon(stdTime)
}
var (
loc *Location
err error
)
if loc, err = parseTimezone(timezone...); err != nil {
return &Carbon{Error: err}
}
return NewCarbon(stdTime.In(loc))
}
// CreateFromTimestamp creates a Carbon instance from a given timestamp with second precision.
func CreateFromTimestamp(timestamp int64, timezone ...string) *Carbon {
var (
loc *Location
err error
)
if loc, err = parseTimezone(timezone...); err != nil {
return &Carbon{Error: err}
}
return NewCarbon(time.Unix(timestamp, MinNanosecond).In(loc))
}
// CreateFromTimestampMilli creates a Carbon instance from a given timestamp with millisecond precision.
func CreateFromTimestampMilli(timestampMilli int64, timezone ...string) *Carbon {
var (
loc *Location
err error
)
if loc, err = parseTimezone(timezone...); err != nil {
return &Carbon{Error: err}
}
return NewCarbon(time.Unix(timestampMilli/1e3, (timestampMilli%1e3)*1e6).In(loc))
}
// CreateFromTimestampMicro creates a Carbon instance from a given timestamp with microsecond precision.
func CreateFromTimestampMicro(timestampMicro int64, timezone ...string) *Carbon {
var (
loc *Location
err error
)
if loc, err = parseTimezone(timezone...); err != nil {
return &Carbon{Error: err}
}
return NewCarbon(time.Unix(timestampMicro/1e6, (timestampMicro%1e6)*1e3).In(loc))
}
// CreateFromTimestampNano creates a Carbon instance from a given timestamp with nanosecond precision.
func CreateFromTimestampNano(timestampNano int64, timezone ...string) *Carbon {
var (
loc *Location
err error
)
if loc, err = parseTimezone(timezone...); err != nil {
return &Carbon{Error: err}
}
return NewCarbon(time.Unix(timestampNano/1e9, timestampNano%1e9).In(loc))
}
// CreateFromDateTime creates a Carbon instance from a given date and time.
func CreateFromDateTime(year, month, day, hour, minute, second int, timezone ...string) *Carbon {
return create(year, month, day, hour, minute, second, MinNanosecond, timezone...)
}
// CreateFromDateTimeMilli creates a Carbon instance from a given date, time and millisecond.
func CreateFromDateTimeMilli(year, month, day, hour, minute, second, millisecond int, timezone ...string) *Carbon {
return create(year, month, day, hour, minute, second, millisecond*1e6, timezone...)
}
// CreateFromDateTimeMicro creates a Carbon instance from a given date, time and microsecond.
func CreateFromDateTimeMicro(year, month, day, hour, minute, second, microsecond int, timezone ...string) *Carbon {
return create(year, month, day, hour, minute, second, microsecond*1e3, timezone...)
}
// CreateFromDateTimeNano creates a Carbon instance from a given date, time and nanosecond.
func CreateFromDateTimeNano(year, month, day, hour, minute, second, nanosecond int, timezone ...string) *Carbon {
return create(year, month, day, hour, minute, second, nanosecond, timezone...)
}
// CreateFromDate creates a Carbon instance from a given date.
func CreateFromDate(year, month, day int, timezone ...string) *Carbon {
return create(year, month, day, MinHour, MinMinute, MinSecond, MinNanosecond, timezone...)
}
// CreateFromDateMilli creates a Carbon instance from a given date and millisecond.
func CreateFromDateMilli(year, month, day, millisecond int, timezone ...string) *Carbon {
return create(year, month, day, MinHour, MinMinute, MinSecond, millisecond*1e6, timezone...)
}
// CreateFromDateMicro creates a Carbon instance from a given date and microsecond.
func CreateFromDateMicro(year, month, day, microsecond int, timezone ...string) *Carbon {
return create(year, month, day, MinHour, MinMinute, MinSecond, microsecond*1e3, timezone...)
}
// CreateFromDateNano creates a Carbon instance from a given date and nanosecond.
func CreateFromDateNano(year, month, day, nanosecond int, timezone ...string) *Carbon {
return create(year, month, day, MinHour, MinMinute, MinSecond, nanosecond, timezone...)
}
// CreateFromTime creates a Carbon instance from a given time(year, month and day are taken from the current time).
func CreateFromTime(hour, minute, second int, timezone ...string) *Carbon {
year, month, day := Now(timezone...).Date()
return create(year, month, day, hour, minute, second, MinNanosecond, timezone...)
}
// CreateFromTimeMilli creates a Carbon instance from a given time and millisecond(year, month and day are taken from the current time).
func CreateFromTimeMilli(hour, minute, second, millisecond int, timezone ...string) *Carbon {
year, month, day := Now(timezone...).Date()
return create(year, month, day, hour, minute, second, millisecond*1e6, timezone...)
}
// CreateFromTimeMicro creates a Carbon instance from a given time and microsecond(year, month and day are taken from the current time).
func CreateFromTimeMicro(hour, minute, second, microsecond int, timezone ...string) *Carbon {
year, month, day := Now(timezone...).Date()
return create(year, month, day, hour, minute, second, microsecond*1e3, timezone...)
}
// CreateFromTimeNano creates a Carbon instance from a given time and nanosecond(year, month and day are taken from the current time).
func CreateFromTimeNano(hour, minute, second, nanosecond int, timezone ...string) *Carbon {
year, month, day := Now(timezone...).Date()
return create(year, month, day, hour, minute, second, nanosecond, timezone...)
}
// creates a new Carbon instance from a given date, time and nanosecond.
func create(year, month, day, hour, minute, second, nanosecond int, timezone ...string) *Carbon {
var (
loc *Location
err error
)
if loc, err = parseTimezone(timezone...); err != nil {
return &Carbon{Error: err}
}
return NewCarbon(time.Date(year, time.Month(month), day, hour, minute, second, nanosecond, loc))
}
// creates a new Carbon instance from a given date, time and nanosecond based on the existing Carbon.
func (c *Carbon) create(year, month, day, hour, minute, second, nanosecond int) *Carbon {
return &Carbon{
time: time.Date(year, time.Month(month), day, hour, minute, second, nanosecond, c.loc),
weekStartsAt: c.weekStartsAt,
weekendDays: c.weekendDays,
loc: c.loc,
lang: c.lang.Copy(),
currentLayout: c.currentLayout,
isEmpty: c.isEmpty,
Error: c.Error,
}
}