timedate

package
v1.2.2 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2025 License: MIT Imports: 3 Imported by: 1

Documentation

Index

Constants

View Source
const (
	// not used
	DATE_ONLY     TimeConvertSetting = "DateOnly"
	TIME_ONLY     TimeConvertSetting = "TimeOnly"
	DATE_AND_TIME TimeConvertSetting = "DateAndTime"

	// FORMAT_PB_DATETIME string = "2006-01-02 15:04:05.000Z"
	FORMAT_PB_DATETIME string = "2006-01-02 15:04:05.999Z"
	FORMAT_PB_TIME     string = "15:04:05.000Z"
	FORMAT_H_M         string = "15:04"
	EMPTY_DATE_STRING  string = "0001-01-01 00:00:0.000Z" // not sure we need this, but just in case we need to define "empty" date while variable must have value, then this is the value to denote empty date
	BIG_DATE_STRING    string = "3000-01-01 00:00:0.000Z" // sometime we need to check if date is less than but the end-date is empty

)

TODO: This was created originally for PocketBase, we need to change this to more standard format.

Variables

View Source
var (
	// NOTE: WARNING: IMPORTANT: PLEASE USE TimeToString before saving to PB if not it will not work!
	TimeToString       func(time.Time) string          = ConvertGoLangTimeToPBString
	StringToTime       func(string) time.Time          = ConvertDateTimeStringToGoLangTime
	DateStrToTime      func(string) time.Time          = ConvertDateOnlyStringToGoLangTime // convert "yyyy-mm-dd" to time.Time
	TimeStrToTime      func(string) time.Time          = ConvertTimeOnlyStringToGoLangTime
	EMPTY_DATE         time.Time                       = TimeStrToTime(EMPTY_DATE_STRING)
	ParseStringnToTime func(string) (time.Time, error) = ConvertManyTimeFormatToGoLangTime
)

ShortCut Only, Not really needed

Functions

func AddDaysToStringDate

func AddDaysToStringDate(dateStr string, days int) string

Add days into string date, can do negative days as well to substract dateStr by days dateStr = mm/dd/yyyy and return format is the same

func AddMinutesToDateTimeString

func AddMinutesToDateTimeString(datetimeStr string, minutes int) string

Add minutes to date-time string (PB format) and return PB format as well. Can use negative value for minutes to substract

func AddMinutesToDateTimeStringAsGoTime

func AddMinutesToDateTimeStringAsGoTime(datetimeStr string, minutes int) time.Time

Add minutes to datetimeStr and return as time

func ConvertDateOnlyStringToGoLangTime

func ConvertDateOnlyStringToGoLangTime(s string) time.Time

Convert string to time.Time (both date and time) Only accept yyyy-mm-dd format, else return .IsZero()

func ConvertDateTimeStringToGoLangTime

func ConvertDateTimeStringToGoLangTime(s string) time.Time

Convert string to time.Time (both date and time)

func ConvertGoLangTimeToPBString

func ConvertGoLangTimeToPBString(tt time.Time) string

Convert time.Time to string using default FORMAT_PB_DATETIME format

func ConvertGoLangTimeToStringWithFormat

func ConvertGoLangTimeToStringWithFormat(timeObj time.Time, format string) string

Convert time.Time to string with format (if empty return using FORMAT_PB_DATETIME)

func ConvertManyTimeFormatToGoLangTime

func ConvertManyTimeFormatToGoLangTime(str string) (time.Time, error)

Convert string (time and date) to GoLangTime parseTime tries to parse a string into a time.Time using common formats.

func ConvertTimeOnlyStringToGoLangTime

func ConvertTimeOnlyStringToGoLangTime(s string) time.Time

This is to convert string like 'hh:mm' | 'hh:mm:ss' | 'hh:mm:ss:nnnZ' format to time.Time struct

func DateStringABeforeB

func DateStringABeforeB(a, b string) bool

Compare date_only format for strings ie "2024-04-03" and "2024-04-20" then true

func DurationElapsedLong

func DurationElapsedLong(uptime time.Duration) string

This is usually to print the elapsed time for some function or db access usually is small units, so definitely no years and all. Output: 23 milliseconds,12 microseconds,39 nanoseconds

func DurationElapsedShort

func DurationElapsedShort(uptime time.Duration) string

This is usually to print the elapsed time for some function or db access usually is small units, so definitely no years -- until hours Output: 23ms,12µs,39ns

func DurationHUmanReadableWithOptions

func DurationHUmanReadableWithOptions(uptime time.Duration, opt HumanReadableOptions) string

func DurationHumanReadableLong

func DurationHumanReadableLong(uptime time.Duration) string

Default long format for duration string, prints every unit from year-to-nanoseconds Output: 2 days, 3 hours and 15 minutes

func DurationUptimeLong

func DurationUptimeLong(uptime time.Duration) string

Default long format for Uptime duration string Output: remove the seconds and smaller

func DurationUptimeShort

func DurationUptimeShort(uptime time.Duration) string

Default short format for Uptime duration string Output: remove the seconds and smaller 2d,20h,12m or just remove the comma as well: 2d20h12m

func ExtractDateFromString

func ExtractDateFromString(thedate string) string

Format that from PB usually is 2006-01-02 15:04:05.999Z we only need the first 10 chars

func ExtractDateTimeFromString

func ExtractDateTimeFromString(thedate string) string

Format for printing only mostly

func ExtractTimeFromString

func ExtractTimeFromString(thedate string) string

format from PB to get time only

func ExtractYearFromString

func ExtractYearFromString(thedate string) string

Format that from PB usually is 2006-01-02 15:04:05.999Z we only need the first 4 chars

func GetDaysFromDateTimeBToA

func GetDaysFromDateTimeBToA(a, b time.Time) int

get number of days between time B to time A (B - A). Except time.Time format and only dateStr

func GetFirstAndLastDayOfYear

func GetFirstAndLastDayOfYear(year int) (time.Time, time.Time)

Get start and end date of the year in time.Time (UTC)

func GetFirstDayOfYear

func GetFirstDayOfYear(year int) time.Time

Get first day of the year

func GetLastDayOfYear

func GetLastDayOfYear(year int) time.Time

func GetMinutesFromDateTimeBToA

func GetMinutesFromDateTimeBToA(a, b time.Time) float64

Meaning from time B --- to time A ==> meaning = B - A (if posistive means B is later, negative means B is before A)

func GetMinutesFromDateTimeStringBtoA

func GetMinutesFromDateTimeStringBtoA(a, b string) float64

Ignore error, accept "2024-12-03 10:23:00.000Z" format. B minus A

func GetMinutesFromTimeStringBToA

func GetMinutesFromTimeStringBToA(timeStr1, timeStr2 string) float64

Ignore error and return 0 instead. Except "10:32" and "13:20" format

func GetNumberOfWeeksInMonth

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

============= THIS IS UTILS functions if not yet exist getNumberOfWeeksInMonth calculates the number of weeks in a month

func GetRangeFromDate

func GetRangeFromDate(calcDate string, n int) (string, string)

Get n-days previous and n-days next from calcDate. Accept the dateOnly string Also return dateOnly format as string only, not time.Time

func GetWeekNumberInMonth

func GetWeekNumberInMonth(date time.Time) int

getWeekNumberInMonth determines the week number for a specific date

func IsDateStrInRange

func IsDateStrInRange(x, a, b string) bool

compare if string x is in between string a and string b and only compare the date. This takes string in dateOnly format 'yyyy-mm-dd'

func IsTimeStringExpired

func IsTimeStringExpired(expiresAt string) bool

Is timestring expired Check if expiresAt(string) is already expired (before) compared to time.now

func PBDateTimeStringABeforeB

func PBDateTimeStringABeforeB(a, b string) bool

func SetDateToTimeOnlyGoLangTime

func SetDateToTimeOnlyGoLangTime(timeonly, dateToSet time.Time) time.Time

days.TimeStringToTimeInstanceWithDate cannot be used because it parse timeonly as date.TimeOnly ("hh:mm:ss") format. While timeonly is usually

func TimeStringFromNow

func TimeStringFromNow(duration int) string

Convert int duration to time string : now + duration Output is RFC3339

Types

type HumanReadableOptions

type HumanReadableOptions struct {
	NanoSecondString   string
	MicroSecondString  string
	MilliSecondString  string
	SecondString       string
	MinuteString       string
	HourString         string
	DayString          string
	WeekString         string
	MonthString        string
	YearString         string
	ValueUnitSeparator string
	WithSpaceUnit      bool // space between units like: 20 days[this-space]20 hour
	WithComma          bool // comma between units like: 20 days, 20 hour
	WithAndEnd         bool // with "and" at the end: 20 days and 20 hour
	AutoSingular       bool // if true: if the value==1 then remove the last "s" at the end
}

func DefaultLongtHumanReadableOptions

func DefaultLongtHumanReadableOptions() HumanReadableOptions

func DefaultShortHumanReadableOptions

func DefaultShortHumanReadableOptions() HumanReadableOptions

type TimeConvertSetting

type TimeConvertSetting string

Jump to

Keyboard shortcuts

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