xtime

package
v0.0.0-...-ab29bdc Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2017 License: Apache-2.0 Imports: 9 Imported by: 1

README

xtime

xtime is a time parsing utility in Go. It exposes a set of time formats that it knows how to parse, and a single function Parse() to parse any time string.

import (
	"fmt"
	"time"
	"github.com/surgebase/parse/xtime"
)

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

Documentation

Overview

Package xtime is a time parser that parses the time without knowning the exact format.

Index

Examples

Constants

View Source
const (
	MaximumPOSIXBranches = 512
)

Variables

View Source
var (
	ErrNotImplemented         = errors.New("xtime: not implemented")
	ErrInvalidFormatString    = errors.New("xtime: invalid format string")
	ErrFormatStringTooComplex = errors.New("xtime: format string is too complex to be parsed")
	ErrInvalidTime            = errors.New("xtime: 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)

func IsTime

func IsTime(t string) bool

func Parse

func Parse(t string) (time.Time, error)
Example
t1, _ := time.Parse(time.RFC3339, "2006-01-02T15:04:05+07:00")
t2, err := 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)
}
Output:

func ParseInLocation

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

func TimezoneOffset

func TimezoneOffset(offset string) (hours int, minutes int, err error)

func TimezoneOffsetAt

func TimezoneOffsetAt(offset string, t time.Time) (hours int, minutes int, err error)

func WindowsLocationString

func WindowsLocationString(l *time.Location) (name string, err error)

Types

type TimeTree

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

func Compile

func Compile(formats []string) *TimeTree

func Root

func Root() *TimeTree

func (*TimeTree) IsTime

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

func (*TimeTree) Parse

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

func (*TimeTree) ParseInLocation

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

Jump to

Keyboard shortcuts

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