Documentation
¶
Index ¶
- Variables
- type ParseTime
- func (pt *ParseTime) ANSIC(value string) (time.Time, error)
- func (pt *ParseTime) GetLocation() *time.Location
- func (pt *ParseTime) ISO8601(value string) (time.Time, error)
- func (pt *ParseTime) Parse(value string) (time.Time, error)
- func (pt *ParseTime) RFC8xx1123(value string) (time.Time, error)
- func (pt *ParseTime) SetLocation(loc *time.Location)
- func (pt *ParseTime) US(value string) (time.Time, error)
Constants ¶
This section is empty.
Variables ¶
var ( // ISO8601, RFC3339 ISO8601 = strings.Join([]string{ `(?:`, year, ymdSep, month, ymdSep, day, `)?`, t, `(?:`, hour, hmsSep, min, hmsSep, sec, `?`, nsec, `)?`, s, offset, s, zone, }, "") // RFC822, RFC850, RFC1123 RFC8xx1123 = strings.Join([]string{ `(?:`, weekday, `,?`, s, `)?`, day, ymdSep, monthAbbr, ymdSep, shortYear, hmsSep, `(?:`, hour, hmsSep, min, hmsSep, sec, `?`, nsec, `)?`, s, offsetZone, }, "") ANSIC = strings.Join([]string{ `(?:`, weekday, s, `)?`, monthAbbr, ymdSep, day, ymdSep, `(?:`, hour, hmsSep, min, hmsSep, sec, `?`, nsec, `)?`, s, `(?:`, offsetZone, s, year, `)?`, }, "") US = strings.Join([]string{ `(?:`, monthAbbr, ymdSep, day, `(?:,)?`, ymdSep, shortYear, `)?`, s, `(?:at)?`, s, `(?:`, hour, hmsSep, min, hmsSep, sec, `?`, nsec, `)?`, s, ampm, `?`, s, usOffsetZone, }, "") Months = map[string]int{ "Jan": 1, "January": 1, "Feb": 2, "Februray": 2, "Mar": 3, "March": 3, "Apr": 4, "April": 4, "May": 5, "Jun": 6, "June": 6, "Jul": 7, "July": 7, "Aug": 8, "August": 8, "Sep": 9, "September": 9, "Oct": 10, "October": 10, "Nov": 11, "November": 11, "Dec": 12, "December": 12, } )
Regular expressions
Functions ¶
This section is empty.
Types ¶
type ParseTime ¶
type ParseTime struct {
// contains filtered or unexported fields
}
ParseTime parses the date/time string
func NewParseTime ¶
NewParseTime creates a new ParseTime instance with a specified time location. The location can be provided in several ways based on the number and type of arguments passed.
Parameters (variadic):
- With no arguments: Uses the current local timezone -
- With one argument: Can be either:
- 1. time.Location: Uses the provided location directly
- 2. string: Can be:
- - A. Empty string: Uses the current local timezone
- - B. Timezone name: Loads the location using time.LoadLocation
- - C. Timezone abbreviation: Attempts to parse using internal timezone database -
- With two arguments:
- 1. string: Name for the timezone
- 2. int: Offset in seconds from UTC
Returns:
- ParseTime: A new ParseTime instance configured with the specified location
- error: An error if the location couldn't be determined or if invalid arguments were provided
Example usage:
// Using local timezone
pt1, _ := NewParseTime()
// Using a specific timezone name
pt2, _ := NewParseTime("America/New_York")
// Using a fixed timezone
pt3, _ := NewParseTime("EST", -18000) // EST = UTC-5
// Using a time.Location
loc := time.UTC
pt4, _ := NewParseTime(loc)
Errors:
- Returns error if more than 2 arguments are provided
- Returns error if timezone name is invalid and not a recognized abbreviation
- Returns error if arguments are of invalid types
func (*ParseTime) ANSIC ¶
ANSIC parses a date/time string in ANSI C format using the ParseTime instance's location. This is a convenience wrapper around parseANSIC that omits the priority value from the return.
The ANSI C format is: "Mon Jan _2 15:04:05 2006" Format components:
- Weekday name (optional)
- Month name
- Day of month (space-padded for single digits)
- Hour:Minute:Second (24-hour format)
- Optional nanoseconds
- Optional timezone
- Year (4 digits)
Parameters:
- value: A string in ANSI C format
Returns:
- time.Time: The parsed time value
- error: An error if the string cannot be parsed in ANSI C format
Example usage:
pt, _ := NewParseTime("America/New_York")
// Basic format
t1, _ := pt.ANSIC("Jan 2 15:04:05 2006")
// With timezone
t2, _ := pt.ANSIC("Jan 2 15:04:05 EST 2006")
// With nanoseconds
t3, _ := pt.ANSIC("Jan 2 15:04:05.123456789 2006")
func (*ParseTime) GetLocation ¶
GetLocation returns *time.Location
func (*ParseTime) Parse ¶
Parse attempts to parse a date/time string using multiple format parsers, returning the most appropriate match. It tries the following formats in order:
- ISO8601 (e.g., "2006-01-02T15:04:05Z")
- RFC822/RFC850/RFC1123 (e.g., "Mon, 02 Jan 2006 15:04:05 MST")
- ANSI C (e.g., "Mon Jan _2 15:04:05 2006")
- US format (e.g., "Jan 2, 2006 3:04:05 PM MST")
The function uses a priority system to determine the best match when multiple formats are valid for the input string. The priority is based on how closely the input matches each format pattern, with lower priority values indicating better matches.
Parameters:
- value: A date/time string in any of the supported formats
Returns:
- time.Time: The parsed time value from the best matching format
- error: errInvalidDateTime if the string cannot be parsed in any supported format
Example usage:
pt, _ := NewParseTime("America/New_York")
// ISO8601 format
t1, _ := pt.Parse("2006-01-02T15:04:05Z")
// RFC1123 format
t2, _ := pt.Parse("Mon, 02 Jan 2006 15:04:05 MST")
// ANSI C format
t3, _ := pt.Parse("Jan 2 15:04:05 2006")
// US format
t4, _ := pt.Parse("Jan 2, 2006 3:04:05 PM")
Note: If a string is valid in multiple formats, the format with the closest match (lowest priority value) will be used to parse the final time value.
func (*ParseTime) RFC8xx1123 ¶
RFC8xx1123 parses a date/time string in RFC822, RFC850, or RFC1123 format using the ParseTime instance's location. This is a convenience wrapper around parseRFC8xx1123 that omits the priority value from the return.
Supported formats:
- RFC822: "02 Jan 06 15:04 MST"
- RFC850: "Monday, 02-Jan-06 15:04:05 MST"
- RFC1123: "Mon, 02 Jan 2006 15:04:05 MST"
Parameters:
- value: A string in RFC822, RFC850, or RFC1123 format
Returns:
- time.Time: The parsed time value
- error: An error if the string cannot be parsed in any of the supported formats
Example usage:
pt, _ := NewParseTime("America/New_York")
// Parse RFC822
t1, _ := pt.RFC8xx1123("02 Jan 06 15:04 EST")
// Parse RFC850
t2, _ := pt.RFC8xx1123("Monday, 02-Jan-06 15:04:05 EST")
// Parse RFC1123
t3, _ := pt.RFC8xx1123("Mon, 02 Jan 2006 15:04:05 EST")
func (*ParseTime) SetLocation ¶
SetLocation sets *time.Location
func (*ParseTime) US ¶
US parses a date/time string in US format using the ParseTime instance's location. This is a convenience wrapper around parseUS that omits the priority value from the return.
Supported formats include:
- Date only: "Jan 2, 2006" or "January 2, 2006" or "01/02/2006"
- With time: "Jan 2, 2006 3:04:05 PM"
- With timezone: "Jan 2, 2006 3:04:05 PM MST"
- With nanoseconds: "Jan 2, 2006 3:04:05.999999999 PM MST"
The function handles both 12-hour (with AM/PM) and 24-hour time formats. When only date is provided, time is set to midnight (00:00:00).
Parameters:
- value: A string in US date format
Returns:
- time.Time: The parsed time value
- error: An error if the string cannot be parsed in US format
Example usage:
pt, _ := NewParseTime("America/New_York")
// Parse date only
t1, _ := pt.US("Jan 2, 2006")
// Parse with time (12-hour format)
t2, _ := pt.US("January 2, 2006 3:04:05 PM")
// Parse with timezone
t3, _ := pt.US("Jan 2, 2006 15:04:05 EST")