OSDN Git Service

new repo
[bytom/vapor.git] / vendor / github.com / tendermint / tmlibs / common / date.go
1 package common
2
3 import (
4         "strings"
5         "time"
6
7         "github.com/pkg/errors"
8 )
9
10 // TimeLayout helps to parse a date string of the format YYYY-MM-DD
11 //   Intended to be used with the following function:
12 //       time.Parse(TimeLayout, date)
13 var TimeLayout = "2006-01-02" //this represents YYYY-MM-DD
14
15 // ParseDateRange parses a date range string of the format start:end
16 //   where the start and end date are of the format YYYY-MM-DD.
17 //   The parsed dates are time.Time and will return the zero time for
18 //   unbounded dates, ex:
19 //   unbounded start:   :2000-12-31
20 //       unbounded end:         2000-12-31:
21 func ParseDateRange(dateRange string) (startDate, endDate time.Time, err error) {
22         dates := strings.Split(dateRange, ":")
23         if len(dates) != 2 {
24                 err = errors.New("bad date range, must be in format date:date")
25                 return
26         }
27         parseDate := func(date string) (out time.Time, err error) {
28                 if len(date) == 0 {
29                         return
30                 }
31                 out, err = time.Parse(TimeLayout, date)
32                 return
33         }
34         startDate, err = parseDate(dates[0])
35         if err != nil {
36                 return
37         }
38         endDate, err = parseDate(dates[1])
39         if err != nil {
40                 return
41         }
42         return
43 }