Go Time Manipulation Toolkit
This challenge involves building a set of functions in Go to perform common time-based operations. Working with dates and times is a fundamental aspect of many applications, from scheduling and logging to financial calculations and data analysis. Mastering these operations will enhance your ability to manage temporal data effectively.
Problem Description
Your task is to create a Go package that provides a collection of utility functions for manipulating time.Time objects. This toolkit should allow users to perform various operations such as adding/subtracting durations, formatting times, checking time intervals, and calculating differences.
Key Requirements:
- Add Duration: A function that takes a
time.Timeobject and atime.Durationand returns a newtime.Timeobject representing the original time plus the duration. - Subtract Duration: A function that takes a
time.Timeobject and atime.Durationand returns a newtime.Timeobject representing the original time minus the duration. - Format Time: A function that takes a
time.Timeobject and a format string (following Go'stime.Time.Formatconventions) and returns the formatted time as a string. - Parse Time: A function that takes a time string and a format string and returns a
time.Timeobject and anerrorif parsing fails. - Is Within Interval: A function that takes a
time.Timeobject and two othertime.Timeobjects representing the start and end of an interval (inclusive of start, exclusive of end). It should returntrueif the given time falls within the interval, andfalseotherwise. - Time Difference: A function that takes two
time.Timeobjects and returns the absolute difference between them as atime.Duration.
Expected Behavior:
- All functions should be well-defined and handle standard Go
time.Timeandtime.Durationtypes. - The
Parse Timefunction must return an error when the input string does not match the provided format. - The
Is Within Intervalfunction should treat the interval as[start, end). - The
Time Differencefunction should return a non-negativetime.Duration.
Edge Cases to Consider:
- Parsing invalid time strings.
- Intervals where the start time is after the end time.
- Zero durations.
- Leap years and daylight saving time changes (handled inherently by
timepackage, but understanding their impact is good).
Examples
Example 1:
inputTime := time.Date(2023, 10, 26, 10, 0, 0, 0, time.UTC)
durationToAdd := 2 * time.Hour
// Expected Output: time.Date(2023, 10, 26, 12, 0, 0, 0, time.UTC)
Explanation: Adding 2 hours to 10:00 AM results in 12:00 PM on the same day.
Example 2:
inputTime := time.Date(2023, 10, 26, 10, 0, 0, 0, time.UTC)
durationToSubtract := 3 * time.Hour
// Expected Output: time.Date(2023, 10, 26, 7, 0, 0, 0, time.UTC)
Explanation: Subtracting 3 hours from 10:00 AM results in 7:00 AM on the same day.
Example 3:
inputTime := time.Date(2023, 10, 26, 15, 30, 0, 0, time.UTC)
format := "2006-01-02 15:04:05"
// Expected Output: "2023-10-26 15:30:00"
Explanation: Formatting the given time.Time object into the specified "YYYY-MM-DD HH:MM:SS" layout.
Example 4:
timeString := "2023/10/26 10:30 AM"
format := "2006/01/02 03:04 PM"
// Expected Output: time.Date(2023, 10, 26, 10, 30, 0, 0, time.Local), nil
Explanation: Parsing a time string using a specific format.
Example 5:
targetTime := time.Date(2023, 10, 26, 12, 0, 0, 0, time.UTC)
startTime := time.Date(2023, 10, 26, 10, 0, 0, 0, time.UTC)
endTime := time.Date(2023, 10, 26, 14, 0, 0, 0, time.UTC)
// Expected Output: true
Explanation: 12:00 PM is within the interval [10:00 AM, 2:00 PM).
Example 6:
time1 := time.Date(2023, 10, 26, 10, 0, 0, 0, time.UTC)
time2 := time.Date(2023, 10, 26, 13, 30, 0, 0, time.UTC)
// Expected Output: 3.5 * time.Hour
Explanation: The absolute difference between 10:00 AM and 1:30 PM is 3 hours and 30 minutes.
Constraints
- The input
time.Timeobjects will be valid Gotime.Timeinstances. - The input
time.Durationobjects will be valid Gotime.Durationinstances. - Format strings for
Format TimeandParse Timewill adhere to Go's standard layout conventions (e.g., "2006-01-02 15:04:05"). - For
Is Within Interval, assumestartTimeandendTimeare in the same timezone astargetTimeor can be consistently compared. - The
Parse Timefunction should return a zerotime.Timeand a descriptive error if parsing fails. - Performance is not a primary concern for this challenge, but inefficient algorithms (e.g., iterating through days to add years) should be avoided.
Notes
- Familiarize yourself with the Go
timepackage documentation. - Consider how to handle potential errors gracefully, especially in the
Parse Timefunction. - For
Is Within Interval, carefully consider the inclusive/exclusive nature of the boundaries. - The
time.Time.Equal,time.Time.Before, andtime.Time.Aftermethods will be useful. - The
time.Duration.Abs()method can be helpful forTime Difference.