mirror of
https://github.com/araddon/dateparse.git
synced 2025-01-10 15:03:48 +08:00
Doc improvements
This commit is contained in:
parent
56d24425b2
commit
66fb8cfe35
19
README.md
19
README.md
@ -11,24 +11,25 @@ Parse any date string without knowing format in advance. Uses a scanner to read
|
||||
|
||||
```go
|
||||
|
||||
// Normal parse. If no recognized Timezone/Offset info
|
||||
// exists in the datestring, it uses UTC.
|
||||
// Normal parse. Equivalent Timezone rules as time.Parse()
|
||||
t, err := dateparse.ParseAny("3/1/2014")
|
||||
|
||||
// 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.
|
||||
// Parse with Location, equivalent to time.ParseInLocation() timezone/offset
|
||||
// rules. Using location arg, if timezone/offset info exists in the
|
||||
// datestring, it uses the given location rules 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.
|
||||
// Set Location to time.Local. Same as ParseIn Location but lazily uses
|
||||
// the global time.Local variable for Location argument.
|
||||
denverLoc, _ := time.LoadLocation("America/Denver")
|
||||
// use time.Local global variable to store location
|
||||
time.Local = denverLoc
|
||||
t, err := dateparse.ParseLocal("3/1/2014")
|
||||
// Equivalent to
|
||||
t, err := dateparse.ParseIn("3/1/2014", time.Local)
|
||||
|
||||
```
|
||||
|
||||
|
32
parseany.go
32
parseany.go
@ -59,7 +59,7 @@ var (
|
||||
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 {
|
||||
t, err := parseTime(datestr, nil)
|
||||
if err != nil {
|
||||
@ -68,18 +68,16 @@ func MustParse(datestr string) time.Time {
|
||||
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) {
|
||||
return parseTime(datestr, nil)
|
||||
}
|
||||
|
||||
// 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.
|
||||
// Parse with Location, equivalent to time.ParseInLocation() timezone/offset
|
||||
// rules. Using location arg, if timezone/offset info exists in the
|
||||
// datestring, it uses the given location rules 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)
|
||||
}
|
||||
@ -87,10 +85,18 @@ func ParseIn(datestr string, loc *time.Location) (time.Time, error) {
|
||||
// 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.
|
||||
// Set Location to time.Local. Same as ParseIn Location but lazily uses
|
||||
// the global time.Local variable for Location argument.
|
||||
//
|
||||
// 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) {
|
||||
return parseTime(datestr, time.Local)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user