Doc improvements

This commit is contained in:
Aaron Raddon 2017-07-26 22:37:37 -07:00
parent 56d24425b2
commit 66fb8cfe35
2 changed files with 29 additions and 22 deletions

View File

@ -11,24 +11,25 @@ Parse any date string without knowing format in advance. Uses a scanner to read
```go ```go
// Normal parse. If no recognized Timezone/Offset info // Normal parse. Equivalent Timezone rules as time.Parse()
// exists in the datestring, it uses UTC.
t, err := dateparse.ParseAny("3/1/2014") t, err := dateparse.ParseAny("3/1/2014")
// Parse with Location. If no recognized Timezone/Offset info // Parse with Location, equivalent to time.ParseInLocation() timezone/offset
// exists in the datestring, it uses given location. // rules. Using location arg, if timezone/offset info exists in the
// IF there IS timezone/offset info it uses the given location // datestring, it uses the given location rules for any zone interpretation.
// info for any zone interpretation. That is, MST means one thing // That is, MST means one thing when using America/Denver and something else
// when using America/Denver and something else in other locations. // in other locations.
denverLoc, _ := time.LoadLocation("America/Denver") denverLoc, _ := time.LoadLocation("America/Denver")
t, err := dateparse.ParseIn("3/1/2014", denverLoc) t, err := dateparse.ParseIn("3/1/2014", denverLoc)
// Set Location to time.Local. Same as ParseIn Location but // Set Location to time.Local. Same as ParseIn Location but lazily uses
// Lazily uses a global variable for Location Info. // the global time.Local variable for Location argument.
denverLoc, _ := time.LoadLocation("America/Denver") denverLoc, _ := time.LoadLocation("America/Denver")
// use time.Local global variable to store location // use time.Local global variable to store location
time.Local = denverLoc time.Local = denverLoc
t, err := dateparse.ParseLocal("3/1/2014") t, err := dateparse.ParseLocal("3/1/2014")
// Equivalent to
t, err := dateparse.ParseIn("3/1/2014", time.Local)
``` ```

View File

@ -59,7 +59,7 @@ var (
shortDates = []string{"01/02/2006", "1/2/2006", "06/01/02", "01/02/06", "1/2/06"} shortDates = []string{"01/02/2006", "1/2/2006", "06/01/02", "01/02/06", "1/2/06"}
) )
// MustParse Parse a date, and panic if it can't be parsed // Parse a date, and panic if it can't be parsed
func MustParse(datestr string) time.Time { func MustParse(datestr string) time.Time {
t, err := parseTime(datestr, nil) t, err := parseTime(datestr, nil)
if err != nil { if err != nil {
@ -68,18 +68,16 @@ func MustParse(datestr string) time.Time {
return t return t
} }
// ParseAny Given an unknown date format, detect the layout, parse. // Given an unknown date format, detect the layout, parse.
func ParseAny(datestr string) (time.Time, error) { func ParseAny(datestr string) (time.Time, error) {
return parseTime(datestr, nil) return parseTime(datestr, nil)
} }
// ParseIn Given an unknown date format, detect the layout, // Parse with Location, equivalent to time.ParseInLocation() timezone/offset
// using given location, parse. // rules. Using location arg, if timezone/offset info exists in the
// // datestring, it uses the given location rules for any zone interpretation.
// If no recognized Timezone/Offset info exists in the datestring, it uses // That is, MST means one thing when using America/Denver and something else
// given location. IF there IS timezone/offset info it uses the given location // in other locations.
// info for any zone interpretation. That is, MST means one thing when using
// America/Denver and something else in other locations.
func ParseIn(datestr string, loc *time.Location) (time.Time, error) { func ParseIn(datestr string, loc *time.Location) (time.Time, error) {
return parseTime(datestr, loc) return parseTime(datestr, loc)
} }
@ -87,10 +85,18 @@ func ParseIn(datestr string, loc *time.Location) (time.Time, error) {
// ParseLocal Given an unknown date format, detect the layout, // ParseLocal Given an unknown date format, detect the layout,
// using time.Local, parse. // using time.Local, parse.
// //
// If no recognized Timezone/Offset info exists in the datestring, it uses // Set Location to time.Local. Same as ParseIn Location but lazily uses
// given location. IF there IS timezone/offset info it uses the given location // the global time.Local variable for Location argument.
// info for any zone interpretation. That is, MST means one thing when using //
// America/Denver and something else in other locations. // denverLoc, _ := time.LoadLocation("America/Denver")
// time.Local = denverLoc
//
// t, err := dateparse.ParseLocal("3/1/2014")
//
// Equivalent to:
//
// t, err := dateparse.ParseIn("3/1/2014", denverLoc)
//
func ParseLocal(datestr string) (time.Time, error) { func ParseLocal(datestr string) (time.Time, error) {
return parseTime(datestr, time.Local) return parseTime(datestr, time.Local)
} }