mirror of
https://github.com/araddon/dateparse.git
synced 2024-11-10 11:51:54 +08:00
Improve doc on time.Location usage
This commit is contained in:
parent
3e1f751fed
commit
7dedf040d6
17
README.md
17
README.md
@ -11,14 +11,25 @@ Parse any date string without knowing format in advance. Uses a scanner to read
|
|||||||
|
|
||||||
```go
|
```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")
|
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")
|
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
|
||||||
|
// 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
|
Extended example https://github.com/araddon/dateparse/blob/master/example/main.go
|
||||||
|
10
parseany.go
10
parseany.go
@ -87,12 +87,22 @@ func ParseAny(datestr string) (time.Time, error) {
|
|||||||
|
|
||||||
// ParseIn Given an unknown date format, detect the layout,
|
// ParseIn Given an unknown date format, detect the layout,
|
||||||
// using given location, parse.
|
// 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) {
|
func ParseIn(datestr string, loc *time.Location) (time.Time, error) {
|
||||||
return parseTime(datestr, loc)
|
return parseTime(datestr, loc)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
||||||
|
// 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) {
|
func ParseLocal(datestr string) (time.Time, error) {
|
||||||
return parseTime(datestr, time.Local)
|
return parseTime(datestr, time.Local)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user