102 lines
2.5 KiB
Go
102 lines
2.5 KiB
Go
package datetime
|
||
|
||
import (
|
||
"fmt"
|
||
"time"
|
||
|
||
. "gitea.zaclys.com/bvaudour/gob/option"
|
||
)
|
||
|
||
// Zero retourne la date correspondant au 01/01/0001 00:00:00 UTC.
|
||
// Si tz est fourni, la date est dans le fuseau horaire demandé,
|
||
// sinon, dans le fuseau horaire par défaut.
|
||
func Zero(tz ...string) Result[time.Time] {
|
||
var t time.Time
|
||
|
||
return toTZ(t, tz...)
|
||
}
|
||
|
||
// Now retourne la date actuelle.
|
||
func Now(tz ...string) Result[time.Time] {
|
||
t := time.Now()
|
||
|
||
return toTZ(t, tz...)
|
||
}
|
||
|
||
// Tomorrow retourne la date dans 24h.
|
||
func Tomorrow(tz ...string) Result[time.Time] {
|
||
now := Now(tz...)
|
||
if t, ok := now.Ok(); ok {
|
||
return Ok(AddDays(t, 1))
|
||
}
|
||
|
||
return now
|
||
}
|
||
|
||
// Yesterday retourne la date il y a 24h.
|
||
func Yesterday(tz ...string) Result[time.Time] {
|
||
now := Now(tz...)
|
||
if t, ok := now.Ok(); ok {
|
||
return Ok(AddDays(t, -1))
|
||
}
|
||
|
||
return now
|
||
}
|
||
|
||
// FromTimestamp convertit un timestamp (exprimé en secondes) en date.
|
||
func FromTimestamp(timestamp int64, tz ...string) Result[time.Time] {
|
||
t := time.Unix(timestamp, 0)
|
||
|
||
return toTZ(t, tz...)
|
||
}
|
||
|
||
// FromTimestampMilli convertit un timestamp (exprimé en ms) en date.
|
||
func FromTimestampMilli(timestamp int64, tz ...string) Result[time.Time] {
|
||
t := time.Unix(timestamp/1e3, (timestamp%1e3)*1e6)
|
||
|
||
return toTZ(t, tz...)
|
||
}
|
||
|
||
// FromTimestampMicro convertit un timestamp (exprimé en μs) en date.
|
||
func FromTimestampMicro(timestamp int64, tz ...string) Result[time.Time] {
|
||
t := time.Unix(timestamp/1e6, (timestamp%1e6)*1e3)
|
||
|
||
return toTZ(t, tz...)
|
||
}
|
||
|
||
// FromTimestampNano convertit un timestamp (exprimé en ns) en date.
|
||
func FromTimestampNano(timestamp int64, tz ...string) Result[time.Time] {
|
||
t := time.Unix(timestamp/1e9, timestamp%1e9)
|
||
|
||
return toTZ(t, tz...)
|
||
}
|
||
|
||
// FromDateTime retourne la date à partir des données numériques complètes.
|
||
func FromDateTime(year, month, day, hour, minute, second int, tz ...string) Result[time.Time] {
|
||
location, ok := getTZ(tz...).Get()
|
||
if !ok {
|
||
return Err[time.Time](fmt.Errorf(errInvalidTZ, tz))
|
||
}
|
||
|
||
t := time.Date(year, time.Month(month-1), day, hour, minute, second, 0, location)
|
||
|
||
return Ok(t)
|
||
}
|
||
|
||
// FromDate retourne le début du jour à partir des données de dates.
|
||
func FromDate(year, month, day int, tz ...string) Result[time.Time] {
|
||
return FromDateTime(year, month, day, 0, 0, 0)
|
||
}
|
||
|
||
// FromTime retourne la date d’aujourd’hui à l’heure indiquée.
|
||
func FromTime(hour, minute, second int, tz ...string) Result[time.Time] {
|
||
now := Now(tz...)
|
||
if !now.IsOk() {
|
||
return now
|
||
}
|
||
|
||
n, _ := now.Ok()
|
||
year, month, day := n.Date()
|
||
return FromDateTime(year, int(month+1), day, hour, minute, second, tz...)
|
||
}
|