dateparser

package module
v0.0.0-...-81e70b8 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2017 License: BSD-2-Clause Imports: 8 Imported by: 5

README

dateparser

A parser for arbitarily-formatted dates/times. It is a translation of the Python dateutil.parser module from Gustavo Niemeyer's dateutil package.

dateutil homepage: http://labix.org/python-dateutil

dateutil.parser source: http://bazaar.launchpad.net/~dateutil/dateutil/trunk/view/head:/dateutil/parser.py

Install

"go get"-able link:

go get github.com/kierdavis/dateparser

Alternatively, clone this repository by hand and install with:

git clone https://github.com/kierdavis/dateparser.git
cd dateparser
go install

Documentation

Godoc: http://godoc.org/github.com/kierdavis/dateparser

Example code: http://godoc.org/github.com/kierdavis/dateparser#_example_Parser-Parse

Documentation

Overview

Package dateparser implements a parser for arbitarily-formatted dates/times. It is a translation of the Python dateutil.parser module from Gustavo Niemeyer's dateutil package.

dateutil homepage: http://labix.org/python-dateutil

dateutil.parser source: http://bazaar.launchpad.net/~dateutil/dateutil/trunk/view/head:/dateutil/parser.py

This package comes with numerous unit tests (also ported from the dateutil project).

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Parse

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

Parses timestr using a parser with all values at their defaults. To get more control over the parsing, create an instance of Parser and use its Parse method.

Types

type ParseError

type ParseError struct {
	Timestr string // The whole input string that could not be parsed.
	Why     string // A textual description of why the parse failed.
	Where   string // The token in the input string that caused the failure.
}

This error is returned if the input could not be parsed. Unicode decoding errors are returned as they are, not converted into this error.

func (ParseError) Error

func (e ParseError) Error() string

Returns a string representation of the error.

type Parser

type Parser struct {
	// The default time (from which components not present in the input are
	// taken from). Defaults to the current time truncated to the nearest day
	// (i.e. midnight today).
	Default time.Time

	// Whether or not to perform a fuzzy search. Specifically, invalid tokens
	// are ignored if this is true and cause a parse failure if this is false.
	Fuzzy bool

	// In cases where the day and month cannot be distinguished (such as
	// 10-09-2003), this setting is checked to determine the preferred order.
	// With DayFirst set to false, "10-09-2003" will be parsed as "mm-dd-yyyy" (9th Oct 2003).
	// With DayFirst set to true, "10-09-2003" will be parsed as "dd-mm-yyyy" (10th Sep 2003).
	DayFirst bool

	// In cases where a 2-digit year cannot be distinguished from the day or
	// month (such as 10-09-03), this setting is checked to determine the
	// preferred order.
	// With YearFirst set to false, "10-09-03" will be parsed as "mm-dd-yy" (9th Oct 2003).
	// With YearFirst set to true, "10-09-03" will be parsed as "yy-mm-dd" (3rd Sep 2010).
	YearFirst bool

	// Whether or not to ignore timezones. This only affects the post-processing
	// of the result; timezone information in the input will still be parsed and
	// so still has the chance to trigger parsing errors. If this option is true
	// all returned times have a timezone of UTC+0.
	IgnoreTZ bool

	// A map of custom timezone names to their offsets in seconds. If a timezone
	// name is not found in this map, it is looked up using the
	// time.LoadLocation function.
	TZInfos map[string]int
}

Contains the settings used in parsing. An empty structure (new(Parser) or &Parser{}) is suffice to be able to parse dates.

func (*Parser) Parse

func (parser *Parser) Parse(timestr string) (t time.Time, err error)

Parses the input string and returns either a parsed date or an error. The error may be a ParseError, or an error returned by time.LoadLocation or bufio.Reader.ReadRune.

Example
parser := &Parser{}
t, err := parser.Parse("Thu, 25 Sep 2003 10:49:41")
if err != nil {
	fmt.Printf("Parse error: %s\n", err.Error())
}

fmt.Printf("year: %d, month: %d, day: %d\n", t.Year(), t.Month(), t.Day())
fmt.Printf("hour: %d, minute: %d, second: %d\n", t.Hour(), t.Minute(), t.Second())
Output:

year: 2003, month: 9, day: 25
hour: 10, minute: 49, second: 41

Jump to

Keyboard shortcuts

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