Improve doc on time.Location usage

This commit is contained in:
Aaron Raddon 2017-07-26 17:08:58 -07:00
parent 3e1f751fed
commit 7dedf040d6
2 changed files with 24 additions and 3 deletions

View File

@ -11,14 +11,25 @@ Parse any date string without knowing format in advance. Uses a scanner to read
```go
// Normal parse
// Normal parse. If no recognized Timezone/Offset info
// exists in the datestring, it uses UTC.
t, err := dateparse.ParseAny("3/1/2014")
// Parse with Location
// Parse with Location. If no recognized Timezone/Offset info
// exists in the datestring, it uses given location.
// IF there IS timezone/offset info it uses the given location
// 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")
t, err := dateparse.ParseIn("3/1/2014", denverLoc)
// Set Location to time.Local. Same as ParseIn Location but
// Lazily uses a global variable for Location Info.
denverLoc, _ := time.LoadLocation("America/Denver")
// use time.Local global variable to store location
time.Local = denverLoc
t, err := dateparse.ParseLocal("3/1/2014")
```
Extended example https://github.com/araddon/dateparse/blob/master/example/main.go

View File

@ -87,12 +87,22 @@ func ParseAny(datestr string) (time.Time, error) {
// ParseIn Given an unknown date format, detect the layout,
// using given location, parse.
//
// If no recognized Timezone/Offset info exists in the datestring, it uses
// given location. IF there IS timezone/offset info it uses the given location
// 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) {
return parseTime(datestr, loc)
}
// ParseLocal Given an unknown date format, detect the layout,
// using time.Local, parse.
//
// If no recognized Timezone/Offset info exists in the datestring, it uses
// given location. IF there IS timezone/offset info it uses the given location
// info for any zone interpretation. That is, MST means one thing when using
// America/Denver and something else in other locations.
func ParseLocal(datestr string) (time.Time, error) {
return parseTime(datestr, time.Local)
}