Documentation ¶
Index ¶
- func After[T TimeZone](d time.Duration) <-chan Time[T]
- func NowWithContext[T TimeZone](ctx context.Context, t Time[T]) context.Context
- type Day
- type Hour
- type Minute
- type Month
- type Nanosecond
- type NullTime
- type Period
- func (p Period[T]) Contains(t Time[T]) int
- func (p Period[T]) From() Time[T]
- func (p Period[T]) Periodic(next func(Time[T]) Time[T]) periodical[T]
- func (p Period[T]) PeriodicAdvance(u1 Unit, u2 ...Unit) periodical[T]
- func (p Period[T]) PeriodicDate(years int, months int, days int) periodical[T]
- func (p Period[T]) PeriodicDuration(d time.Duration) periodical[T]
- func (p Period[T]) PeriodicISODuration(duration string) (periodical[T], error)
- func (p Period[T]) String() string
- func (p Period[T]) To() Time[T]
- type Quarter
- type Second
- type Semester
- type Time
- func ConvertTz[U TimeZone, T TimeZone](from Time[T]) Time[U]
- func In[T TimeZone](tm time.Time) Time[T]
- func New[T TimeZone](year int, month time.Month, day int, hour int, min int, sec int, nsec int) Time[T]
- func Now[T TimeZone]() Time[T]
- func NowContext[T TimeZone](ctx context.Context) Time[T]
- func Parse[T TimeZone](layout, value string) (Time[T], error)
- func ParseISO[T TimeZone](value string) (Time[T], error)
- func Strptime[T TimeZone](source string, format string) (Time[T], error)
- func Unix[T TimeZone](sec int64, nsec int64) Time[T]
- func UnixMicro[T TimeZone](usec int64) Time[T]
- func UnixMilli[T TimeZone](msec int64) Time[T]
- func (t Time[T]) Add(d time.Duration) Time[T]
- func (t Time[T]) AddDate(years int, months int, days int) Time[T]
- func (t Time[T]) Advance(u1 Unit, u2 ...Unit) Time[T]
- func (t Time[T]) After(u Time[T]) bool
- func (t Time[T]) AppendFormat(b []byte, layout string) []byte
- func (t Time[T]) Before(u Time[T]) bool
- func (t Time[T]) Change(u1 Unit, u2 ...Unit) Time[T]
- func (t Time[T]) Clock() (hour, min, sec int)
- func (t Time[T]) Compare(u Time[T]) int
- func (t Time[T]) Date() (year int, month time.Month, day int)
- func (t Time[T]) Day() int
- func (t Time[T]) DiffInCalendarDays(u Time[T]) int
- func (t Time[T]) EndOfMonth() Time[T]
- func (t Time[T]) EndOfQuarter() Time[T]
- func (t Time[T]) EndOfSemester() Time[T]
- func (t Time[T]) EndOfWeek() Time[T]
- func (t Time[T]) EndOfYear() Time[T]
- func (t Time[T]) Equal(u Time[T]) bool
- func (t Time[T]) Format(layout string) string
- func (t Time[T]) GoString() string
- func (t *Time[T]) GobDecode(data []byte) error
- func (t Time[T]) GobEncode() ([]byte, error)
- func (t Time[T]) Hour() int
- func (t Time[T]) ISOWeek() (year, week int)
- func (t Time[T]) IsBetween(from Time[T], to Time[T]) bool
- func (t Time[T]) IsDST() bool
- func (t Time[T]) IsLeapYear() bool
- func (t Time[T]) IsZero() bool
- func (t Time[T]) Local() Time[tz.Local]
- func (t Time[T]) Location() *time.Location
- func (t Time[T]) MarshalBinary() ([]byte, error)
- func (t Time[T]) MarshalJSON() ([]byte, error)
- func (t Time[T]) MarshalText() ([]byte, error)
- func (t Time[T]) Minute() int
- func (t Time[T]) Month() time.Month
- func (t Time[T]) Nanosecond() int
- func (t Time[T]) Quarter() Quarter[T]
- func (t Time[T]) Round(d time.Duration) Time[T]
- func (t *Time[T]) Scan(src any) error
- func (t Time[T]) Second() int
- func (t Time[T]) Semester() Semester[T]
- func (t Time[T]) StartOfMonth() Time[T]
- func (t Time[T]) StartOfQuarter() Time[T]
- func (t Time[T]) StartOfSemester() Time[T]
- func (t Time[T]) StartOfWeek() Time[T]
- func (t Time[T]) StartOfYear() Time[T]
- func (t Time[T]) StdTime() time.Time
- func (t Time[T]) Strftime(format string) string
- func (t Time[T]) String() string
- func (t Time[T]) Sub(u Time[T]) time.Duration
- func (t Time[T]) Truncate(d time.Duration) Time[T]
- func (t Time[T]) Unix() int64
- func (t Time[T]) UnixMicro() int64
- func (t Time[T]) UnixMilli() int64
- func (t Time[T]) UnixNano() int64
- func (t *Time[T]) UnmarshalBinary(data []byte) error
- func (t *Time[T]) UnmarshalJSON(data []byte) error
- func (t *Time[T]) UnmarshalText(data []byte) error
- func (t Time[T]) Value() (driver.Value, error)
- func (t Time[T]) Weekday() time.Weekday
- func (t Time[T]) Year() int
- func (t Time[T]) YearDay() int
- func (t Time[T]) Zone() (name string, offset int)
- func (t Time[T]) ZoneBounds() (start, end Time[T])
- type TimeZone
- type Unit
- type Year
Examples ¶
- After
- ConvertTz
- In
- New
- NewPeriod
- Now
- NowContext
- Parse
- Period (PeriodicalSlice)
- Period.Contains
- Period.PeriodicAdvance
- Period.PeriodicDate
- Period.PeriodicDuration
- Period.PeriodicISODuration
- Quarter.After
- Quarter.Before
- Quarter.Compare
- Semester.After
- Semester.Before
- Semester.Compare
- Strptime
- Time.Add
- Time.AddDate
- Time.Advance
- Time.After
- Time.AppendFormat
- Time.Before
- Time.Change
- Time.Compare
- Time.Date
- Time.Day
- Time.Equal
- Time.Round
- Time.StdTime
- Time.Strftime
- Time.String
- Time.Sub
- Time.Truncate
- Time.Unix
- Unix
- UnixMicro
- UnixMilli
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func After ¶
After waits for the duration to elapse and then sends the current time on the returned channel. It is equivalent to NewTimer(d).C. The underlying Timer is not recovered by the garbage collector until the timer fires. If efficiency is a concern, use NewTimer instead and call Timer.Stop if the timer is no longer needed.
This is a simple wrapper function for time.After.
Example ¶
package main import ( "fmt" "time" "github.com/Code-Hex/synchro" "github.com/Code-Hex/synchro/tz" ) var c chan int func handle(int) {} func main() { select { case m := <-c: handle(m) case <-synchro.After[tz.UTC](time.Millisecond): fmt.Println("timed out") } }
Output: timed out
Types ¶
type Minute ¶ added in v0.0.9
type Minute int
Minute sets the minute component of the time being built.
type Nanosecond ¶ added in v0.0.9
type Nanosecond int
Nanosecond sets the nanosecond component of the time being built.
type NullTime ¶ added in v0.0.4
NullTime represents a Time[T] that may be null. NullTime implements the sql.Scanner interface so it can be used as a scan destination, similar to sql.NullString.
type Period ¶ added in v0.2.0
type Period[T TimeZone] struct { // contains filtered or unexported fields }
Period allows iteration over a set of dates and times, recurring at regular intervals, over a given period.
Example (PeriodicalSlice) ¶
package main import ( "fmt" "time" "github.com/Code-Hex/synchro" "github.com/Code-Hex/synchro/tz" ) func main() { p1, _ := synchro.NewPeriod[tz.UTC]( "2009-01-01T00:00:00", "2009-01-01T03:00:00", ) got := p1.PeriodicDuration(time.Hour).Slice() fmt.Println("len:", len(got)) for _, current := range got { fmt.Println(current) } }
Output: len: 4 2009-01-01 00:00:00 +0000 UTC 2009-01-01 01:00:00 +0000 UTC 2009-01-01 02:00:00 +0000 UTC 2009-01-01 03:00:00 +0000 UTC
func NewPeriod ¶ added in v0.2.0
NewPeriod creates a new Period struct between the 'from' and 'to' values you specified.
If Time[T] or time.Time is specified, it guarantees no error returns. When a string or []byte is passed, ParseISO function is called internally. Therefore, these parameters should be in a format compatible with ParseISO.
Example ¶
package main import ( "fmt" "time" "github.com/Code-Hex/synchro" "github.com/Code-Hex/synchro/tz" ) func main() { // with synchro.Time params p1, _ := synchro.NewPeriod[tz.UTC]( synchro.New[tz.UTC](2009, 1, 1, 0, 0, 0, 0), synchro.New[tz.UTC](2009, 1, 10, 23, 59, 59, 0), ) // with time.Time params p2, _ := synchro.NewPeriod[tz.UTC]( time.Date(2009, 1, 1, 0, 0, 0, 0, time.UTC), time.Date(2009, 1, 10, 23, 59, 59, 0, time.UTC), ) // with ISO 8601 date and time format string or bytes p3, _ := synchro.NewPeriod[tz.UTC]( "2009-01-01", []byte("2009-01-10T23:59:59Z"), ) // Reverse p4, _ := synchro.NewPeriod[tz.UTC]( "2009-01-10T23:59:59", "2009-01-01", ) fmt.Printf("p1: %s\n", p1) fmt.Printf("p2: %s\n", p2) fmt.Printf("p3: %s\n", p3) fmt.Printf("p4: %s\n", p4) }
Output: p1: from 2009-01-01 00:00:00 +0000 UTC to 2009-01-10 23:59:59 +0000 UTC p2: from 2009-01-01 00:00:00 +0000 UTC to 2009-01-10 23:59:59 +0000 UTC p3: from 2009-01-01 00:00:00 +0000 UTC to 2009-01-10 23:59:59 +0000 UTC p4: from 2009-01-10 23:59:59 +0000 UTC to 2009-01-01 00:00:00 +0000 UTC
func (Period[T]) Contains ¶ added in v0.3.0
Contains checks whether the specified t is included within from and to.
if p.from < t && t < p.to, it returns +1; if p.from == t || t == p.to, it returns 0. Otherwise returns -1;
Example ¶
package main import ( "fmt" "github.com/Code-Hex/synchro" "github.com/Code-Hex/synchro/tz" ) func main() { p, _ := synchro.NewPeriod[tz.UTC]( "2008-12-28", "2009-01-03", ) t := synchro.New[tz.UTC](2009, 1, 3, 0, 0, 0, 0) // p.Contains(t) >= 0 is the same as 2008-12-28 <= t && t <= 2009-01-03 fmt.Println("p.Contains(t) >= 0:", p.Contains(t) >= 0) }
Output: p.Contains(t) >= 0: true
func (Period[T]) Periodic ¶ added in v0.2.0
Periodic returns a channel that emits Time[T] values at regular intervals between the start and end times of the Period[T]. The interval is specified by the next function argument.
If start < end, the process will increase from start to end. In other words, when the current value exceeds end, the iteration is terminated.
If start > end, the process will decrease from start to end. In other words, when the current value falls below end, the iteration is terminated.
func (Period[T]) PeriodicAdvance ¶ added in v0.2.0
PeriodicAdvance is a wrapper for the Periodic function. The interval is specified by the provided date and time unit arguments.
Example ¶
package main import ( "fmt" "time" "github.com/Code-Hex/synchro" "github.com/Code-Hex/synchro/tz" ) func main() { p1, _ := synchro.NewPeriod[tz.UTC]( "2009-01-01", time.Date(2013, 1, 3, 0, 0, 0, 0, time.UTC), ) for current := range p1.PeriodicAdvance(synchro.Year(1), synchro.Day(1)) { fmt.Println(current) } }
Output: 2009-01-01 00:00:00 +0000 UTC 2010-01-02 00:00:00 +0000 UTC 2011-01-03 00:00:00 +0000 UTC 2012-01-04 00:00:00 +0000 UTC
func (Period[T]) PeriodicDate ¶ added in v0.2.0
PeriodicDuration is a wrapper for the Periodic function. The interval is specified by the given number of years, months, and days.
Example ¶
package main import ( "fmt" "github.com/Code-Hex/synchro" "github.com/Code-Hex/synchro/tz" ) func main() { p1, _ := synchro.NewPeriod[tz.UTC]( "2009-01-01", "2009-01-03", ) for current := range p1.PeriodicDate(0, 0, 1) { fmt.Println(current) } }
Output: 2009-01-01 00:00:00 +0000 UTC 2009-01-02 00:00:00 +0000 UTC 2009-01-03 00:00:00 +0000 UTC
func (Period[T]) PeriodicDuration ¶ added in v0.2.0
PeriodicDuration is a wrapper for the Periodic function. The interval is specified by the time.Duration argument.
Example ¶
package main import ( "fmt" "time" "github.com/Code-Hex/synchro" "github.com/Code-Hex/synchro/tz" ) func main() { p1, _ := synchro.NewPeriod[tz.UTC]( "2009-01-01", synchro.New[tz.UTC](2009, 1, 1, 3, 59, 59, 0), ) for current := range p1.PeriodicDuration(time.Hour) { fmt.Println(current) } }
Output: 2009-01-01 00:00:00 +0000 UTC 2009-01-01 01:00:00 +0000 UTC 2009-01-01 02:00:00 +0000 UTC 2009-01-01 03:00:00 +0000 UTC
func (Period[T]) PeriodicISODuration ¶ added in v0.2.0
PeriodicISODuration is a wrapper for the Periodic function. It accepts a duration in ISO 8601 format as a parameter.
Examples of valid durations include:
PnYnMnDTnHnMnS (e.g., P3Y6M4DT12H30M5S) PnW (e.g., P4W)
Example ¶
package main import ( "fmt" "github.com/Code-Hex/synchro" "github.com/Code-Hex/synchro/tz" ) func main() { p1, _ := synchro.NewPeriod[tz.UTC]( "2009-01-01", "2009-03-01", ) iter, _ := p1.PeriodicISODuration("P1M") for current := range iter { fmt.Println(current) } }
Output: 2009-01-01 00:00:00 +0000 UTC 2009-02-01 00:00:00 +0000 UTC 2009-03-01 00:00:00 +0000 UTC
type Quarter ¶
type Quarter[T TimeZone] struct { // contains filtered or unexported fields }
func (Quarter[T]) After ¶
After reports whether the Quarter instant q is after u.
Example ¶
package main import ( "fmt" "github.com/Code-Hex/synchro" "github.com/Code-Hex/synchro/tz" ) func main() { d1 := synchro.New[tz.UTC](2023, 1, 1, 0, 0, 0, 0) d2 := synchro.New[tz.UTC](2023, 4, 1, 0, 0, 0, 0) d3 := synchro.New[tz.UTC](2024, 1, 1, 0, 0, 0, 0) q1 := d1.Quarter() q2 := d2.Quarter() q3 := d3.Quarter() fmt.Printf("q2.After(q1) = %v\n", q2.After(q1)) fmt.Printf("q3.After(q1) = %v\n", q3.After(q1)) fmt.Printf("q1.After(q2) = %v\n", q1.After(q2)) }
Output: q2.After(q1) = true q3.After(q1) = true q1.After(q2) = false
func (Quarter[T]) Before ¶
Before reports whether the Quarter instant q is before u.
Example ¶
package main import ( "fmt" "github.com/Code-Hex/synchro" "github.com/Code-Hex/synchro/tz" ) func main() { d1 := synchro.New[tz.UTC](2023, 1, 1, 0, 0, 0, 0) d2 := synchro.New[tz.UTC](2023, 4, 1, 0, 0, 0, 0) d3 := synchro.New[tz.UTC](2024, 1, 1, 0, 0, 0, 0) q1 := d1.Quarter() q2 := d2.Quarter() q3 := d3.Quarter() fmt.Printf("q1.Before(q2) = %v\n", q1.Before(q2)) fmt.Printf("q1.Before(q3) = %v\n", q1.Before(q3)) fmt.Printf("q2.Before(q1) = %v\n", q2.Before(q1)) }
Output: q1.Before(q2) = true q1.Before(q3) = true q2.Before(q1) = false
func (Quarter[T]) Compare ¶
Compare compares the Quarter instant q with u. If q is before u, it returns -1; if q is after u, it returns +1; if they're the same, it returns 0.
Example ¶
package main import ( "fmt" "github.com/Code-Hex/synchro" "github.com/Code-Hex/synchro/tz" ) func main() { d1 := synchro.New[tz.UTC](2023, 1, 1, 0, 0, 0, 0) d2 := synchro.New[tz.UTC](2023, 3, 31, 23, 59, 59, 999999999) d3 := synchro.New[tz.UTC](2023, 7, 1, 0, 0, 0, 0) d4 := synchro.New[tz.UTC](2024, 1, 1, 0, 0, 0, 0) q1 := d1.Quarter() q2 := d2.Quarter() q3 := d3.Quarter() q4 := d4.Quarter() fmt.Printf("q1.Compare(q2) = %d\n", q1.Compare(q2)) fmt.Printf("q1.Compare(q3) = %d\n", q1.Compare(q3)) fmt.Printf("q4.Compare(q1) = %d\n", q4.Compare(q1)) }
Output: q1.Compare(q2) = 0 q1.Compare(q3) = -1 q4.Compare(q1) = 1
type Second ¶ added in v0.0.9
type Second int
Second sets the second component of the time being built.
type Semester ¶
type Semester[T TimeZone] struct { // contains filtered or unexported fields }
func (Semester[T]) After ¶
After reports whether the Semester instant s is after u.
Example ¶
package main import ( "fmt" "github.com/Code-Hex/synchro" "github.com/Code-Hex/synchro/tz" ) func main() { d1 := synchro.New[tz.UTC](2023, 1, 1, 0, 0, 0, 0) d2 := synchro.New[tz.UTC](2023, 7, 1, 0, 0, 0, 0) d3 := synchro.New[tz.UTC](2024, 1, 1, 0, 0, 0, 0) s1 := d1.Semester() s2 := d2.Semester() s3 := d3.Semester() fmt.Printf("s2.After(s1) = %v\n", s2.After(s1)) fmt.Printf("s3.After(s1) = %v\n", s3.After(s1)) fmt.Printf("s1.After(s2) = %v\n", s1.After(s2)) }
Output: s2.After(s1) = true s3.After(s1) = true s1.After(s2) = false
func (Semester[T]) Before ¶
Before reports whether the Semester instant s is before u.
Example ¶
package main import ( "fmt" "github.com/Code-Hex/synchro" "github.com/Code-Hex/synchro/tz" ) func main() { d1 := synchro.New[tz.UTC](2023, 1, 1, 0, 0, 0, 0) d2 := synchro.New[tz.UTC](2023, 7, 1, 0, 0, 0, 0) d3 := synchro.New[tz.UTC](2024, 1, 1, 0, 0, 0, 0) s1 := d1.Semester() s2 := d2.Semester() s3 := d3.Semester() fmt.Printf("s1.Before(s2) = %v\n", s1.Before(s2)) fmt.Printf("s1.Before(s3) = %v\n", s1.Before(s3)) fmt.Printf("s2.Before(s1) = %v\n", s2.Before(s1)) }
Output: s1.Before(s2) = true s1.Before(s3) = true s2.Before(s1) = false
func (Semester[T]) Compare ¶
Compare compares the Semester instant s with u. If s is before u, it returns -1; if s is after u, it returns +1; if they're the same, it returns 0.
Example ¶
package main import ( "fmt" "github.com/Code-Hex/synchro" "github.com/Code-Hex/synchro/tz" ) func main() { d1 := synchro.New[tz.UTC](2023, 1, 1, 0, 0, 0, 0) d2 := synchro.New[tz.UTC](2023, 6, 30, 23, 59, 59, 999999999) d3 := synchro.New[tz.UTC](2023, 7, 1, 0, 0, 0, 0) d4 := synchro.New[tz.UTC](2024, 1, 1, 0, 0, 0, 0) s1 := d1.Semester() s2 := d2.Semester() s3 := d3.Semester() s4 := d4.Semester() fmt.Printf("s1.Compare(s2) = %d\n", s1.Compare(s2)) fmt.Printf("s1.Compare(s3) = %d\n", s1.Compare(s3)) fmt.Printf("s4.Compare(s1) = %d\n", s4.Compare(s1)) }
Output: s1.Compare(s2) = 0 s1.Compare(s3) = -1 s4.Compare(s1) = 1
type Time ¶
type Time[T TimeZone] struct { // contains filtered or unexported fields }
func ConvertTz ¶
ConvertTz can be used to convert a time from one time zone to another.
For example to convert from UTC to Asia/Tokyo. If `2009-11-10 23:00:00 +0000 UTC` as input, the output will be `2009-11-11 08:00:00 +0900 Asia/Tokyo`.
Example ¶
package main import ( "fmt" "time" "github.com/Code-Hex/synchro" "github.com/Code-Hex/synchro/tz" ) func main() { utc := synchro.New[tz.UTC](2009, time.November, 10, 23, 0, 0, 0) fmt.Printf("Go launched at %s\n", utc) jst := synchro.ConvertTz[tz.AsiaTokyo](utc) fmt.Printf("Go launched at %s\n", jst) }
Output: Go launched at 2009-11-10 23:00:00 +0000 UTC Go launched at 2009-11-11 08:00:00 +0900 JST
func In ¶
In returns timezone-aware time.
If the given time.Time is the zero value, a zero value Time[T] is returned. A zero value Time[T] is not timezone-aware. Always set UTC.
Example ¶
package main import ( "fmt" "time" "github.com/Code-Hex/synchro" "github.com/Code-Hex/synchro/tz" ) func main() { d := time.Date(2023, 9, 2, 14, 0, 0, 0, time.UTC) utc := synchro.In[tz.UTC](d) fmt.Println(utc) jst := synchro.In[tz.AsiaTokyo](d) fmt.Println(jst) }
Output: 2023-09-02 14:00:00 +0000 UTC 2023-09-02 23:00:00 +0900 JST
func New ¶
func New[T TimeZone](year int, month time.Month, day int, hour int, min int, sec int, nsec int) Time[T]
New returns the Time corresponding to
yyyy-mm-dd hh:mm:ss + nsec nanoseconds
in the appropriate zone for that time in the given timezone.
The month, day, hour, min, sec, and nsec values may be outside their usual ranges and will be normalized during the conversion. For example, October 32 converts to November 1.
A daylight savings time transition skips or repeats times. For example, in the United States, March 13, 2011 2:15am never occurred, while November 6, 2011 1:15am occurred twice. In such cases, the choice of time zone, and therefore the time, is not well-defined. Date returns a time that is correct in one of the two zones involved in the transition, but it does not guarantee which.
This is a simple wrapper function for time.Date.
Example ¶
package main import ( "fmt" "time" "github.com/Code-Hex/synchro" "github.com/Code-Hex/synchro/tz" ) func main() { utc := synchro.New[tz.UTC](2009, time.November, 10, 23, 0, 0, 0) fmt.Printf("Go launched at %s\n", utc) }
Output: Go launched at 2009-11-10 23:00:00 +0000 UTC
func Now ¶
Now returns the current time with timezone.
Example ¶
package main import ( "fmt" "github.com/Code-Hex/synchro" "github.com/Code-Hex/synchro/tz" ) func main() { // The current UTC time is fixed to `2023-09-02 14:00:00`. utcNow := synchro.Now[tz.UTC]() fmt.Println(utcNow) jstNow := synchro.Now[tz.AsiaTokyo]() fmt.Println(jstNow) }
Output: 2023-09-02 14:00:00 +0000 UTC 2023-09-02 23:00:00 +0900 JST
func NowContext ¶
NowContext returns the current time stored in the provided context. If the time is not found in the context, it returns zero value.
NowContext and NowWithContext are useful when you want to store the current time within the context of the executing logic. For example, in scenarios where you consider the entire lifecycle of an HTTP request, from its initiation to the response.
By capturing the timestamp when the request occurs and fixing it as the current time, you can achieve consistent handling of the current time throughout that request. This ensures uniformity in dealing with the current time within the scope of that specific request.
Example ¶
package main import ( "context" "fmt" "github.com/Code-Hex/synchro" "github.com/Code-Hex/synchro/tz" ) func main() { // This is the timestamp when the request occurred. // The current UTC time is fixed to `2023-09-02 14:00:00`. timestamp := synchro.Now[tz.UTC]() // The context of the request. ctx := context.Background() // Set the current time within the request context.Context. ctx = synchro.NowWithContext[tz.UTC](ctx, timestamp) utcNow := synchro.NowContext[tz.UTC](ctx) fmt.Println(utcNow) // A zero value is returned because the time is not stored in the same timezone. jstNow := synchro.NowContext[tz.AsiaTokyo](ctx) fmt.Println(jstNow) }
Output: 2023-09-02 14:00:00 +0000 UTC 0001-01-01 00:00:00 +0000 UTC
func Parse ¶
Parse parses a formatted string and returns the time value it represents. See the documentation for the constant called Layout to see how to represent the format. The second argument must be parseable using the format string (layout) provided as the first argument.
This is a simple wrapper function for time.ParseInLocation.
Example ¶
package main import ( "fmt" "time" "github.com/Code-Hex/synchro" "github.com/Code-Hex/synchro/tz" ) func init() { synchro.SetNow(func() time.Time { return time.Date(2023, 9, 2, 14, 0, 0, 0, time.UTC) }) } type EuropeBerlin struct{} func (EuropeBerlin) Location() *time.Location { loc, _ := time.LoadLocation("Europe/Berlin") return loc } func main() { const longForm = "Jan 2, 2006 at 3:04pm (MST)" t, _ := synchro.Parse[EuropeBerlin](longForm, "Jul 9, 2012 at 5:02am (CEST)") fmt.Println(t) // Note: without explicit zone, returns time in given location. const shortForm = "2006-Jan-02" t, _ = synchro.Parse[EuropeBerlin](shortForm, "2012-Jul-09") fmt.Println(t) _, err := synchro.Parse[tz.UTC](time.RFC3339, time.RFC3339) fmt.Println("error", err) // Returns an error as the layout is not a valid time value }
Output: 2012-07-09 05:02:00 +0200 CEST 2012-07-09 00:00:00 +0200 CEST error parsing time "2006-01-02T15:04:05Z07:00": extra text: "07:00"
func ParseISO ¶ added in v0.0.5
ParseISO parses an ISO8601-compliant date or datetime string and returns its representation as a Time. If the input string does not conform to the ISO8601 standard or if any other parsing error occurs, an error is returned. Supported formats include:
Basic Extended 20070301 2007-03-01 2012W521 2012-W52-1 2012Q485 2012-Q4-85 20070301T1300Z 2007-03-01T13:00Z 20070301T1300Z 2007-03-01T13:00Z 20070301T1300+0100 2007-03-01T13:00+01:00 20070301T1300-0600 2007-03-01T13:00-06:00 20070301T130045Z 2007-03-01T13:00:45Z 20070301T130045+0100 2007-03-01T13:00:45+01:00 ... and other combinations
func Strptime ¶ added in v0.5.0
Strptime parses time string with the default location. The location is also used to parse the time zone name (%Z).
The format string should follow the format of strptime(3) in man pages. The following list shows the supported format specifiers:
- %a: abbreviated weekday name
- %A: full weekday name
- %b: abbreviated month name
- %B: full month name
- %c: preferred date and time representation
- %C: century number (00-99)
- %d: day of the month (01-31)
- %D: same as %m/%d/%y
- %e: day of the month (1-31)
- %F: same as %Y-%m-%d
- %g: last two digits of the year (00-99)
- %G: year as a 4-digit number
- %h: same as %b
- %H: hour (00-23)
- %I: hour (01-12)
- %j: day of the year (001-366)
- %m: month (01-12)
- %M: minute (00-59)
- %n: newline character
- %p: either "am" or "pm" according to the given time value
- %r: time in a.m. and p.m. notation
- %R: time in 24 hour notation
- %S: second (00-60)
- %t: tab character
- %T: current time, equal to %H:%M:%S
- %u: weekday as a number (1-7)
- %U: week number of the current year, starting with the first Sunday as the first day of the first week
- %V: week number of the current year, starting with the first week that has at least 4 days in the new year
- %w: day of the week as a decimal, Sunday being 0
- %W: week number of the current year, starting with the first Monday as the first day of the first week
- %x: preferred date representation without the time
- %X: preferred time representation without the date
- %y: year without a century (00-99)
- %Y: year with century
- %z: time zone offset, such as "-0700"
- %Z: time zone name, such as "UTC" or "GMT"
This is a wrapper for the github.com/itchyny/timefmt-go library.
Example ¶
package main import ( "fmt" "github.com/Code-Hex/synchro" "github.com/Code-Hex/synchro/tz" ) func main() { t, _ := synchro.Strptime[tz.AsiaTokyo]("2023-09-02 14:09:56", "%Y-%m-%d %H:%M:%S") fmt.Println(t) _, err := synchro.Strptime[tz.UTC]("invalid", "%Y") fmt.Println("error", err) // Returns an error as the layout is not a valid time value }
Output: 2023-09-02 14:09:56 +0900 JST error failed to parse "invalid" with "%Y": cannot parse %Y
func Unix ¶
Unix returns the local Time corresponding to the given Unix time, sec seconds and nsec nanoseconds since January 1, 1970 UTC. It is valid to pass nsec outside the range [0, 999999999]. Not all sec values have a corresponding time value. One such value is 1<<63-1 (the largest int64 value).
This is a simple wrapper function for time.Unix.
Example ¶
package main import ( "fmt" "time" "github.com/Code-Hex/synchro" "github.com/Code-Hex/synchro/tz" ) func main() { unixTime := synchro.New[tz.UTC](2009, time.November, 10, 23, 0, 0, 0) fmt.Println(unixTime.Unix()) t := synchro.Unix[tz.UTC](unixTime.Unix(), 0) fmt.Println(t) }
Output: 1257894000 2009-11-10 23:00:00 +0000 UTC
func UnixMicro ¶
UnixMicro returns the local Time corresponding to the given Unix time, usec microseconds since January 1, 1970 UTC.
This is a simple wrapper function for time.UnixMicro.
Example ¶
package main import ( "fmt" "time" "github.com/Code-Hex/synchro" "github.com/Code-Hex/synchro/tz" ) func main() { umt := synchro.New[tz.UTC](2009, time.November, 10, 23, 0, 0, 0) fmt.Println(umt.UnixMicro()) t := synchro.UnixMicro[tz.UTC](umt.UnixMicro()) fmt.Println(t) }
Output: 1257894000000000 2009-11-10 23:00:00 +0000 UTC
func UnixMilli ¶
UnixMilli returns the local Time corresponding to the given Unix time, msec milliseconds since January 1, 1970 UTC.
This is a simple wrapper function for time.UnixMilli.
Example ¶
package main import ( "fmt" "time" "github.com/Code-Hex/synchro" "github.com/Code-Hex/synchro/tz" ) func main() { umt := synchro.New[tz.UTC](2009, time.November, 10, 23, 0, 0, 0) fmt.Println(umt.UnixMilli()) t := synchro.UnixMilli[tz.UTC](umt.UnixMilli()) fmt.Println(t) }
Output: 1257894000000 2009-11-10 23:00:00 +0000 UTC
func (Time[T]) Add ¶
Add returns the time t+d.
This is a simple wrapper method for (time.Time{}).Add.
Example ¶
package main import ( "fmt" "time" "github.com/Code-Hex/synchro" "github.com/Code-Hex/synchro/tz" ) func main() { start := synchro.New[tz.UTC](2009, 1, 1, 12, 0, 0, 0) afterTenSeconds := start.Add(time.Second * 10) afterTenMinutes := start.Add(time.Minute * 10) afterTenHours := start.Add(time.Hour * 10) afterTenDays := start.Add(time.Hour * 24 * 10) fmt.Printf("start = %v\n", start) fmt.Printf("start.Add(time.Second * 10) = %v\n", afterTenSeconds) fmt.Printf("start.Add(time.Minute * 10) = %v\n", afterTenMinutes) fmt.Printf("start.Add(time.Hour * 10) = %v\n", afterTenHours) fmt.Printf("start.Add(time.Hour * 24 * 10) = %v\n", afterTenDays) }
Output: start = 2009-01-01 12:00:00 +0000 UTC start.Add(time.Second * 10) = 2009-01-01 12:00:10 +0000 UTC start.Add(time.Minute * 10) = 2009-01-01 12:10:00 +0000 UTC start.Add(time.Hour * 10) = 2009-01-01 22:00:00 +0000 UTC start.Add(time.Hour * 24 * 10) = 2009-01-11 12:00:00 +0000 UTC
func (Time[T]) AddDate ¶
AddDate returns the time corresponding to adding the given number of years, months, and days to t.
This is a simple wrapper method for (time.Time{}).AddDate.
Example ¶
package main import ( "fmt" "github.com/Code-Hex/synchro" "github.com/Code-Hex/synchro/tz" ) func main() { start := synchro.New[tz.UTC](2009, 1, 1, 0, 0, 0, 0) oneDayLater := start.AddDate(0, 0, 1) oneMonthLater := start.AddDate(0, 1, 0) oneYearLater := start.AddDate(1, 0, 0) fmt.Printf("oneDayLater: start.AddDate(0, 0, 1) = %v\n", oneDayLater) fmt.Printf("oneMonthLater: start.AddDate(0, 1, 0) = %v\n", oneMonthLater) fmt.Printf("oneYearLater: start.AddDate(1, 0, 0) = %v\n", oneYearLater) }
Output: oneDayLater: start.AddDate(0, 0, 1) = 2009-01-02 00:00:00 +0000 UTC oneMonthLater: start.AddDate(0, 1, 0) = 2009-02-01 00:00:00 +0000 UTC oneYearLater: start.AddDate(1, 0, 0) = 2010-01-01 00:00:00 +0000 UTC
func (Time[T]) Advance ¶ added in v0.0.8
Advance adjusts the time based on the provided unit values, moving it forward in time. u1 is a required unit, while u2... can be provided as additional optional units. This method returns a new Time[T] and does not modify the original. The time is adjusted in the order the units are provided.
Example ¶
package main import ( "fmt" "time" "github.com/Code-Hex/synchro" "github.com/Code-Hex/synchro/tz" ) // shorthand way type Y = synchro.Year type M = synchro.Month type D = synchro.Day type HH = synchro.Hour type MM = synchro.Minute type SS = synchro.Second type NS = synchro.Nanosecond func main() { utc := synchro.New[tz.UTC](2009, time.November, 10, 23, 0, 0, 0) c1 := utc.Advance(synchro.Year(1)) c11 := utc.Advance(Y(1), Y(1)) // +2 years c2 := utc.Advance(Y(1), M(1)) c3 := utc.Advance(Y(1), M(1), D(1)) c4 := c3.Advance(HH(1)) c5 := c3.Advance(HH(1), MM(1)) c6 := c3.Advance(HH(1), MM(1), SS(1)) c7 := c3.Advance(HH(1), MM(1), SS(1), NS(123456789)) fmt.Printf("Go launched at %s\n", utc) fmt.Println(c1) fmt.Println(c11) fmt.Println() fmt.Println(c2) fmt.Println(c3) fmt.Println(c4) fmt.Println(c5) fmt.Println(c6) fmt.Println(c7) }
Output: Go launched at 2009-11-10 23:00:00 +0000 UTC 2010-11-10 23:00:00 +0000 UTC 2011-11-10 23:00:00 +0000 UTC 2010-12-10 23:00:00 +0000 UTC 2010-12-11 23:00:00 +0000 UTC 2010-12-12 00:00:00 +0000 UTC 2010-12-12 00:01:00 +0000 UTC 2010-12-12 00:01:01 +0000 UTC 2010-12-12 00:01:01.123456789 +0000 UTC
func (Time[T]) After ¶
After reports whether the time instant t is after u.
If you want to compare time.Time as a parameter, please use with the Time method.
Example ¶
package main import ( "fmt" "github.com/Code-Hex/synchro" "github.com/Code-Hex/synchro/tz" ) func main() { year2000 := synchro.New[tz.UTC](2000, 1, 1, 0, 0, 0, 0) year3000 := synchro.New[tz.UTC](3000, 1, 1, 0, 0, 0, 0) isYear3000AfterYear2000 := year3000.After(year2000) // True isYear2000AfterYear3000 := year2000.After(year3000) // False fmt.Printf("year3000.After(year2000) = %v\n", isYear3000AfterYear2000) fmt.Printf("year2000.After(year3000) = %v\n", isYear2000AfterYear3000) }
Output: year3000.After(year2000) = true year2000.After(year3000) = false
func (Time[T]) AppendFormat ¶
AppendFormat is like Format but appends the textual representation to b and returns the extended buffer.
This is a simple wrapper method for (time.Time{}).AppendFormat.
Example ¶
package main import ( "fmt" "time" "github.com/Code-Hex/synchro" "github.com/Code-Hex/synchro/tz" ) func main() { t := synchro.New[tz.UTC](2017, time.November, 4, 11, 0, 0, 0) text := []byte("Time: ") text = t.AppendFormat(text, time.Kitchen) fmt.Println(string(text)) }
Output: Time: 11:00AM
func (Time[T]) Before ¶
Before reports whether the time instant t is before u.
If you want to compare time.Time as a parameter, please use with the Time method.
Example ¶
package main import ( "fmt" "github.com/Code-Hex/synchro" "github.com/Code-Hex/synchro/tz" ) func main() { year2000 := synchro.New[tz.UTC](2000, 1, 1, 0, 0, 0, 0) year3000 := synchro.New[tz.UTC](3000, 1, 1, 0, 0, 0, 0) isYear2000BeforeYear3000 := year2000.Before(year3000) // True isYear3000BeforeYear2000 := year3000.Before(year2000) // False fmt.Printf("year2000.Before(year3000) = %v\n", isYear2000BeforeYear3000) fmt.Printf("year3000.Before(year2000) = %v\n", isYear3000BeforeYear2000) }
Output: year2000.Before(year3000) = true year3000.Before(year2000) = false
func (Time[T]) Change ¶ added in v0.0.8
Change modifies the time based on the provided unit values. u1 is a required unit, while u2... can be provided as additional optional units. This method returns a new Time[T] and does not modify the original.
Example ¶
package main import ( "fmt" "time" "github.com/Code-Hex/synchro" "github.com/Code-Hex/synchro/tz" ) // shorthand way type Y = synchro.Year type M = synchro.Month type D = synchro.Day type HH = synchro.Hour type MM = synchro.Minute type SS = synchro.Second type NS = synchro.Nanosecond func main() { utc := synchro.New[tz.UTC](2009, time.November, 10, 23, 0, 0, 0) c1 := utc.Change(synchro.Year(2010)) c2 := utc.Change(synchro.Year(2010), synchro.Month(time.December)) c3 := utc.Change(Y(2010), M(time.December), D(1)) c4 := c3.Change(synchro.Hour(1)) c5 := c3.Change(HH(1), MM(1)) c6 := c3.Change(HH(1), MM(1), SS(1)) c7 := c3.Change(HH(1), MM(1), SS(1), NS(123456789)) fmt.Printf("Go launched at %s\n", utc) fmt.Println(c1) fmt.Println(c2) fmt.Println(c3) fmt.Println(c4) fmt.Println(c5) fmt.Println(c6) fmt.Println(c7) }
Output: Go launched at 2009-11-10 23:00:00 +0000 UTC 2010-11-10 23:00:00 +0000 UTC 2010-12-10 23:00:00 +0000 UTC 2010-12-01 23:00:00 +0000 UTC 2010-12-01 01:00:00 +0000 UTC 2010-12-01 01:01:00 +0000 UTC 2010-12-01 01:01:01 +0000 UTC 2010-12-01 01:01:01.123456789 +0000 UTC
func (Time[T]) Clock ¶
Clock returns the hour, minute, and second within the day specified by t.
This is a simple wrapper method for (time.Time{}).Clock.
func (Time[T]) Compare ¶
Compare compares the time instant t with u. If t is before u, it returns -1; if t is after u, it returns +1; if they're the same, it returns 0.
If you want to compare time.Time as a parameter, please use with the Time method.
Example ¶
package main import ( "fmt" "github.com/Code-Hex/synchro" "github.com/Code-Hex/synchro/tz" ) func main() { base := synchro.New[tz.UTC](2000, 1, 1, 1, 0, 0, 0) before := synchro.New[tz.UTC](2000, 1, 1, 0, 0, 0, 0) after := synchro.New[tz.UTC](3000, 1, 1, 2, 0, 0, 0) isSame := base.Compare(base) isAfter := base.Compare(after) isBefore := base.Compare(before) fmt.Printf("base.Compare(base) = %d\n", isSame) fmt.Printf("base.Compare(after) = %d\n", isAfter) fmt.Printf("base.Compare(before) = %d\n", isBefore) }
Output: base.Compare(base) = 0 base.Compare(after) = -1 base.Compare(before) = 1
func (Time[T]) Date ¶
Date returns the year, month, and day in which t occurs.
This is a simple wrapper method for (time.Time{}).Date.
Example ¶
package main import ( "fmt" "github.com/Code-Hex/synchro" "github.com/Code-Hex/synchro/tz" ) func main() { d := synchro.New[tz.UTC](2000, 2, 1, 12, 30, 0, 0) year, month, day := d.Date() fmt.Printf("year = %v\n", year) fmt.Printf("month = %v\n", month) fmt.Printf("day = %v\n", day) }
Output: year = 2000 month = February day = 1
func (Time[T]) Day ¶
Day returns the day of the month specified by t.
This is a simple wrapper method for (time.Time{}).Day.
Example ¶
package main import ( "fmt" "github.com/Code-Hex/synchro" "github.com/Code-Hex/synchro/tz" ) func main() { d := synchro.New[tz.UTC](2000, 2, 1, 12, 30, 0, 0) day := d.Day() fmt.Printf("day = %v\n", day) }
Output: day = 1
func (Time[T]) DiffInCalendarDays ¶ added in v0.0.5
DiffInCalendarDays calculates the difference in calendar days between t and u. (t-u) Calendar days are calculated by considering only the dates, excluding the times, and then determining the difference in days.
func (Time[T]) EndOfMonth ¶
EndOfMonth returns Time for end of the month.
func (Time[T]) EndOfQuarter ¶
EndOfQuarter returns a Time for end of the quarter.
func (Time[T]) EndOfSemester ¶
EndOfSemester returns a Time for end of the semester.
func (Time[T]) Equal ¶
Equal reports whether t and u represent the same time instant.
If you want to compare time.Time as a parameter, please use with the Time method.
Example ¶
package main import ( "fmt" "github.com/Code-Hex/synchro" "github.com/Code-Hex/synchro/tz" ) func main() { d1 := synchro.New[tz.UTC](2000, 2, 1, 12, 30, 0, 0) d2, _ := synchro.Parse[tz.UTC]("2006 Jan 02 15:04:05 (MST)", "2000 Feb 01 12:30:00 (JST)") datesEqualUsingEqualOperator := d1 == d2 datesEqualUsingFunction := d1.Equal(d2) fmt.Printf("d1 = %q\n", d1) fmt.Printf("d2 = %q\n", d2) fmt.Printf("datesEqualUsingEqualOperator = %v\n", datesEqualUsingEqualOperator) fmt.Printf("datesEqualUsingFunction = %v\n", datesEqualUsingFunction) }
Output: d1 = "2000-02-01 12:30:00 +0000 UTC" d2 = "2000-02-01 12:30:00 +0000 UTC" datesEqualUsingEqualOperator = true datesEqualUsingFunction = true
func (Time[T]) Format ¶
Format returns a textual representation of the time value formatted according to the layout defined by the argument.
This is a simple wrapper method for (time.Time{}).Format.
func (Time[T]) GoString ¶
GoString implements fmt.GoStringer and formats t to be printed in Go source code.
This is a simple wrapper method for (time.Time{}).GoString.
func (*Time[T]) GobDecode ¶
GobDecode implements the gob.GobDecoder interface.
This is a simple wrapper method for (time.Time{}).GobDecode.
func (Time[T]) GobEncode ¶
GobEncode implements the gob.GobEncoder interface.
This is a simple wrapper method for (time.Time{}).GobEncode.
func (Time[T]) Hour ¶
Hour returns the hour within the day specified by t, in the range [0, 23].
This is a simple wrapper method for (time.Time{}).Hour.
func (Time[T]) ISOWeek ¶
ISOWeek returns the ISO 8601 year and week number in which t occurs.
This is a simple wrapper method for (time.Time{}).ISOWeek.
func (Time[T]) IsDST ¶
IsDST reports whether the time in the configured location is in Daylight Savings Time.
This is a simple wrapper method for (time.Time{}).IsDST.
func (Time[T]) IsLeapYear ¶
IsLeapYear returns true if t is leap year.
func (Time[T]) IsZero ¶
IsZero reports whether t represents the zero time instant, January 1, year 1, 00:00:00 UTC.
This is a simple wrapper method for (time.Time{}).IsZero.
func (Time[T]) Location ¶
Location returns the time zone information associated with t.
This is a simple wrapper method for (time.Time{}).Location.
func (Time[T]) MarshalBinary ¶
MarshalBinary implements the encoding.BinaryMarshaler interface.
This is a simple wrapper method for (time.Time{}).MarshalBinary.
func (Time[T]) MarshalJSON ¶
MarshalJSON implements the json.Marshaler interface. The time is a quoted string in the RFC 3339 format with sub-second precision. If the timestamp cannot be represented as valid RFC 3339 (e.g., the year is out of range), then an error is reported.
This is a simple wrapper method for (time.Time{}).MarshalJSON.
func (Time[T]) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface. The time is formatted in RFC 3339 format with sub-second precision. If the timestamp cannot be represented as valid RFC 3339 (e.g., the year is out of range), then an error is reported.
This is a simple wrapper method for (time.Time{}).MarshalText.
func (Time[T]) Minute ¶
Minute returns the minute offset within the hour specified by t, in the range [0, 59].
This is a simple wrapper method for (time.Time{}).Minute.
func (Time[T]) Month ¶
Month returns the month of the year specified by t.
This is a simple wrapper method for (time.Time{}).Month.
func (Time[T]) Nanosecond ¶
Nanosecond returns the nanosecond offset within the second specified by t, in the range [0, 999999999].
This is a simple wrapper method for (time.Time{}).Nanosecond.
func (Time[T]) Round ¶
Round returns the result of rounding t to the nearest multiple of d (since the zero time). The rounding behavior for halfway values is to round up. If d <= 0, Round returns t stripped of any monotonic clock reading but otherwise unchanged.
This is a simple wrapper method for (time.Time{}).Round.
Example ¶
package main import ( "fmt" "time" "github.com/Code-Hex/synchro" "github.com/Code-Hex/synchro/tz" ) func main() { t := synchro.New[tz.UTC](0, 0, 0, 12, 15, 30, 918273645) round := []time.Duration{ time.Nanosecond, time.Microsecond, time.Millisecond, time.Second, 2 * time.Second, time.Minute, 10 * time.Minute, time.Hour, } for _, d := range round { fmt.Printf("t.Round(%6s) = %s\n", d, t.Round(d).Format("15:04:05.999999999")) } }
Output: t.Round( 1ns) = 12:15:30.918273645 t.Round( 1µs) = 12:15:30.918274 t.Round( 1ms) = 12:15:30.918 t.Round( 1s) = 12:15:31 t.Round( 2s) = 12:15:30 t.Round( 1m0s) = 12:16:00 t.Round( 10m0s) = 12:20:00 t.Round(1h0m0s) = 12:00:00
func (Time[T]) Second ¶
Second returns the second offset within the minute specified by t, in the range [0, 59].
This is a simple wrapper method for (time.Time{}).Second.
func (Time[T]) StartOfMonth ¶
StartOfMonth returns Time for start of the month.
func (Time[T]) StartOfQuarter ¶
StartOfQuarter returns a Time for start of the quarter.
func (Time[T]) StartOfSemester ¶
StartOfSemester returns a Time for start of the semester.
func (Time[T]) StartOfWeek ¶
StartOfWeek returns Time for start of the week.
func (Time[T]) StartOfYear ¶
StartOfYear returns Time for start of the year.
func (Time[T]) StdTime ¶
StdTime returns the time.Time.
Example ¶
package main import ( "fmt" "time" "github.com/Code-Hex/synchro" "github.com/Code-Hex/synchro/tz" ) func main() { want := time.Date(2023, 9, 2, 14, 0, 0, 0, time.UTC) got1 := synchro.In[tz.UTC](want).StdTime() got2 := synchro.In[tz.AsiaTokyo](want).StdTime() fmt.Println(want.Equal(got1)) fmt.Println(want.Equal(got2)) }
Output: true true
func (Time[T]) Strftime ¶ added in v0.5.0
Strftime formats the time according to the given format string.
This method is a wrapper for the github.com/itchyny/timefmt-go library.
Example:
- %Y-%m-%d %H:%M:%S => 2023-09-02 14:09:56
- %a, %d %b %Y %T %z => Sat, 02 Sep 2023 14:09:56 +0900
The format string should follow the format of strftime(3) in man pages. The following list shows the supported format specifiers:
- %a: Abbreviated weekday name (Sun)
- %A: Full weekday name (Sunday)
- %b: Abbreviated month name (Jan)
- %B: Full month name (January)
- %c: Date and time representation
- %C: Year divided by 100 (00-99)
- %d: Day of the month (01-31)
- %D: Short MM/DD/YY date, equivalent to %m/%d/%y
- %e: Day of the month, with a space preceding single digits ( 1-31)
- %F: Equivalent to %Y-%m-%d (the ISO 8601 date format)
- %g: Week-based year, last two digits (00-99)
- %G: Week-based year
- %h: Abbreviated month name (Jan)
- %H: Hour in 24h format (00-23)
- %I: Hour in 12h format (01-12)
- %j: Day of the year (001-366)
- %m: Month as a decimal number (01-12)
- %M: Minute (00-59)
- %n: New-line character
- %p: AM or PM designation
- %P: am or pm designation
- %r: 12-hour clock time
- %R: 24-hour HH:MM time, equivalent to %H:%M
- %S: Second (00-59)
- %t: Horizontal-tab character
- %T: 24-hour clock time, equivalent to %H:%M:%S
- %u: ISO 8601 weekday as number with Monday as 1 (1-7)
- %U: Week number with the first Sunday as the first day of week (00-53)
- %V: ISO 8601 week number (01-53)
- %w: Weekday as a decimal number with Sunday as 0 (0-6)
- %W: Week number with the first Monday as the first day of week (00-53)
- %x: Date representation
- %X: Time representation
- %y: Year, last two digits (00-99)
- %Y: Year
- %z: ISO 8601 offset from UTC in timezone (+HHMM)
- %Z: Timezone name or abbreviation
- %+: Extended date and time representation
- %::z: Colon-separated offset from UTC in timezone (e.g. +05:00)
- %:::z: Like %::z, but with optional seconds
Example ¶
package main import ( "fmt" "github.com/Code-Hex/synchro" "github.com/Code-Hex/synchro/tz" ) func main() { t := synchro.New[tz.AsiaTokyo](2023, 9, 2, 14, 9, 56, 0) fmt.Println(t.Strftime("%Y-%m-%d %H:%M:%S")) fmt.Println(t.Strftime("%a, %d %b %Y %T %z")) }
Output: 2023-09-02 14:09:56 Sat, 02 Sep 2023 14:09:56 +0900
func (Time[T]) String ¶
String returns the time formatted using the format string
"2006-01-02 15:04:05.999999999 -0700 MST"
If the time has a monotonic clock reading, the returned string includes a final field "m=±<value>", where value is the monotonic clock reading formatted as a decimal number of seconds.
This is a simple wrapper method for (time.Time{}).String.
Example ¶
package main import ( "fmt" "github.com/Code-Hex/synchro" "github.com/Code-Hex/synchro/tz" ) func main() { timeWithNanoseconds := synchro.New[tz.UTC](2000, 2, 1, 12, 13, 14, 15) withNanoseconds := timeWithNanoseconds.String() timeWithoutNanoseconds := synchro.New[tz.UTC](2000, 2, 1, 12, 13, 14, 0) withoutNanoseconds := timeWithoutNanoseconds.String() fmt.Printf("withNanoseconds = %v\n", string(withNanoseconds)) fmt.Printf("withoutNanoseconds = %v\n", string(withoutNanoseconds)) }
Output: withNanoseconds = 2000-02-01 12:13:14.000000015 +0000 UTC withoutNanoseconds = 2000-02-01 12:13:14 +0000 UTC
func (Time[T]) Sub ¶
Sub returns the duration t-u. If the result exceeds the maximum (or minimum) value that can be stored in a Duration, the maximum (or minimum) duration will be returned. To compute t-d for a duration d, use t.Add(-d).
This is a simple wrapper method for (time.Time{}).Sub.
Example ¶
package main import ( "fmt" "github.com/Code-Hex/synchro" "github.com/Code-Hex/synchro/tz" ) func main() { start := synchro.New[tz.UTC](2000, 1, 1, 0, 0, 0, 0) end := synchro.New[tz.UTC](2000, 1, 1, 12, 0, 0, 0) difference := end.Sub(start) fmt.Printf("difference = %v\n", difference) }
Output: difference = 12h0m0s
func (Time[T]) Truncate ¶
Truncate returns the result of rounding t down to a multiple of d (since the zero time). If d <= 0, Truncate returns t stripped of any monotonic clock reading but otherwise unchanged.
This is a simple wrapper method for (time.Time{}).Truncate.
Example ¶
package main import ( "fmt" "time" "github.com/Code-Hex/synchro" "github.com/Code-Hex/synchro/tz" ) func init() { synchro.SetNow(func() time.Time { return time.Date(2023, 9, 2, 14, 0, 0, 0, time.UTC) }) } func main() { t, _ := synchro.Parse[tz.UTC]("2006 Jan 02 15:04:05", "2012 Dec 07 12:15:30.918273645") trunc := []time.Duration{ time.Nanosecond, time.Microsecond, time.Millisecond, time.Second, 2 * time.Second, time.Minute, 10 * time.Minute, } for _, d := range trunc { fmt.Printf("t.Truncate(%5s) = %s\n", d, t.Truncate(d).Format("15:04:05.999999999")) } // To round to the last midnight in the local timezone, create a new Date. midnight := synchro.New[tz.UTC](t.Year(), t.Month(), t.Day(), 0, 0, 0, 0) _ = midnight }
Output: t.Truncate( 1ns) = 12:15:30.918273645 t.Truncate( 1µs) = 12:15:30.918273 t.Truncate( 1ms) = 12:15:30.918 t.Truncate( 1s) = 12:15:30 t.Truncate( 2s) = 12:15:30 t.Truncate( 1m0s) = 12:15:00 t.Truncate(10m0s) = 12:10:00
func (Time[T]) Unix ¶
Unix returns t as a Unix time, the number of seconds elapsed since January 1, 1970 UTC. The result does not depend on the location associated with t. Unix-like operating systems often record time as a 32-bit count of seconds, but since the method here returns a 64-bit value it is valid for billions of years into the past or future.
This is a simple wrapper method for (time.Time{}).Unix.
Example ¶
package main import ( "fmt" "time" "github.com/Code-Hex/synchro" "github.com/Code-Hex/synchro/tz" ) func main() { // 1 billion seconds of Unix, three ways. fmt.Println(synchro.Unix[tz.UTC](1e9, 0)) // 1e9 seconds fmt.Println(synchro.Unix[tz.UTC](0, 1e18)) // 1e18 nanoseconds fmt.Println(synchro.Unix[tz.UTC](2e9, -1e18)) // 2e9 seconds - 1e18 nanoseconds t := synchro.New[tz.UTC](2001, time.September, 9, 1, 46, 40, 0) fmt.Println(t.Unix()) // seconds since 1970 fmt.Println(t.UnixNano()) // nanoseconds since 1970 }
Output: 2001-09-09 01:46:40 +0000 UTC 2001-09-09 01:46:40 +0000 UTC 2001-09-09 01:46:40 +0000 UTC 1000000000 1000000000000000000
func (Time[T]) UnixMicro ¶
UnixMicro returns t as a Unix time, the number of microseconds elapsed since January 1, 1970 UTC. The result is undefined if the Unix time in microseconds cannot be represented by an int64 (a date before year -290307 or after year 294246). The result does not depend on the location associated with t.
This is a simple wrapper method for (time.Time{}).UnixMicro.
func (Time[T]) UnixMilli ¶
UnixMilli returns t as a Unix time, the number of milliseconds elapsed since January 1, 1970 UTC. The result is undefined if the Unix time in milliseconds cannot be represented by an int64 (a date more than 292 million years before or after 1970). The result does not depend on the location associated with t.
This is a simple wrapper method for (time.Time{}).UnixMilli.
func (Time[T]) UnixNano ¶
UnixNano returns t as a Unix time, the number of nanoseconds elapsed since January 1, 1970 UTC. The result is undefined if the Unix time in nanoseconds cannot be represented by an int64 (a date before the year 1678 or after 2262). Note that this means the result of calling UnixNano on the zero Time is undefined. The result does not depend on the location associated with t.
This is a simple wrapper method for (time.Time{}).UnixNano.
func (*Time[T]) UnmarshalBinary ¶
UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
This is a simple wrapper method for (time.Time{}).UnmarshalBinary.
func (*Time[T]) UnmarshalJSON ¶
UnmarshalJSON implements the json.Unmarshaler interface. The time must be a quoted string in the RFC 3339 format.
This is a simple wrapper method for (time.Time{}).UnmarshalJSON.
func (*Time[T]) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface. The time must be in the RFC 3339 format.
This is a simple wrapper method for (time.Time{}).MarshalText.
func (Time[T]) Weekday ¶
Weekday returns the day of the week specified by t.
This is a simple wrapper method for (time.Time{}).Weekday.
func (Time[T]) Year ¶
Year returns the year in which t occurs.
This is a simple wrapper method for (time.Time{}).Year.
func (Time[T]) YearDay ¶
YearDay returns the day of the year specified by t, in the range [1,365] for non-leap years, and [1,366] in leap years.
This is a simple wrapper method for (time.Time{}).YearDay.
func (Time[T]) Zone ¶
Zone computes the time zone in effect at time t, returning the abbreviated name of the zone (such as "CET") and its offset in seconds east of UTC.
This is a simple wrapper method for (time.Time{}).Zone.
func (Time[T]) ZoneBounds ¶
ZoneBounds returns the bounds of the time zone in effect at time t. The zone begins at start and the next zone begins at end. If the zone begins at the beginning of time, start will be returned as a zero Time. If the zone goes on forever, end will be returned as a zero Time. The Location of the returned times will be the same as t.
This is a simple wrapper method for (time.Time{}).ZoneBounds.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
internal
|
|
scripts
|
|
Package tz provides timezone related types.
|
Package tz provides timezone related types. |