Documentation
¶
Overview ¶
Package duration parse, format and calculate duration in full and customizable units.
Index ¶
- Constants
- Variables
- func RegisterUnitSet(units []Unit) error
- type Date
- func (date Date) Add(day int) Date
- func (t Date) After(u Date) bool
- func (t Date) Before(u Date) bool
- func (t Date) Equal(u Date) bool
- func (date Date) MarshalJSON() ([]byte, error)
- func (date *Date) Scan(value interface{}) error
- func (date Date) String() string
- func (t Date) Sub(u Date) time.Duration
- func (date *Date) UnmarshalJSON(b []byte) error
- func (date Date) Value() (driver.Value, error)
- type Duration
- type HMS
- type Sleep
- func (s *Sleep) Asleep() bool
- func (s *Sleep) Awake(event interface{})
- func (s *Sleep) AwakeAt(at time.Time, event interface{})
- func (s *Sleep) AwakeAtEalier(at time.Time, event interface{})
- func (s *Sleep) AwakeAtLater(at time.Time, event interface{})
- func (s *Sleep) ClearAwakeAt()
- func (s *Sleep) GetAwakeAt() time.Time
- func (s *Sleep) Run() interface{}
- func (s *Sleep) SetAwakeAt(at time.Time, event interface{})
- func (s *Sleep) Sleep(d time.Duration, event interface{}) interface{}
- type Time
- type Unit
Examples ¶
Constants ¶
const ( Year int64 = 365 * Day Month = 30 * Day Week = 7 * Day Day = 24 * Hour Hour = 60 * Minute Minute = 60 * Second Second = 1000 * Millisecond Millisecond = 1000 * Microsecond Microsecond = 1000 * Nanosecond Nanosecond = 1 )
Variables ¶
var EN = []Unit{ {Value: Year, Name: "Y", OtherNames: []string{"year", "years"}}, {Value: Month, Name: "M", OtherNames: []string{"month", "months"}}, {Value: Week, Name: "", OtherNames: []string{"W", "week", "weeks"}}, {Value: Day, Name: "D", OtherNames: []string{"day", "days"}}, {Value: Hour, Name: "h", OtherNames: []string{"hour", "hours"}}, {Value: Minute, Name: "m", OtherNames: []string{"minute", "minutes"}}, {Value: Second, Name: "s", OtherNames: []string{"second", "seconds"}}, {Value: Millisecond, Name: "ms", OtherNames: []string{"millisecond", "milliseconds"}}, {Value: Microsecond, Name: "us", OtherNames: []string{ "µs", "μs", "microsecond", "microseconds", }}, {Value: Nanosecond, Name: "ns", OtherNames: []string{"nanosecond", "nanoseconds"}}, }
var ZH = []Unit{ {Value: Year, Name: "年", OtherNames: []string{}}, {Value: Month, Name: "个月", OtherNames: []string{"月"}}, {Value: Week, Name: "", OtherNames: []string{"周", "星期", "个星期"}}, {Value: Day, Name: "天", OtherNames: []string{"日"}}, {Value: Hour, Name: "小时", OtherNames: []string{"时"}}, {Value: Minute, Name: "分", OtherNames: []string{"分钟"}}, {Value: Second, Name: "秒", OtherNames: []string{"秒钟"}}, {Value: Millisecond, Name: "毫秒", OtherNames: []string{}}, {Value: Microsecond, Name: "微秒", OtherNames: []string{}}, {Value: Nanosecond, Name: "纳秒", OtherNames: []string{}}, }
Functions ¶
func RegisterUnitSet ¶
RegisterUnitSet register a set of units which will be used by Parse() and Duration.String().
Types ¶
type Date ¶
Example (Compare) ¶
day1 := NewDate(2021, 10, 1) day2 := NewDate(2021, 10, 2) day3 := NewDate(2021, 10, 2) fmt.Println(day1.After(day2)) fmt.Println(day1.Before(day2)) fmt.Println(day1.Equal(day2)) fmt.Println(day2.After(day3)) fmt.Println(day2.Before(day3)) fmt.Println(day2.Equal(day3))
Output: false true false false false true
func NewDateFromTime ¶
func (Date) MarshalJSON ¶
Example ¶
var day = Date{} b, err := json.Marshal(day) fmt.Println(string(b), err) day = NewDate(2018, 04, 01) b, err = json.Marshal(day) fmt.Println(string(b), err)
Output: null <nil> "2018-04-01" <nil>
func (*Date) UnmarshalJSON ¶
Example ¶
var day = Date{} err := json.Unmarshal([]byte(`"2019-09-12"`), &day) fmt.Println(day, err) day = Date{} err = json.Unmarshal([]byte(`2019-09-12`), &day) fmt.Println(day, err)
Output: 2019-09-12 <nil> invalid character '-' after top-level value
type Duration ¶
type Duration struct { Value int64 // max duration: (1<<63 - 1) / (365 * 86400 * 1000 * 1000 * 1000) = 292 year Units []Unit N int8 // String returns only the n most Signifcant units if n > 0. }
func ParseDuration ¶
Parse like time.ParseDuration, no layout is needed. All registered units are recoganized.
Example (Empty) ¶
fmt.Println(ParseDuration(``)) fmt.Println(ParseDuration(`0`)) fmt.Println(ParseDuration(`+0`)) fmt.Println(ParseDuration(`-0`))
Output: 0 <nil> 0 <nil> 0 <nil> 0 <nil>
func (Duration) Round ¶
Round returns the result of rounding d to the nearest multiple of m. The rounding behavior for halfway values is to round away from zero. If the result exceeds the maximum (or minimum) value that can be stored in a Duration, Round returns the maximum (or minimum) duration. If m <= 0, Round returns d unchanged.
Example ¶
d, _ := ParseDuration(`2m29s`) fmt.Println(d.Round(Minute)) d, _ = ParseDuration(`2m30s`) fmt.Println(d.Round(Minute)) d, _ = ParseDuration(`-2m29s`) fmt.Println(d.Round(Minute)) d, _ = ParseDuration(`-2m30s`) fmt.Println(d.Round(Minute))
Output: 2m 3m -2m -3m
func (Duration) String ¶
Example ¶
d, _ := ParseDuration(`2m3s`) fmt.Println(d) d, _ = ParseDuration(`-1Y2M9D13h5m`) d.N = 2 fmt.Println(d)
Output: 2m3s -1Y2M
func (Duration) Truncate ¶
Truncate returns the result of rounding d toward zero to a multiple of m. If m <= 0, Truncate returns d unchanged.
Example ¶
d, _ := ParseDuration(`2m3s`) fmt.Println(d.Truncate(Minute)) d, _ = ParseDuration(`-1Y2M9D13h5m`) fmt.Println(d.Truncate(Day))
Output: 2m -1Y2M9D
func (*Duration) UnmarshalJSON ¶
Example ¶
var d Duration if err := json.Unmarshal([]byte(`"123秒"`), &d); err != nil { fmt.Println(err) } fmt.Println(d)
Output: 2分3秒
func (*Duration) UnmarshalYAML ¶
Example ¶
var d Duration if err := yaml.Unmarshal([]byte(`123秒`), &d); err != nil { fmt.Println(err) } fmt.Println(d) if err := yaml.Unmarshal([]byte(`"123秒"`), &d); err != nil { fmt.Println(err) } fmt.Println(d)
Output: 2分3秒 2分3秒
type HMS ¶
HMS reprenets hour, minute, second.
Example ¶
type Stu struct { Name string `json:"name"` FinishedAt HMS `json:"finishedAt"` } d, err := NewHMS("14:05:00") fmt.Println(err) stu := Stu{Name: "A", FinishedAt: *d} b, _ := json.Marshal(stu) fmt.Println(string(b)) stu = Stu{Name: "A"} b, _ = json.Marshal(stu) fmt.Println(string(b)) stu2 := Stu{} data := []byte(`{"name": "W5"}`) json.Unmarshal(data, &stu2) fmt.Println(stu2.FinishedAt.IsZero()) data = []byte(`{"name": "W5", "finishedAt": ""}`) json.Unmarshal(data, &stu2) fmt.Println(stu2.FinishedAt) fmt.Println(NewHMS("24:00:00"))
Output: <nil> {"name":"A","finishedAt":"14:05:00"} {"name":"A","finishedAt":null} true 00:00:00 23:59:59 <nil>
func (HMS) MarshalJSON ¶
func (*HMS) UnmarshalJSON ¶
type Sleep ¶
func (*Sleep) Asleep ¶
Example ¶
package main import ( "fmt" "time" "gitee.com/go-better/dev/type/time2" ) func main() { var s time2.Sleep go func() { time.Sleep(time.Millisecond) fmt.Println(s.Asleep()) time.Sleep(2 * time.Millisecond) fmt.Println(s.Asleep()) }() s.Sleep(2*time.Millisecond, nil) fmt.Println(s.Asleep()) time.Sleep(5 * time.Millisecond) }
Output: true false false
func (*Sleep) Awake ¶
func (s *Sleep) Awake(event interface{})
the same as s.AwakeAt(time.Now())
Example ¶
package main import ( "fmt" "time" "gitee.com/go-better/dev/type/time2" ) func main() { var s time2.Sleep go func() { fmt.Println(s.Sleep(time.Minute, nil)) }() time.Sleep(time.Millisecond) s.Awake("hi, wake up") time.Sleep(5 * time.Millisecond) }
Output: hi, wake up
func (*Sleep) AwakeAt ¶
set awake at time to the specified time, and awake at the specified time if asleep.
func (*Sleep) AwakeAtEalier ¶
if current awake at time is zero time or the specified time is ealier than current awake at time, call s.AwakeAt(at), otherwise do nothing.
func (*Sleep) AwakeAtLater ¶
if the specified time is later than current awake at time, call s.AwakeAt(at), otherwise do nothing.
func (*Sleep) GetAwakeAt ¶
get awake at time.
Example ¶
package main import ( "fmt" "gitee.com/go-better/dev/type/time2" ) func main() { var s time2.Sleep s.Sleep(0, nil) fmt.Println(s.GetAwakeAt().IsZero()) s.ClearAwakeAt() fmt.Println(s.GetAwakeAt().IsZero()) }
Output: false true
func (*Sleep) Run ¶
func (s *Sleep) Run() interface{}
Run method on a single `Sleep` instance should not be called concurrently, otherwise only a random one will be awaken up by `Awake*` methods. Run method on a single `Sleep` instance can be called many times serially. Run returns the event that awaken it.
func (*Sleep) SetAwakeAt ¶
set awake at time.
func (*Sleep) Sleep ¶
Sleep method on a single `Sleep` instance should not be called concurrently, otherwise only a random one will be awaken up by `Awake*` methods. Sleep method on a single `Sleep` instance can be called many times serially. Sleep returns the event that awaken it.
Example ¶
package main import ( "fmt" "time" "gitee.com/go-better/dev/type/time2" ) func main() { var s time2.Sleep go func() { fmt.Println(s.Sleep(0, "wake up")) }() time.Sleep(5 * time.Millisecond) }
Output: wake up
type Time ¶
func (Time) MarshalJSON ¶
Example ¶
var t = Time{} b, err := json.Marshal(t) fmt.Println(string(b), err) t = New(2019, 9, 12, 12, 1, 2) b, err = json.Marshal(t) fmt.Println(string(b), err)
Output: null <nil> "2019-09-12 12:01:02" <nil>
func (*Time) UnmarshalJSON ¶
Example ¶
var t = Time{} err := json.Unmarshal([]byte(`"2019-09-12 12:01:02"`), &t) fmt.Println(t, err) t = Time{} err = json.Unmarshal([]byte(`2019-09-12 12:01:02`), &t) fmt.Println(t, err)
Output: 2019-09-12 12:01:02 <nil> invalid character '-' after top-level value