-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
31ecd42
commit f0cfb96
Showing
5 changed files
with
106 additions
and
56 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package duration | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strconv" | ||
) | ||
|
||
type Days int | ||
|
||
var regexpDays = regexp.MustCompile(`^P(P<days>[0-9]+)D$`) | ||
|
||
func ParseDays(s string) (Days, error) { | ||
var d Days | ||
|
||
if !regexpDays.MatchString(s) { | ||
return d, fmt.Errorf("invalid duration format: %s, expected format is 'PxD'", s) | ||
} | ||
|
||
matches := regexpDays.FindStringSubmatch(s) | ||
for i, name := range regexpDays.SubexpNames() { | ||
switch match := matches[i]; name { | ||
case "days": | ||
days, err := strconv.Atoi(match) | ||
if err != nil { | ||
return d, err | ||
} | ||
d = Days(days) | ||
} | ||
} | ||
|
||
return d, nil | ||
} | ||
|
||
func (d Days) String() string { | ||
return fmt.Sprintf("P%dD", d) | ||
} | ||
|
||
func (d Days) MarshalText() ([]byte, error) { | ||
return []byte(d.String()), nil | ||
} | ||
|
||
func (d *Days) UnmarshalText(data []byte) error { | ||
var err error | ||
*d, err = ParseDays(string(data)) | ||
return err | ||
} |
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,47 @@ | ||
package duration | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strconv" | ||
) | ||
|
||
type Nights int | ||
|
||
var regexpNights = regexp.MustCompile(`^P(P<nights>[0-9]+)N$`) | ||
|
||
func ParseNights(s string) (Nights, error) { | ||
var d Nights | ||
|
||
if !regexpNights.MatchString(s) { | ||
return d, fmt.Errorf("invalid duration format: %s, expected format is 'PxN'", s) | ||
} | ||
|
||
matches := regexpNights.FindStringSubmatch(s) | ||
for i, name := range regexpNights.SubexpNames() { | ||
switch match := matches[i]; name { | ||
case "nights": | ||
nights, err := strconv.Atoi(match) | ||
if err != nil { | ||
return d, err | ||
} | ||
d = Nights(nights) | ||
} | ||
} | ||
|
||
return d, nil | ||
} | ||
|
||
func (d Nights) String() string { | ||
return fmt.Sprintf("P%dN", d) | ||
} | ||
|
||
func (d Nights) MarshalText() ([]byte, error) { | ||
return []byte(d.String()), nil | ||
} | ||
|
||
func (d *Nights) UnmarshalText(data []byte) error { | ||
var err error | ||
*d, err = ParseNights(string(data)) | ||
return err | ||
} |
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