-
-
Notifications
You must be signed in to change notification settings - Fork 261
Expand file tree
/
Copy pathcarbon.go
More file actions
77 lines (68 loc) · 1.8 KB
/
carbon.go
File metadata and controls
77 lines (68 loc) · 1.8 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
// @Package carbon
// @Description a simple, semantic and developer-friendly time package for golang
// @Source github.com/dromara/carbon
// @Document carbon.go-pkg.com
// @Developer gouguoyin
// @Email 245629560@qq.com
// Package carbon is a simple, semantic and developer-friendly time package for golang.
package carbon
import (
"time"
)
type StdTime = time.Time
type Weekday = time.Weekday
type Location = time.Location
type Duration = time.Duration
// Carbon defines a Carbon struct.
type Carbon struct {
time StdTime
weekStartsAt Weekday
weekendDays []Weekday
loc *Location
lang *Language
currentLayout string
isEmpty bool
Error error
}
// NewCarbon returns a new Carbon instance.
func NewCarbon(stdTime ...StdTime) *Carbon {
c := new(Carbon)
c.lang = NewLanguage().SetLocale(DefaultLocale)
c.weekStartsAt = DefaultWeekStartsAt
c.weekendDays = DefaultWeekendDays
c.currentLayout = DefaultLayout
if len(stdTime) > 0 {
c.time = stdTime[0]
c.loc = c.time.Location()
return c
}
c.loc, c.Error = parseTimezone(DefaultTimezone)
return c
}
// Copy returns a copy of the Carbon instance.
func (c *Carbon) Copy() *Carbon {
if c.IsNil() {
return nil
}
// Create a deep copy of weekendDays slice to avoid shared reference
weekendDays := make([]Weekday, len(c.weekendDays))
copy(weekendDays, c.weekendDays)
return &Carbon{
time: c.time,
weekStartsAt: c.weekStartsAt,
weekendDays: weekendDays,
loc: c.loc,
lang: c.lang,
currentLayout: c.currentLayout,
isEmpty: c.isEmpty,
Error: c.Error,
}
}
// Sleep sleeps for the specified duration like time.Sleep.
func Sleep(d time.Duration) {
if IsTestNow() && d > 0 {
frozenNow.testNow = frozenNow.testNow.AddDuration(d.String())
return
}
time.Sleep(d)
}