Allow for empty values, nillable parsing

This commit is contained in:
2024-01-18 03:08:09 -06:00
parent 849fa153c8
commit 0e9bcaf07c
2 changed files with 19 additions and 9 deletions

View File

@@ -184,15 +184,15 @@ type NaiveTime struct {
Minutes uint
}
func (nt NaiveTime) Sub(other NaiveTime) time.Duration {
func (nt *NaiveTime) Sub(other *NaiveTime) time.Duration {
return time.Hour*time.Duration(nt.Hours-other.Hours) + time.Minute*time.Duration(nt.Minutes-other.Minutes)
}
func ParseNaiveTime(integer uint64) NaiveTime {
func ParseNaiveTime(integer uint64) *NaiveTime {
minutes := uint(integer % 100)
hours := uint(integer / 100)
return NaiveTime{Hours: hours, Minutes: minutes}
return &NaiveTime{Hours: hours, Minutes: minutes}
}
func (nt NaiveTime) String() string {

View File

@@ -92,10 +92,15 @@ func (m *MeetingTimeResponse) EndDay() time.Time {
// StartTime returns the start time of the meeting time as a NaiveTime object
// This is not cached and is parsed on each invocation. It may also panic without handling.
func (m *MeetingTimeResponse) StartTime() NaiveTime {
value, err := strconv.ParseUint(m.MeetingTime.BeginTime, 10, 32)
func (m *MeetingTimeResponse) StartTime() *NaiveTime {
raw := m.MeetingTime.BeginTime
if raw == "" {
return nil
}
value, err := strconv.ParseUint(raw, 10, 32)
if err != nil {
log.Fatal().Err(err).Str("raw", m.MeetingTime.BeginTime).Msg("Cannot parse start time integer")
log.Fatal().Err(err).Str("raw", raw).Msg("Cannot parse start time integer")
}
return ParseNaiveTime(value)
@@ -103,10 +108,15 @@ func (m *MeetingTimeResponse) StartTime() NaiveTime {
// EndTime returns the end time of the meeting time as a NaiveTime object
// This is not cached and is parsed on each invocation. It may also panic without handling.
func (m *MeetingTimeResponse) EndTime() NaiveTime {
value, err := strconv.ParseUint(m.MeetingTime.EndTime, 10, 32)
func (m *MeetingTimeResponse) EndTime() *NaiveTime {
raw := m.MeetingTime.EndTime
if raw == "" {
return nil
}
value, err := strconv.ParseUint(raw, 10, 32)
if err != nil {
log.Fatal().Err(err).Str("raw", m.MeetingTime.EndTime).Msg("Cannot parse end time integer")
log.Fatal().Err(err).Str("raw", raw).Msg("Cannot parse end time integer")
}
return ParseNaiveTime(value)