Documentation
¶
Index ¶
- Constants
- Variables
- func ConvertRelativeDateToActual(from string) string
- func FormatTime(t time.Time, outputFormat string) (string, error)
- func FormatUTCOffset(seconds int) string
- func ListIANAZones() []string
- func LoadZoneDefinitions() map[string]ZoneDefinition
- func ParseZoneAliases(spec string) (map[string]string, error)
- func Reformat(source string, outputFormat string) (string, error)
- type Conv
- type Diff
- type Dur
- type DurMath
- type OptionsConv
- type OptionsDiff
- type OptionsDur
- type OptionsDurMath
- func DurMathWithAbsolute(absolute bool) OptionsDurMath
- func DurMathWithBrief(brief bool) OptionsDurMath
- func DurMathWithDecimals(decimals int) OptionsDurMath
- func DurMathWithFirst(first string) OptionsDurMath
- func DurMathWithSecond(second string) OptionsDurMath
- func DurMathWithTarget(target string) OptionsDurMath
- type OptionsTimeZoneConverter
- type TimeZoneConverter
- type ZoneDefinition
Constants ¶
const ( ModName string = "DateTimeMate" ModVersion string = "1.23.0" ModUrl string = "https://github.com/jftuga/DateTimeMate" )
const DateOrderEnvVar = "DTMATE_DATE_ORDER"
DateOrderEnvVar names the environment variable that controls how an ambiguous slash-separated date such as "01/02/2024" is interpreted: "MDY" (month/day/year, the default) or "DMY" (day/month/year). Unambiguous dates such as "25/12/2024" parse the same way regardless of this variable. When an ambiguous date is parsed and the variable is not set, a warning naming the variable is written to stderr.
const ZoneAliasesEnvVar = "DTMATE_TZ_ALIASES"
ZoneAliasesEnvVar names the environment variable holding pipe-delimited abbreviation overrides, e.g. DTMATE_TZ_ALIASES="IST=Asia/Jerusalem|CST=Asia/Shanghai"
Variables ¶
var ( ErrInvalidTimezone = errors.New("invalid timezone specification") ErrEmptyInput = errors.New("empty input provided") ErrPre1970 = errors.New("date/times before 1970 are not converted by default because time zone data is unreliable before then") )
var ErrNegativeDuration = errors.New("durmath does not accept negative durations; use -a/--add or -s/--sub to control the operation")
ErrNegativeDuration is returned when either duration operand contains a negative amount; the sign of the operation is already carried by Add/Sub, so negative inputs are rejected.
var ReadmeMd string
ReadmeMd is the project README, embedded here (next to the file at the module root) because go:embed cannot reference parent directories from the cmd package; the CLI extracts its -e examples from it.
Functions ¶
func ConvertRelativeDateToActual ¶ added in v1.2.0
ConvertRelativeDateToActual converts "yesterday", "today", "tomorrow" into actual dates; yesterday and tomorrow are -/+ 24 hours of current time
func FormatTime ¶ added in v1.21.0
FormatTime renders an already-parsed time.Time using strftime format specifiers, with additional support for Unix seconds via '%s'. Unlike Reformat it performs no parsing, so the time's location (and therefore %Z, %z and %s) is preserved exactly.
Returns an error if the outputFormat is invalid.
func FormatUTCOffset ¶ added in v1.12.0
FormatUTCOffset renders an offset in seconds east of UTC as ±HH:MM
func ListIANAZones ¶ added in v1.12.0
func ListIANAZones() []string
ListIANAZones returns the IANA time zone names (e.g. America/New_York) that resolve against the time zone database in use, sorted; names from the generated ianaZoneNames list that no longer resolve are dropped
func LoadZoneDefinitions ¶ added in v1.12.0
func LoadZoneDefinitions() map[string]ZoneDefinition
LoadZoneDefinitions returns a map of timezone abbreviations to their definitions
func ParseZoneAliases ¶ added in v1.12.0
ParseZoneAliases parses pipe-delimited abbreviation overrides such as "IST=Asia/Jerusalem|CST=Asia/Shanghai"; keys are uppercased and every value must name a valid IANA time zone
func Reformat ¶ added in v1.2.0
Reformat converts a date/time string into a specified format. The source can be:
- A Unix timestamp (e.g., "1700265600")
- A relative date (e.g., "yesterday", "now")
- Any other parseable date format
The outputFormat parameter uses strftime format specifiers, with additional support for Unix seconds via '%s'.
Example usage:
s, err := Reformat("1700265600", "%Y-%m-%d") // Unix timestamp to date
s, err := Reformat("yesterday", "%Y-%m-%d %H:%M:%S") // Relative date to datetime
s, err := Reformat("2024-01-01", "%s") // Date to Unix timestamp
Returns an error if:
- The outputFormat is invalid
- The source date cannot be parsed
- The time parser initialization fails
Types ¶
type Conv ¶ added in v1.1.0
func NewConv ¶ added in v1.1.0
func NewConv(options ...OptionsConv) *Conv
func (*Conv) ConvertDuration ¶ added in v1.1.0
ConvertDuration converts a duration from the format specified in Conv.Source to the format specified in Conv.Target.
This function performs the following steps: 1. If the Source is in brief format (e.g., "1h30m"), it expands it to long format. 2. Parses the Source string to calculate the total duration in nanoseconds. 3. If the Target is in brief format, it expands it to a list of unit names. 4. Formats the duration according to the specified target units. 5. If Conv.Brief is true, it converts the result back to brief format.
The function handles both brief (e.g., "1h30m") and long (e.g., "1 hour 30 minutes") formats for both input and output. A leading "-" negates the whole source, and a net-negative total (e.g. "30 minutes -2 hours") is rendered with a leading "-".
Returns:
- string: The converted duration in the specified target format.
- error: An error if any step of the conversion process fails.
type Diff ¶
func NewDiff ¶
func NewDiff(options ...OptionsDiff) *Diff
func (*Diff) CalculateDiff ¶
CalculateDiff returns the time difference between Start and End, both as a formatted string and as a time.Duration; both sides are parsed with the same shared chain used by every other sub-command (parseDateTimeOrUnix) when Absolute is set, both the formatted string and the returned duration are non-negative
type DurMath ¶ added in v1.9.0
type DurMath struct {
First string
Second string
Target string
Brief bool
Decimals int
Absolute bool
}
DurMath adds or subtracts two durations, First and Second, which may be expressed in different units. Target optionally converts the result to a specific group of units; when empty, the result is a full breakdown from years down to seconds. Brief renders compact output such as "2h15m". Decimals sets the number of decimal places on the smallest output unit. Absolute renders the result without a sign, e.g. -15 minutes becomes 15 minutes.
func NewDurMath ¶ added in v1.9.0
func NewDurMath(options ...OptionsDurMath) *DurMath
NewDurMath returns a DurMath configured with the given options.
type OptionsConv ¶ added in v1.1.0
type OptionsConv func(*Conv)
func ConvWithBrief ¶ added in v1.1.0
func ConvWithBrief(brief bool) OptionsConv
func ConvWithDecimals ¶ added in v1.6.0
func ConvWithDecimals(decimals int) OptionsConv
ConvWithDecimals sets the number of decimal places used when formatting the last (smallest) target unit; 0 keeps the default integer truncation
func ConvWithSource ¶ added in v1.1.0
func ConvWithSource(source string) OptionsConv
func ConvWithTarget ¶ added in v1.1.0
func ConvWithTarget(target string) OptionsConv
type OptionsDiff ¶
type OptionsDiff func(*Diff)
func DiffWithAbsolute ¶ added in v1.10.0
func DiffWithAbsolute(absolute bool) OptionsDiff
DiffWithAbsolute makes CalculateDiff return an absolute (positive) duration and formatted string regardless of argument order
func DiffWithBrief ¶
func DiffWithBrief(brief bool) OptionsDiff
func DiffWithEnd ¶
func DiffWithEnd(end string) OptionsDiff
func DiffWithStart ¶
func DiffWithStart(start string) OptionsDiff
type OptionsDur ¶
type OptionsDur func(*Dur)
func DurWithDur ¶
func DurWithDur(d string) OptionsDur
func DurWithFrom ¶
func DurWithFrom(from string) OptionsDur
func DurWithOutputFormat ¶
func DurWithOutputFormat(outputFormat string) OptionsDur
func DurWithRepeat ¶
func DurWithRepeat(repeat int) OptionsDur
func DurWithUntil ¶
func DurWithUntil(until string) OptionsDur
type OptionsDurMath ¶ added in v1.9.0
type OptionsDurMath func(*DurMath)
OptionsDurMath is a functional option used to configure a DurMath.
func DurMathWithAbsolute ¶ added in v1.10.0
func DurMathWithAbsolute(absolute bool) OptionsDurMath
DurMathWithAbsolute makes Add and Sub return an absolute (positive) duration; a negative result is rendered without the leading "-"
func DurMathWithBrief ¶ added in v1.9.0
func DurMathWithBrief(brief bool) OptionsDurMath
DurMathWithBrief enables brief output, such as: 2h15m
func DurMathWithDecimals ¶ added in v1.9.0
func DurMathWithDecimals(decimals int) OptionsDurMath
DurMathWithDecimals sets the number of decimal places used when formatting the last (smallest) output unit; 0 keeps the default integer truncation
func DurMathWithFirst ¶ added in v1.9.0
func DurMathWithFirst(first string) OptionsDurMath
DurMathWithFirst sets the first duration operand.
func DurMathWithSecond ¶ added in v1.9.0
func DurMathWithSecond(second string) OptionsDurMath
DurMathWithSecond sets the second duration operand.
func DurMathWithTarget ¶ added in v1.9.0
func DurMathWithTarget(target string) OptionsDurMath
DurMathWithTarget sets the target units for the result; an empty target selects the default full-breakdown output.
type OptionsTimeZoneConverter ¶ added in v1.12.0
type OptionsTimeZoneConverter func(*TimeZoneConverter)
func TimeZoneConverterWithAliases ¶ added in v1.12.0
func TimeZoneConverterWithAliases(aliases map[string]string) OptionsTimeZoneConverter
func TimeZoneConverterWithAllowPre1970 ¶ added in v1.12.0
func TimeZoneConverterWithAllowPre1970(allow bool) OptionsTimeZoneConverter
func TimeZoneConverterWithZoneAbbrevs ¶ added in v1.12.0
func TimeZoneConverterWithZoneAbbrevs(zoneAbbrevs map[string]ZoneDefinition) OptionsTimeZoneConverter
type TimeZoneConverter ¶ added in v1.12.0
type TimeZoneConverter struct {
ZoneAbbrevs map[string]ZoneDefinition
Aliases map[string]string
AllowPre1970 bool
}
TimeZoneConverter converts date/times between time zones; ZoneAbbrevs supplies fixed UTC offsets for abbreviations such as EST or JST that are not resolvable as IANA zone names, Aliases maps abbreviations to IANA zone names and takes precedence over every other resolution, and AllowPre1970 permits conversions of date/times before 1970
func NewTimeZoneConverter ¶ added in v1.12.0
func NewTimeZoneConverter(options ...OptionsTimeZoneConverter) *TimeZoneConverter
NewTimeZoneConverter creates a new timezone converter with the given configuration
func (*TimeZoneConverter) ConvertTimeZone ¶ added in v1.12.0
func (c *TimeZoneConverter) ConvertTimeZone(sourceTime string, targetZone string) (time.Time, error)
ConvertTimeZone converts a date/time string to the target time zone; the source may end in its own zone (IANA name or abbreviation), otherwise it is parsed as a local date/time
func (*TimeZoneConverter) Warnings ¶ added in v1.12.0
func (c *TimeZoneConverter) Warnings(sourceTime, targetZone string) []string
Warnings reports the ambiguous zone abbreviations a conversion of the given source and target would rely on, excluding any overridden by an alias; each message names ZoneAliasesEnvVar so the user can override
type ZoneDefinition ¶ added in v1.12.0
ZoneDefinition describes a time zone abbreviation: its fixed UTC offset in seconds, a human readable description, and, for abbreviations that carry more than one real-world meaning, the other interpretations
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
dtmate
command
|
|
|
example
command
|
|
|
internal
|
|
|
datecalc
Package datecalc applies a signed number of calendar or clock units to a time.Time.
|
Package datecalc applies a signed number of calendar or clock units to a time.Time. |
|
dtparse
Package dtparse is the unified fallback parser for date/time strings that the strict wall-clock, zoned, and slash-date layers of the root package do not claim.
|
Package dtparse is the unified fallback parser for date/time strings that the strict wall-clock, zoned, and slash-date layers of the root package do not claim. |
|
humandur
Package humandur renders a time.Duration as a human-readable list of unit components, e.g.
|
Package humandur renders a time.Duration as a human-readable list of unit components, e.g. |