Skip to content

Commit

Permalink
Adds support for specification of location in parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmycanady committed Mar 25, 2022
1 parent cbb77fa commit 9c51f05
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
18 changes: 17 additions & 1 deletion gocronometer.go
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,23 @@ func (c *Client) ExportServingsParsed(ctx context.Context, startDate time.Time,
return nil, fmt.Errorf("retreiving raw data: %s", err)
}

servings, err := ParseServingsExport(strings.NewReader(raw))
servings, err := ParseServingsExport(strings.NewReader(raw), time.UTC)
if err != nil {
return nil, fmt.Errorf("parsing raw data: %s", err)
}

return servings, nil
}

// ExportServingsParsedWithLocation is the same as ExportServingsParsed but sets the location of every recorded time
// to the location provided.
func (c *Client) ExportServingsParsedWithLocation(ctx context.Context, startDate time.Time, endDate time.Time, location *time.Location) (ServingRecords, error) {
raw, err := c.ExportServings(ctx, startDate, endDate)
if err != nil {
return nil, fmt.Errorf("retreiving raw data: %s", err)
}

servings, err := ParseServingsExport(strings.NewReader(raw), location)
if err != nil {
return nil, fmt.Errorf("parsing raw data: %s", err)
}
Expand Down
8 changes: 6 additions & 2 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ type ServingsExport struct {
Records ServingRecords
}

func ParseServingsExport(rawCSVReader io.Reader) (ServingRecords, error) {
func ParseServingsExport(rawCSVReader io.Reader, location *time.Location) (ServingRecords, error) {

r := csv.NewReader(rawCSVReader)

Expand Down Expand Up @@ -523,7 +523,11 @@ func ParseServingsExport(rawCSVReader io.Reader) (ServingRecords, error) {
if timeStr == "" {
timeStr = "00:00 AM"
}
serving.RecordedTime, err = time.Parse("2006-01-02 15:04 PM", date+" "+timeStr)

if location == nil {
location = time.UTC
}
serving.RecordedTime, err = time.ParseInLocation("2006-01-02 15:04 PM", date+" "+timeStr, location)
if err != nil {
return nil, fmt.Errorf("parsing record time: %s", err)
}
Expand Down

0 comments on commit 9c51f05

Please sign in to comment.