Documentation
¶
Index ¶
- type TimeRegistry
- func (tr *TimeRegistry) Date(fmt string, date any) (string, error)
- func (tr *TimeRegistry) DateAgo(date any) string
- func (tr *TimeRegistry) DateInZone(fmt string, date any, zone string) (string, error)
- func (tr *TimeRegistry) DateModify(fmt string, date time.Time) (time.Time, error)
- func (tr *TimeRegistry) Duration(sec any) string
- func (tr *TimeRegistry) DurationRound(duration any) string
- func (tr *TimeRegistry) HtmlDate(date any) (string, error)
- func (tr *TimeRegistry) HtmlDateInZone(date any, zone string) (string, error)
- func (tr *TimeRegistry) LinkHandler(fh sprout.Handler) error
- func (tr *TimeRegistry) Now() time.Time
- func (tr *TimeRegistry) RegisterAliases(aliasesMap sprout.FunctionAliasMap) error
- func (tr *TimeRegistry) RegisterFunctions(funcsMap sprout.FunctionMap) error
- func (tr *TimeRegistry) RegisterNotices(notices *[]sprout.FunctionNotice) error
- func (tr *TimeRegistry) Uid() string
- func (tr *TimeRegistry) UnixEpoch(date time.Time) string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type TimeRegistry ¶
type TimeRegistry struct {
// contains filtered or unexported fields
}
func NewRegistry ¶
func NewRegistry() *TimeRegistry
NewRegistry creates a new instance of conversion registry.
func (*TimeRegistry) Date ¶
func (tr *TimeRegistry) Date(fmt string, date any) (string, error)
Date formats a given date or current time into a specified format string.
Parameters:
fmt string - the format string. date any - the date to format or the current time if not a date type.
Returns:
string - the formatted date. error - when the timezone is invalid or the date is not in a valid format.
Example:
{{ "2023-05-04T15:04:05Z" | date "Jan 2, 2006" }} // Output: "May 4, 2023"
func (*TimeRegistry) DateAgo ¶
func (tr *TimeRegistry) DateAgo(date any) string
DateAgo calculates how much time has passed since the given date.
Parameters:
date any - the starting date for the calculation.
Returns:
string - a human-readable string describing how long ago the date was.
Example:
{{ "2023-05-04T15:04:05Z" | dateAgo }} // Output: "4m"
func (*TimeRegistry) DateInZone ¶
DateInZone formats a given date or current time into a specified format string in a specified timezone.
Parameters:
fmt string - the format string. date any - the date to format, in various acceptable formats. zone string - the timezone name.
Returns:
string - the formatted date. error - when the timezone is invalid or the date is not in a valid format.
Example:
{{ dateInZone "Jan 2, 2006", "2023-05-04T15:04:05Z", "UTC" }} // Output: "May 4, 2023"
TODO: Change signature
func (*TimeRegistry) DateModify ¶
func (*TimeRegistry) Duration ¶
func (tr *TimeRegistry) Duration(sec any) string
Duration converts seconds into a human-readable duration string.
Parameters:
sec any - the duration in seconds.
Returns:
string - the human-readable duration.
Example:
{{ 3661 | duration }} // Output: "1h1m1s"
func (*TimeRegistry) DurationRound ¶
func (tr *TimeRegistry) DurationRound(duration any) string
DurationRound rounds a duration to the nearest significant unit, such as years or seconds.
Parameters:
duration any - the duration to round.
Returns:
string - the rounded duration.
Example:
{{ "3600s" | durationRound }} // Output: "1h"
func (*TimeRegistry) HtmlDate ¶
func (tr *TimeRegistry) HtmlDate(date any) (string, error)
HtmlDate formats a date into a standard HTML date format (YYYY-MM-DD).
Parameters:
date any - the date to format.
Returns:
string - the formatted date in HTML format. error - when the timezone is invalid or the date is not in a valid format.
Example:
{{ "2023-05-04T15:04:05Z" | htmlDate }} // Output: "2023-05-04"
func (*TimeRegistry) HtmlDateInZone ¶
func (tr *TimeRegistry) HtmlDateInZone(date any, zone string) (string, error)
HtmlDateInZone formats a date into a standard HTML date format (YYYY-MM-DD) in a specified timezone.
Parameters:
date any - the date to format. zone string - the timezone name.
Returns:
string - the formatted date in HTML format. error - when the timezone is invalid or the date is not in a valid format.
Example:
{{ "2023-05-04T15:04:05Z", "UTC" | htmlDateInZone }} // Output: "2023-05-04"
TODO: Change signature
func (*TimeRegistry) LinkHandler ¶
func (tr *TimeRegistry) LinkHandler(fh sprout.Handler) error
LinkHandler links the handler to the registry at runtime.
func (*TimeRegistry) Now ¶
func (tr *TimeRegistry) Now() time.Time
Now returns the current time.
Returns:
time.Time - the current time.
Example:
{{ now }} // Output: "2023-05-07T15:04:05Z"
func (*TimeRegistry) RegisterAliases ¶ added in v0.6.0
func (tr *TimeRegistry) RegisterAliases(aliasesMap sprout.FunctionAliasMap) error
func (*TimeRegistry) RegisterFunctions ¶
func (tr *TimeRegistry) RegisterFunctions(funcsMap sprout.FunctionMap) error
RegisterFunctions registers all functions of the registry.
func (*TimeRegistry) RegisterNotices ¶ added in v0.6.0
func (tr *TimeRegistry) RegisterNotices(notices *[]sprout.FunctionNotice) error
func (*TimeRegistry) Uid ¶
func (tr *TimeRegistry) Uid() string
Uid returns the unique identifier of the registry.
func (*TimeRegistry) UnixEpoch ¶
func (tr *TimeRegistry) UnixEpoch(date time.Time) string
UnixEpoch returns the Unix epoch timestamp of a given date.
Parameters:
date time.Time - the date to convert to a Unix timestamp.
Returns:
string - the Unix timestamp as a string.
Example:
{{ now | unixEpoch }} // Output: "1683306245"