date

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2017 License: BSD-2-Clause, ISC, MIT Imports: 6 Imported by: 0

README

godate

Golang package for intelligently parsing date strings like javascript's Date.parse() and getting the layout of date strings. Fully compatible with the native time package.

This package is still under development.

Usage

Installation
go get github.com/joyt/godate

In your program:

var timestamp time.Time
var err error
timestamp, err = date.Parse("Mar 14 2003")

Example

import (
  "github.com/joyt/godate"
  "time"
  "fmt"
)

func main() {
  var t time.Time
  
  t = date.MustParse("Aug 31 1999")
  fmt.Println(t)
  // Prints 1999-08-31 00:00:00 +0000 UTC
  
  t = date.MustParse("Tuesday, August 31, 1999")
  fmt.Println(t)
  // Prints 1999-08-31 00:00:00 +0000 UTC
  
  t = date.MustParse("Tue 31 Aug '99")
  fmt.Println(t)
  // Prints 1999-08-31 00:00:00 +0000 UTC
  
  t = date.MustParse("08/31/1999")
  fmt.Println(t)
  // Prints 1999-08-31 00:00:00 +0000 UTC
  
  t = date.MustParse("8/31/1999 20:05")
  fmt.Println(t)
  // Prints 1999-08-31 21:05:00 +0000 UTC
  
  t = date.MustParse("31/08/1999 8:05pm")
  fmt.Println(t)
  // Prints 1999-08-31 21:05:00 +0000 UTC
  
  t = date.MustParse("8/31/1999 8:05PM EST")
  fmt.Println(t)
  // Prints 1999-08-31 21:05:00 -0400 EDT
  
  t = date.MustParse("Aug-1999")
  fmt.Println(t)
  // Prints 1999-08-01 00:00:00 +0000 UTC
}

Notes

The parser is extremely lenient, and will try to interpret whatever it is given as a date as much as possible.

In cases where the meaning of the date is ambiguous (such as 6/09, which could mean Jun 9th or Jun 2009), the parser generally defaults to the higher resolution date (Jun 9th). An exception is made for dates without separators such as "200609", where the parser will always try to assume the year is first (200609 -> Sep 2006, NOT Jun 20th 2009).

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertGoLayoutToUnicode

func ConvertGoLayoutToUnicode(layout string) string

ConvertGoLayoutToUnicode converts the given time layout string to on using the Unicode standard prescribed in http://www.unicode.org/reports/tr35/tr35-19.html#Date_Format_Patterns NOT TESTED.

func Layout

func Layout(s string) string

Layout returns the layout of this date, appropriate for use with the Go time package, for example "Jan 02 2006". See http://golang.org/pkg/time/ for more examples.

func LayoutUnicode

func LayoutUnicode(s string) string

LayoutUnicode returns the layout of this date according to the Unicode standard: http://www.unicode.org/reports/tr35/tr35-19.html#Date_Format_Patterns NOT TESTED.

func MustParse

func MustParse(s string) time.Time

MustParse is like Parse except it panics if the string is not parseable.

func Parse

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

Parse attempts to parse this string as a timestamp, returning an error if it cannot. Example inputs: "July 9 1977", "07/9/1977 5:03pm". Assumes UTC if a timezone is not provided.

func ParseAndGetLayout

func ParseAndGetLayout(date string) (time.Time, string, error)

ParseAndGetLayout attempts to parse this string as a timestamp and if successful, returns the timestamp and the layout of the string.

func ParseInLocation

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

ParseInLocation is like Parse except it uses the given location when parsing the date.

Types

This section is empty.

Jump to

Keyboard shortcuts

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