Created
May 18, 2020 15:40
-
-
Save vadzappa/9e6bb84162d1e184a0325a911f32f15c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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