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:
DurationType: Define aDurationtype as anint64representing nanoseconds.ParseString(s string) (Duration, error): This function should parse a string representation of a duration into aDurationvalue. 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").
Add(a, b Duration) Duration: This function should add twoDurationvalues and return the result.Sub(a, b Duration) Duration: This function should subtract twoDurationvalues and return the result.String(d Duration) string: This function should format aDurationvalue 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,1s300msis preferred over1000ms.
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
sinParseStringcan contain integers ranging from 0 to 1000000. - The input string
scan contain multiple durations separated by spaces. - The output string in
Stringshould not contain trailing zeros after the decimal point (e.g., "1.0s" is acceptable, but "1.000s" is not). - The
Durationtype should store the duration in nanoseconds. - The
Stringfunction 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
Stringfunction can be implemented recursively to handle different units. - Think about how to efficiently combine durations with different units into a single
Durationvalue. - Focus on clarity and readability in your code. Good variable names and comments are encouraged.