Hone logo
Hone
Problems

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:

  1. Add Duration: A function that takes a time.Time object and a time.Duration and returns a new time.Time object representing the original time plus the duration.
  2. Subtract Duration: A function that takes a time.Time object and a time.Duration and returns a new time.Time object representing the original time minus the duration.
  3. Format Time: A function that takes a time.Time object and a format string (following Go's time.Time.Format conventions) and returns the formatted time as a string.
  4. Parse Time: A function that takes a time string and a format string and returns a time.Time object and an error if parsing fails.
  5. Is Within Interval: A function that takes a time.Time object and two other time.Time objects representing the start and end of an interval (inclusive of start, exclusive of end). It should return true if the given time falls within the interval, and false otherwise.
  6. Time Difference: A function that takes two time.Time objects and returns the absolute difference between them as a time.Duration.

Expected Behavior:

  • All functions should be well-defined and handle standard Go time.Time and time.Duration types.
  • The Parse Time function must return an error when the input string does not match the provided format.
  • The Is Within Interval function should treat the interval as [start, end).
  • The Time Difference function should return a non-negative time.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 time package, 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.Time objects will be valid Go time.Time instances.
  • The input time.Duration objects will be valid Go time.Duration instances.
  • Format strings for Format Time and Parse Time will adhere to Go's standard layout conventions (e.g., "2006-01-02 15:04:05").
  • For Is Within Interval, assume startTime and endTime are in the same timezone as targetTime or can be consistently compared.
  • The Parse Time function should return a zero time.Time and 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 time package documentation.
  • Consider how to handle potential errors gracefully, especially in the Parse Time function.
  • For Is Within Interval, carefully consider the inclusive/exclusive nature of the boundaries.
  • The time.Time.Equal, time.Time.Before, and time.Time.After methods will be useful.
  • The time.Duration.Abs() method can be helpful for Time Difference.
Loading editor...
go