Skip to content

Commit

Permalink
Add helpers for creating a Date from a string
Browse files Browse the repository at this point in the history
  • Loading branch information
printesoi committed Apr 11, 2024
1 parent 493e3e4 commit c69f31f
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions xml_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,25 @@ func NewDateFromTime(t time.Time) *Date {
return MakeDate(t.In(RoZoneLocation).Date()).Ptr()
}

// MakeDateFromString creates a Date in Romanian time zone from a string in the
// YYYY-MM-DD format.
func MakeDateFromString(str string) (Date, error) {
t, err := time.ParseInLocation(time.DateOnly, str, RoZoneLocation)
if err != nil {
return Date{}, err
}
return MakeDate(t.Date()), nil
}

// NewDateFromString same as MakeDateFromString, but returns a pointer to Date.
func NewDateFromString(str string) (*Date, error) {
d, err := MakeDateFromString(str)
if err != nil {
return nil, err
}
return d.Ptr(), nil
}

// MarshalXML implements the xml.Marshaler interface.
func (d Date) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
v := d.Format(time.DateOnly)
Expand Down

0 comments on commit c69f31f

Please sign in to comment.