parseext

package
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2021 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	MaximumPOSIXBranches = 512
)

Variables

View Source
var (
	ErrNotImplemented         = errors.New("not implemented")
	ErrInvalidFormatString    = errors.New("invalid format string")
	ErrFormatStringTooComplex = errors.New("format string is too complex to be parsed")
	ErrInvalidTime            = errors.New("invalid time")
)
View Source
var TimeFormats = []string{
	"Mon Jan _2 15:04:05 2006",
	"Mon Jan 02 15:04:05 -0700 2006",
	"Mon Jan 02 15:04:05 -07:00 2006",
	"02 Jan 06 15:04 -0700",
	"02 Jan 06 15:04 -07:00",
	"Monday, 02-Jan-06 15:04:05",
	"Monday, 02-Jan-06 15:04:05 -0700",
	"Monday, 02-Jan-06 15:04:05 -07:00",
	"Mon, 02 Jan 2006 15:04:05",
	"Mon, 02 Jan 2006 15:04:05 -0700",
	"Mon, 02 Jan 2006 15:04:05 -07:00",
	"2006-01-02T15:04:05",
	"2006-01-02T15:04:05Z0700",
	"2006-01-02T15:04:05Z07:00",
	"2006-01-02T15:04:05.999999999Z07:00",
	"2006-01-02T15:04:05.999Z07:00",
	"Jan _2 15:04:05",
	"Jan _2 15:04:05.000",
	"Jan _2 15:04:05.000000",
	"Jan _2 15:04:05.000000000",
	"_2/Jan/2006:15:04:05 -0700",
	"_2/Jan/2006:15:04:05 -07:00",
	"Jan 2, 2006 3:04:05 PM",
	"Jan 2 2006 15:04:05",
	"Jan 2 15:04:05 2006",
	"Jan 2 15:04:05 -0700",
	"2006-01-02 15:04:05,000 -0700",
	"2006-01-02 15:04:05,000 -07:00",
	"2006-01-02 15:04:05 -0700",
	"2006-01-02 15:04:05 -07:00",
	"2006-01-02 15:04:05-0700",
	"2006-01-02 15:04:05-07:00",
	"2006-01-02 15:04:05,000",
	"2006-01-02 15:04:05",
	"2006/01/02 15:04:05",
	"2006/01/02",
	"2006-01-02",
	"01/02/2006",
	"01/02/2006 15:04:05",
	"06-01-02 15:04:05,000 -0700",
	"06-01-02 15:04:05,000 -07:00",
	"06-01-02 15:04:05,000",
	"06-01-02 15:04:05",
	"06/01/02 15:04:05",
	"15:04:05,000",
	"1/2/2006 3:04:05 PM",
	"1/2/06 3:04:05.000 PM",
	"1/2/2006 15:04",
	"1/2/2006",
	"2006/1/2",
}

TimeFormats is a list of commonly seen time formats from log messages.

Functions

func FromPOSIX

func FromPOSIX(format string) ([]string, error)

FromPOSIX converts a POSIX time format string like %Y-%m-%d and converts it to a list of time formats compatible with this package.

func IsTime

func IsTime(t string) bool

IsTime is a convenience function that calls Root().IsTime(),

func Parse

func Parse(t string) (time.Time, error)

Parse is a convenience function that calls Root().Parse().

Example
package main

import (
	"fmt"
	"time"

	"github.com/puppetlabs/leg/timeutil/pkg/parseext"
)

func main() {
	t1, _ := time.Parse(time.RFC3339, "2006-01-02T15:04:05+07:00")
	t2, err := parseext.Parse("2006-01-02T15:04:05+07:00")
	if err != nil {
		fmt.Println(err)
	} else if t1.UnixNano() != t2.UnixNano() {
		fmt.Printf("%d != %d\n", t1.UnixNano(), t2.UnixNano())
	} else {
		fmt.Println(t2)
	}
}

func ParseInLocation

func ParseInLocation(t string, loc *time.Location) (time.Time, error)

ParseInLocation is a convenience function that calls Root().ParseInLocation().

func ParsePOSIX

func ParsePOSIX(format, value string) (time.Time, error)

ParsePOSIX parses the given time string according to the given POSIX format.

Types

type TimeTree

type TimeTree struct {
	// contains filtered or unexported fields
}

TimeTree is a compiled list of time formats.

func Compile

func Compile(formats []string) *TimeTree

Compile creates a new TimeTree from a list of formats.

func Root

func Root() *TimeTree

Root returns the TimeTree corresponding to TimeFormats.

func (*TimeTree) IsTime

func (tt *TimeTree) IsTime(t string) bool

IsTime returns whether the given input would be considered a valid time value.

func (*TimeTree) Parse

func (tt *TimeTree) Parse(t string) (time.Time, error)

Parse uses all the formats in this tree to parse the value in the local time zone.

func (*TimeTree) ParseInLocation

func (tt *TimeTree) ParseInLocation(t string, loc *time.Location) (time.Time, error)

ParseInLocation uses all the formats in this tree to try to parse the given value in a particular time zone.

Jump to

Keyboard shortcuts

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