-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CORE-1983: support the periods and end_date settings in the bulk subs…
…cription request endpoint
- Loading branch information
Showing
4 changed files
with
91 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package timestamp | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strconv" | ||
"time" | ||
) | ||
|
||
const ( | ||
DateOnly = time.DateOnly | ||
DateTimeLocal = "2006-01-02T15:04:05" | ||
RFC3339 = time.RFC3339 | ||
) | ||
|
||
var ( | ||
DateOnlyRegexp = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}$`) | ||
DateTimeLocalRegexp = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}$`) | ||
RFC3339Regexp = regexp.MustCompile(`^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(Z|[+-]\d{2}:\d{2})$`) | ||
) | ||
|
||
// Timestamp is an alias for time.Time with lenient parsing in the local time zone by default. | ||
type Timestamp time.Time | ||
|
||
// layoutForValue returns the layout to use for a given timestamp value. | ||
func layoutForValue(value string) (string, error) { | ||
switch { | ||
case DateOnlyRegexp.MatchString(value): | ||
return DateOnly, nil | ||
case DateTimeLocalRegexp.MatchString(value): | ||
return DateTimeLocal, nil | ||
case RFC3339Regexp.MatchString(value): | ||
return RFC3339, nil | ||
default: | ||
return "", fmt.Errorf("unrecognized timestamp layout: %s", value) | ||
} | ||
} | ||
|
||
// Parse attempts to parse the given value as a timestamp. The timestamp will be parsed in the time zone of the | ||
// current location unless the time zone is included in the timestamp itself. The accepted formats are: | ||
// | ||
// 2024-02-21 - Midnight on the specified date in the local time zone. | ||
// 2024-02-21T01:02:03 - The specified date and time in the local time zone. | ||
// 2024-02-21T01:02:03Z - The specified date and time in UTC. | ||
// 2024-02-01T01:02:03-07:00 - The specified date and time in the specified time zone. | ||
func Parse(value string) (Timestamp, error) { | ||
var t time.Time | ||
|
||
// Determine the timestamp layout. | ||
layout, err := layoutForValue(value) | ||
if err != nil { | ||
return Timestamp(t), err | ||
} | ||
|
||
// Parse the timestamp. | ||
t, err = time.ParseInLocation(layout, value, time.Now().Location()) | ||
return Timestamp(t), err | ||
} | ||
|
||
// UnmarshalJSON converts a JSON to a timestamp. | ||
func (t *Timestamp) UnmarshalJSON(data []byte) error { | ||
value := string(data) | ||
|
||
// Ignore empty values. | ||
if value == "null" || value == `""` { | ||
return nil | ||
} | ||
|
||
// Unquote the string. | ||
value, err := strconv.Unquote(value) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Parse the timestamp. | ||
*t, err = Parse(value) | ||
return err | ||
} |