Documentation ¶
Index ¶
- Constants
- Variables
- func RegisterFixedLocation(name string, secondsFromUTC int)
- type DateRangeType
- type DayTime
- type Duration
- type Exampler
- type ISO8601
- func (t ISO8601) Add(d time.Duration) ISO8601
- func (t ISO8601) After(u ISO8601) bool
- func (t ISO8601) Equal(u ISO8601) bool
- func (t ISO8601) IsZero() bool
- func (t ISO8601) ListExamples() any
- func (t ISO8601) MarshalJSON() ([]byte, error)
- func (t *ISO8601) Scan(src interface{}) error
- func (t ISO8601) String() string
- func (t ISO8601) Sub(u ISO8601) time.Duration
- func (t ISO8601) ToSimpleDate(in *time.Location) SimpleDate
- func (t *ISO8601) ToTime() time.Time
- func (t *ISO8601) UnmarshalJSON(b []byte) error
- func (t ISO8601) Value() (driver.Value, error)
- type KitchenTime
- type SimpleDate
- func (t SimpleDate) Add(d time.Duration) SimpleDate
- func (t SimpleDate) After(d2 SimpleDate) bool
- func (t SimpleDate) Before(d2 SimpleDate) bool
- func (t SimpleDate) CountPastDays(pastDate SimpleDate) int
- func (t SimpleDate) Equal(d SimpleDate) bool
- func (t SimpleDate) IsZero() bool
- func (t SimpleDate) ListExamples() any
- func (t SimpleDate) MarshalJSON() ([]byte, error)
- func (t *SimpleDate) Scan(src interface{}) error
- func (t SimpleDate) String() string
- func (t SimpleDate) ToTime() time.Time
- func (t *SimpleDate) UnmarshalJSON(data []byte) error
- func (t SimpleDate) Value() (driver.Value, error)
- type SimpleDates
- type TimeNotationDuration
Constants ¶
const ( // ISO8601Layout holds the time layout for the the javascript iso time. // Read more at: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString. ISO8601Layout = "2006-01-02T15:04:05" // ISO8601LayoutWithTimezone same as ISO8601Layout but with the timezone suffix. ISO8601LayoutWithTimezone = "2006-01-02T15:04:05Z" // To match Go’s standard time layout that pads zeroes for microseconds, you can use the format 2006-01-02T15:04:05.000000Z07:00. // This layout uses 0s instead of 9s for the fractional second part, which ensures that the microseconds are // always represented with six digits, padding with leading zeroes if necessary. // ISO8601ZUTCOffsetLayoutWithMicroseconds = "2006-01-02T15:04:05.000000Z07:00" // ISO8601ZUTCOffsetLayoutWithMicroseconds ISO 8601 format, with full time and zone with UTC offset. // Example: 2022-08-10T03:21:00.000000+03:00, 2023-02-04T09:48:14+00:00, 2022-08-09T00:00:00.000000. ISO8601ZUTCOffsetLayoutWithMicroseconds = "2006-01-02T15:04:05.999999Z07:00" // ISO8601ZUTCOffsetLayoutWithoutMicroseconds ISO 8601 format, with full time and zone with UTC offset without microsecond precision. ISO8601ZUTCOffsetLayoutWithoutMicroseconds = "2006-01-02T15:04:05Z07:00" )
const (
// DayTimeLayout holds the time layout for the the format of "hour:minute:second", hour can be 15, meaning 3 PM.
DayTimeLayout = "15:04:05"
)
const KitchenTimeLayout = "3:04 PM"
KitchenTimeLayout represents the "3:04 PM" Go time format, similar to time.Kitchen.
const (
// SimpleDateLayout represents the "year-month-day" Go time format.
SimpleDateLayout = "2006-01-02"
)
Variables ¶
var ErrParseKitchenTimeColon = fmt.Errorf("parse kitchen time: missing ':' character")
Functions ¶
func RegisterFixedLocation ¶
RegisterFixedLocation should be called on initialization of the program. It registers a fixed location to the time parser.
E.g. for input of 2023-02-04T09:48:14+03:00 to result a time string of 2023-02-04 09:48:14 +0300 EEST you have to RegisterFixedLocation("EEST", 10800) otherwise it will result to: 2023-02-04 09:48:14 +0300 +0300.
Types ¶
type DateRangeType ¶ added in v12.2.9
type DateRangeType string
DateRangeType is the type of the date range.
const ( // DayRange is the date range type of a day. DayRange DateRangeType = "day" // MonthRange is the date range type of a month. MonthRange DateRangeType = "month" // WeekRange is the date range type of a week. WeekRange DateRangeType = "week" // YearRange is the date range type of a year. YearRange DateRangeType = "year" )
type DayTime ¶
DayTime describes a time compatible with DayTimeLayout.
func ParseDayTime ¶
ParseDayTime reads from "s" and returns the DayTime time.
func (DayTime) MarshalJSON ¶
MarshalJSON writes a quoted string in the DayTime time format.
func (DayTime) String ¶
String returns the text representation of the "t" using the DayTime time layout.
func (*DayTime) UnmarshalJSON ¶
UnmarshalJSON parses the "b" into DayTime time.
type Duration ¶
Duration is a JSON representation of the standard Duration type, until Go version 2 supports it under the hoods.
func (Duration) MarshalJSON ¶
func (Duration) ToDuration ¶
func (*Duration) UnmarshalJSON ¶
type Exampler ¶ added in v12.2.7
type Exampler interface {
ListExamples() any
}
Exampler is an interface used by testing to generate examples.
type ISO8601 ¶
ISO8601 describes a time compatible with javascript time format.
func ParseISO8601 ¶
ParseISO8601 reads from "s" and returns the ISO8601 time.
The function supports the following formats:
- 2024-01-02T15:04:05.999999Z
- 2024-01-02T15:04:05+07:00
- 2024-04-08T08:05:04.830140+00:00
- 2024-01-02T15:04:05Z
- 2024-04-08T08:05:04.830140
- 2024-01-02T15:04:05
func (ISO8601) Equal ¶ added in v12.2.11
Equal reports whether the time instant "t" is equal to "u".
func (ISO8601) IsZero ¶
IsZero reports whether "t" is zero time. It completes the pg.Zeroer interface.
func (ISO8601) ListExamples ¶ added in v12.2.7
Examples returns a list of example values.
func (ISO8601) MarshalJSON ¶
MarshalJSON writes a quoted string in the ISO8601 time format.
func (ISO8601) String ¶
String returns the text representation of the "t" using the ISO8601 time layout.
func (ISO8601) ToSimpleDate ¶
func (t ISO8601) ToSimpleDate(in *time.Location) SimpleDate
ToSimpleDate converts the current ISO8601 "t" to SimpleDate in specific location.
func (*ISO8601) UnmarshalJSON ¶
UnmarshalJSON parses the "b" into ISO8601 time.
type KitchenTime ¶
KitchenTime holds a json "3:04 PM" time.
func ParseKitchenTime ¶
func ParseKitchenTime(s string) (KitchenTime, error)
ParseKitchenTime reads from "s" and returns the KitchenTime time.
func (KitchenTime) IsZero ¶
func (t KitchenTime) IsZero() bool
IsZero reports whether "t" is zero time. It completes the pg.Zeroer interface.
func (KitchenTime) MarshalJSON ¶
func (t KitchenTime) MarshalJSON() ([]byte, error)
MarshalJSON returns the json representation of the "t".
func (*KitchenTime) Scan ¶
func (t *KitchenTime) Scan(src interface{}) error
Scan completes the pg and native sql driver.Scanner interface reading functionality of a custom type.
func (KitchenTime) String ¶
func (t KitchenTime) String() string
String returns the text representation of the date formatted based on the `KitchenTimeLayout`. If date is zero it returns an empty string.
func (*KitchenTime) UnmarshalJSON ¶
func (t *KitchenTime) UnmarshalJSON(data []byte) error
UnmarshalJSON binds the json "data" to "t" with the `KitchenTimeLayout`.
func (KitchenTime) Value ¶
func (t KitchenTime) Value() time.Time
Value returns the standard time type.
type SimpleDate ¶
SimpleDate holds a json "year-month-day" time.
func ParseSimpleDate ¶
func ParseSimpleDate(s string) (SimpleDate, error)
ParseSimpleDate reads from "s" and returns the SimpleDate time.
The function supports the following formats:
- "2024-01-01"
- "2024-1-1"
func SimpleDateFromTime ¶
func SimpleDateFromTime(t time.Time) SimpleDate
SimpleDateFromTime accepts a "t" Time and returns a SimpleDate. If format fails, it returns the zero value of time.Time.
func (SimpleDate) Add ¶ added in v12.2.9
func (t SimpleDate) Add(d time.Duration) SimpleDate
Add returns the date of "t" plus "d".
func (SimpleDate) After ¶
func (t SimpleDate) After(d2 SimpleDate) bool
After reports whether the time instant t is after u.
func (SimpleDate) Before ¶
func (t SimpleDate) Before(d2 SimpleDate) bool
Before reports whether the time instant t is before u.
func (SimpleDate) CountPastDays ¶ added in v12.2.9
func (t SimpleDate) CountPastDays(pastDate SimpleDate) int
CountPastDays returns the count of days between "t" and "pastDate".
func (SimpleDate) Equal ¶ added in v12.2.9
func (t SimpleDate) Equal(d SimpleDate) bool
Equal reports back if "t" and "d" equals to the same date.
func (SimpleDate) IsZero ¶
func (t SimpleDate) IsZero() bool
IsZero reports whether "t" is zero time. It completes the pg.Zeroer interface.
func (SimpleDate) ListExamples ¶ added in v12.2.11
func (t SimpleDate) ListExamples() any
Examples returns a list of example values.
func (SimpleDate) MarshalJSON ¶
func (t SimpleDate) MarshalJSON() ([]byte, error)
MarshalJSON returns the json representation of the "t".
func (*SimpleDate) Scan ¶
func (t *SimpleDate) Scan(src interface{}) error
Scan completes the pg and native sql driver.Scanner interface reading functionality of a custom type.
func (SimpleDate) String ¶
func (t SimpleDate) String() string
String returns the text representation of the date formatted based on the `SimpleDateLayout`. If date is zero it returns an empty string.
func (SimpleDate) ToTime ¶
func (t SimpleDate) ToTime() time.Time
ToTime returns the standard time type.
func (*SimpleDate) UnmarshalJSON ¶
func (t *SimpleDate) UnmarshalJSON(data []byte) error
UnmarshalJSON binds the json "data" to "t" with the `SimpleDateLayout`.
type SimpleDates ¶ added in v12.2.9
type SimpleDates []SimpleDate
Slice of SimpleDate.
func GetSimpleDateRange ¶ added in v12.2.9
func GetSimpleDateRange(date SimpleDate, typ DateRangeType, startWeekday, endWeekday time.Weekday) SimpleDates
GetSimpleDateRange returns a slice of SimpleDate between "start" and "end" pf "date" based on given "typ" (WeekRange, MonthRange...).
Example Code: date := jsonx.SimpleDateFromTime(time.Now()) dates := jsonx.GetSimpleDateRange(date, jsonx.WeekRange, time.Monday, time.Sunday)
func (SimpleDates) Contains ¶ added in v12.2.9
func (t SimpleDates) Contains(date SimpleDate) bool
Contains reports if the "date" exists inside "t".
func (SimpleDates) DateStrings ¶ added in v12.2.9
func (t SimpleDates) DateStrings() []string
DateStrings returns a slice of string representation of the dates.
func (SimpleDates) First ¶ added in v12.2.9
func (t SimpleDates) First() SimpleDate
First returns the first element of the date slice.
func (SimpleDates) Last ¶ added in v12.2.9
func (t SimpleDates) Last() SimpleDate
Last returns the last element of the date slice.
func (*SimpleDates) Scan ¶ added in v12.2.9
func (t *SimpleDates) Scan(src interface{}) error
Scan completes the pg and native sql driver.Scanner interface.
type TimeNotationDuration ¶
TimeNotationDuration is a JSON representation of the standard Duration type in 00:00:00 (hour, minute seconds).
func ParseTimeNotationDuration ¶
func ParseTimeNotationDuration(s string) (TimeNotationDuration, error)
ParseTimeNotationDuration parses a string to a time notation duration (00:00:00) hours:minutes:seconds.
func (TimeNotationDuration) MarshalJSON ¶
func (d TimeNotationDuration) MarshalJSON() ([]byte, error)
func (*TimeNotationDuration) Set ¶
func (d *TimeNotationDuration) Set(v float64)
Set sets the value of duration in nanoseconds.
func (TimeNotationDuration) ToDuration ¶
func (d TimeNotationDuration) ToDuration() time.Duration
func (*TimeNotationDuration) UnmarshalJSON ¶
func (d *TimeNotationDuration) UnmarshalJSON(b []byte) error