Skip to content

Instantly share code, notes, and snippets.

@vadzappa
Created May 18, 2020 15:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vadzappa/9e6bb84162d1e184a0325a911f32f15c to your computer and use it in GitHub Desktop.
Save vadzappa/9e6bb84162d1e184a0325a911f32f15c to your computer and use it in GitHub Desktop.
func CalculateEvectivityTimespan(dueDate, dueTime, duration string) (*protocol.EvectivityDateTime, *protocol.EvectivityDateTime, error) {
if dueTime == "" {
return dateOnlySpan(dueDate, duration)
}
return dateTimeSpan(dueDate, dueTime, duration)
}
func dateOnlySpan(dueDate, duration string) (*protocol.EvectivityDateTime, *protocol.EvectivityDateTime, error) {
start, err := time.Parse(PDDateFormat, dueDate)
if err != nil {
return nil, nil, errors.Wrap(err, "failed to parse due date")
}
end, err := addDuration(start, duration)
if err != nil {
return nil, nil, errors.Wrap(err, "failed to parse duration")
}
return &protocol.EvectivityDateTime{Date: start.Format(PDDateFormat)}, &protocol.EvectivityDateTime{Date: end.Format(PDDateFormat)}, nil
}
func dateTimeSpan(dueDate, dueTime, duration string) (*protocol.EvectivityDateTime, *protocol.EvectivityDateTime, error) {
start, err := time.Parse(PDDateFormat, fmt.Sprintf("%s %s", dueDate, dueTime))
if err != nil {
return nil, nil, errors.Wrap(err, "failed to parse due date")
}
end, err := addDuration(start, duration)
if err != nil {
return nil, nil, errors.Wrap(err, "failed to parse duration")
}
return &protocol.EvectivityDateTime{DateTime: start.Format(PDDateTimeFormat)}, &protocol.EvectivityDateTime{DateTime: end.Format(PDDateTimeFormat)}, nil
}
func addDuration(t time.Time, duration string) (time.Time, error) {
if duration == "" {
return t, nil
}
dur, err := parseDuration(duration)
if err != nil {
return time.Time{}, errors.Wrap(err, "failed to parse duration")
}
return t.Add(dur), nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment