Hone logo
Hone
Problems

Implementing a Custom Duration in Go

The time.Duration type in Go represents a period of time. This challenge asks you to implement a simplified version of time.Duration from scratch, focusing on parsing, arithmetic, and formatting. This exercise will deepen your understanding of Go's type system, string parsing, and numerical operations.

Problem Description

You are tasked with creating a Duration type and associated functions to represent and manipulate time durations. The Duration type should store the duration in nanoseconds as an integer. You need to implement the following functionalities:

  1. Duration Type: Define a Duration type as an int64 representing nanoseconds.
  2. ParseString(s string) (Duration, error): This function should parse a string representation of a duration into a Duration value. The string format should support the following units:
    • ns (nanoseconds)
    • us (microseconds)
    • ms (milliseconds)
    • s (seconds)
    • m (minutes)
    • h (hours)
    • d (days) The string can contain an integer followed by one of the unit suffixes (e.g., "10ms", "2h", "1d"). Multiple durations can be combined in a single string separated by spaces (e.g., "1s 300ms").
  3. Add(a, b Duration) Duration: This function should add two Duration values and return the result.
  4. Sub(a, b Duration) Duration: This function should subtract two Duration values and return the result.
  5. String(d Duration) string: This function should format a Duration value into a human-readable string. Prioritize displaying the largest units first (days, hours, minutes, seconds, milliseconds, microseconds, nanoseconds). If a unit is zero, it should not be included in the output string. For example, 1s300ms is preferred over 1000ms.

Examples

Example 1:

Input: "1s 300ms"
Output: "1.300s"
Explanation: The input string is parsed into 1 second and 300 milliseconds, which are then combined into a single duration string.

Example 2:

Input: "2h 15m 30s"
Output: "2h15m30s"
Explanation: The input string is parsed into 2 hours, 15 minutes, and 30 seconds, which are combined into a single duration string.

Example 3:

Input: "1d 2h 30m"
Output: "1d2h30m"
Explanation: The input string is parsed into 1 day, 2 hours, and 30 minutes, which are combined into a single duration string.

Example 4:

Input: "500ms"
Output: "0.5s"
Explanation: The input string is parsed into 500 milliseconds, which is converted to 0.5 seconds.

Example 5:

Input: "1000000ns"
Output: "1us"
Explanation: The input string is parsed into 1000000 nanoseconds, which is converted to 1 microsecond.

Constraints

  • The input string s in ParseString can contain integers ranging from 0 to 1000000.
  • The input string s can contain multiple durations separated by spaces.
  • The output string in String should not contain trailing zeros after the decimal point (e.g., "1.0s" is acceptable, but "1.000s" is not).
  • The Duration type should store the duration in nanoseconds.
  • The String function should prioritize displaying the largest units first.

Notes

  • Consider using a map to store the unit suffixes and their corresponding multipliers.
  • Error handling is important in ParseString. Return an error if the input string is invalid.
  • The String function can be implemented recursively to handle different units.
  • Think about how to efficiently combine durations with different units into a single Duration value.
  • Focus on clarity and readability in your code. Good variable names and comments are encouraged.
Loading editor...
go