ztime

package
v0.0.0-...-d1c648a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 31, 2025 License: BSD-3-Clause Imports: 14 Imported by: 2

Documentation

Index

Constants

View Source
const (
	ISO8601Format           = "2006-01-02T15:04:05-0700"
	ISO8601NoZoneFormat     = "2006-01-02T15:04:05"
	ISO8601DateFormat       = "2006-01-02"
	ShortFormat             = "2006-01-02 15:04"
	JavascriptFormat        = "2006-01-02T15:04:05-07:00"
	JavascriptFormatWithSub = "2006-01-02T15:04:05.999-07:00"
	JavascriptISO           = "2006-01-02T15:04:05.999Z"
	FullCompact             = "06-Jan-02T15:04:05.9"
	NiceFormat              = "15:04:05 02-Jan-2006" // MaxSize of GetNice()
	RFC3339NoZ              = "2006-01-02T15:04:05-07:00"

	Day  = time.Hour * time.Duration(24)
	Week = Day * time.Duration(7)
)

Variables

View Source
var (
	Distant                  = time.Unix(1<<62, 0)
	BigTime                  = time.Date(2200, 12, 20, 0, 0, 0, 0, time.UTC) // time.Duration can max handle 290 years, so better than 3000. Setting to December 20th, so it is easily distinguishable from zero time
	TestTime                 = MustParse("2020-05-17T10:30:45.0+02:00")      // Random time we can use in tests when it has to look normal and be a fixed time
	ServerTimezoneOffsetSecs int
	SundayFirstWeekdays      = []time.Weekday{time.Sunday, time.Monday, time.Tuesday, time.Wednesday, time.Thursday, time.Friday, time.Saturday}
	Weekdays                 = []time.Weekday{time.Monday, time.Tuesday, time.Wednesday, time.Thursday, time.Friday, time.Saturday, time.Sunday}
)

Distant is a very far-future time when you want to do things forever etc

View Source
var ISODurRegEx = regexp.MustCompile(`P((?P<year>\d+)Y)?((?P<month>\d+)M)?((?P<day>\d+)D)?(T((?P<hour>\d+)H)?((?P<minute>\d+)M)?((?P<second>[\d\.]+)S)?)?`)

Functions

func AddMonthAndYearToTime

func AddMonthAndYearToTime(t time.Time, months, years int) time.Time

AddMonthAndYearToTime adds months and years to a date. It increases/decreases month-count, not using 30 days. If the results day of year is outside current month it is truncated.

func ChangedPartsOfDate

func ChangedPartsOfDate(t time.Time, y int, m time.Month, d int) time.Time

ChangedPartsOfDate changes the year, month or day part of a time Only non -1 parts changed.

func ChangedPartsOfTime

func ChangedPartsOfTime(t time.Time, h, m, s, ns int) time.Time

ChangedPartsOfTime changes the hour, minute second or nanosecond part of a time Only non -1 parts changed.

func DaysInMonth

func DaysInMonth(month time.Month, year int) int

DaysInMonth returns the number of days in a month for a given year

func DaysSince2000FromTime

func DaysSince2000FromTime(t time.Time) int

func DurSeconds

func DurSeconds(d time.Duration) float64

func DurationFromString

func DurationFromString(str string) time.Duration

func DurationToVerbal

func DurationToVerbal(d time.Duration, useSecs bool) string

func Equal

func Equal(a, b time.Time) bool

func GetDurNice

func GetDurNice(d time.Duration, fractDigits int) string

func GetDurationAsHMSString

func GetDurationAsHMSString(duration time.Duration, hours, mins, secs bool, subdigits int) string

func GetDurationHourMinSec

func GetDurationHourMinSec(d time.Duration) (hours int, mins int, secs int, fract float64)

func GetDurationString

func GetDurationString(d time.Duration, secs, mins, hours bool, subDigits int) (str string, overflow bool)

func GetHourAndAM

func GetHourAndAM(t time.Time) (hour int, am bool)

func GetNice

func GetNice(t time.Time, secs bool) string

GetNice returns a nice looking time with relative day.

func GetNiceSubSecs

func GetNiceSubSecs(t time.Time, subSecs int) string

GetNiceSubSecs is like GetNice, but adds seconds is subSecs >= 0, and sub-second decimals if > 0

func GetSecsFromHMSString

func GetSecsFromHMSString(str string, hour, min, sec bool) (float64, error)

func GetShort

func GetShort(t time.Time) string

func GetStartOfDay

func GetStartOfDay(t time.Time) time.Time

func GetStartOfToday

func GetStartOfToday() time.Time

func GetTimeWithServerLocation

func GetTimeWithServerLocation(t time.Time) time.Time

func IsBigTime

func IsBigTime(t time.Time) bool

func IsSameDay

func IsSameDay(a, b time.Time) bool

func IsToday

func IsToday(t time.Time) bool

IsToday returns true if t in local time is same day as now local.

func Max

func Max(a, b time.Time) time.Time

func Maximize

func Maximize(t *time.Time, with time.Time)

func Min

func Min(a, b time.Time) time.Time

func Minimize

func Minimize(t *time.Time, with time.Time)

func Minutes

func Minutes(m int) time.Duration

func MonthFromString

func MonthFromString(str string) (time.Month, bool)

func MustParse

func MustParse(s string) time.Time

MustParse is ued to parse a RFC3339Nano time from string, typically for using in literals.

func OnTheNextHour

func OnTheNextHour(t time.Time) time.Time

func ParseISO8601

func ParseISO8601(str string) (t time.Time, e error)

func ReplaceOldUnixTimeZoneNamesWithNew

func ReplaceOldUnixTimeZoneNamesWithNew(name string) string

func SecondsDur

func SecondsDur(s float64) time.Duration

func Since

func Since(t time.Time) float64

func TimeOfDaysSince2000

func TimeOfDaysSince2000(days int, loc *time.Location) time.Time

func TimeToNearestEmojii

func TimeToNearestEmojii(t time.Time) rune

func TimeToX

func TimeToX(minX, maxX float64, t, start, end time.Time) float64

func TimeZoneNameFromHourOffset

func TimeZoneNameFromHourOffset(offset float32) string

func WithIntegerSeconds

func WithIntegerSeconds(t time.Time) time.Time

func XToTime

func XToTime(minX, maxX float64, x float64, start, end time.Time) time.Time

Types

type Differ

type Differ time.Time

type DurationStruct

type DurationStruct struct {
	Years   int
	Weeks   int
	Months  int
	Days    int
	Hours   int
	Minutes int
	Seconds float32
}

func DurationStructFromISO

func DurationStructFromISO(dur string) (DurationStruct, error)

DurationStructFromISO reads an ISO 8601 dduration string.

func (DurationStruct) ToDuration

func (d DurationStruct) ToDuration() time.Duration

type EpochTime

type EpochTime time.Time // (un)marshals to epoch time in json

func (EpochTime) MarshalJSON

func (e EpochTime) MarshalJSON() ([]byte, error)

func (*EpochTime) UnmarshalJSON

func (e *EpochTime) UnmarshalJSON(b []byte) error

type FieldInc

type FieldInc struct {
	Field TimeFieldFlags
	Step  int
}

func NiceAxisIncrements

func NiceAxisIncrements(start, stop time.Time, pixelLength int) (inc time.Duration, labelInc FieldInc, first time.Time)

NiceAxisIncrements returns a FieldInc for the smallest duration to increment ticks or markings. The increment is returned as a Field in seconds, minutes or hours and a step rather than just a single duration, to alow further choices to be made.

func (FieldInc) IsModOfTimeZero

func (f FieldInc) IsModOfTimeZero(t time.Time) bool

func (FieldInc) Product

func (s FieldInc) Product() time.Duration

type JSONTime

type JSONTime time.Time

func (*JSONTime) UnmarshalJSON

func (jt *JSONTime) UnmarshalJSON(raw []byte) error

type SQLTime

type SQLTime struct {
	time.Time
}

func Make

func Make(t time.Time) SQLTime

func (*SQLTime) Scan

func (t *SQLTime) Scan(value interface{}) error

func (SQLTime) Value

func (t SQLTime) Value() (driver.Value, error)

type TimeFieldFlags

type TimeFieldFlags int
const (
	TimeFieldNone TimeFieldFlags = 1 << iota
	TimeFieldSecs
	TimeFieldMins
	TimeFieldHours
	TimeFieldDays
	TimeFieldMonths
	TimeFieldYears
	TimeFieldAMPM
	TimeFieldDateOnly
	TimeFieldTimeOnly
	TimeFieldNoCalendar
	TimeFieldShortYear
	TimeFieldStatic
	TimeFieldNotFutureIfAmbiguous // day, month, year are not present, subtract 1 to make it in past if current makes it future
)

func ParseDate

func ParseDate(date string, location *time.Location, flags TimeFieldFlags) (t time.Time, faults []TimeFieldFlags, err error)

func (TimeFieldFlags) Duration

func (f TimeFieldFlags) Duration() time.Duration

func (TimeFieldFlags) FieldValue

func (f TimeFieldFlags) FieldValue(t time.Time) int

func (TimeFieldFlags) String

func (f TimeFieldFlags) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL