Time Zone Conversions and Calculations in Go
This challenge focuses on implementing common time zone conversions and calculations using Go's time package. Accurate time handling is crucial in many applications, from scheduling tasks to displaying localized times, and this exercise will solidify your understanding of Go's time manipulation capabilities. You'll be working with time zones, durations, and formatting to achieve specific time-related tasks.
Problem Description
You are tasked with creating a Go program that performs several time-related operations. The program should:
- Convert a time from one time zone to another: Given a time in UTC and a target time zone, convert the time to the specified time zone.
- Calculate the difference between two times: Given two times (both in the same time zone), calculate the duration between them.
- Add a duration to a time: Given a time and a duration, add the duration to the time and return the resulting time.
- Format a time: Given a time and a specific format string, format the time according to the provided format.
The program should be modular, with separate functions for each of these operations. Error handling is essential; functions should return errors when appropriate (e.g., invalid time zone names).
Key Requirements:
- Use the
timeandtime/tzdatapackages. - Handle potential errors gracefully.
- Ensure the code is well-documented and readable.
- The program should be executable and produce the expected output.
Expected Behavior:
The program should take user input (or use hardcoded values for testing) and perform the specified time operations. It should then print the results to the console, including any error messages.
Edge Cases to Consider:
- Invalid time zone names.
- Times that are very close together (to avoid potential floating-point precision issues when calculating durations).
- Times that are in the past or future.
- Leap seconds (though a full leap second implementation is beyond the scope of this challenge, be aware of the potential complexity).
Examples
Example 1:
Input: UTC time: 2023-10-27 10:00:00, Target Time Zone: America/Los_Angeles
Output: America/Los_Angeles time: 2023-10-27 03:00:00
Explanation: Converts 2023-10-27 10:00:00 UTC to 2023-10-27 03:00:00 in America/Los_Angeles.
Example 2:
Input: Time 1: 2023-10-27 10:00:00, Time 2: 2023-10-27 10:30:00 (both in America/New_York)
Output: Duration: 30m
Explanation: Calculates the duration between 2023-10-27 10:00:00 and 2023-10-27 10:30:00 in America/New_York, which is 30 minutes.
Example 3:
Input: Time: 2023-10-27 12:00:00, Duration: 1h30m, Format: 2006-01-02 15:04:05
Output: 2023-10-27 13:30:00
Explanation: Adds 1 hour and 30 minutes to 2023-10-27 12:00:00, resulting in 2023-10-27 13:30:00.
Constraints
- Time zone names must be valid IANA time zone names (e.g., "America/Los_Angeles", "Europe/London").
- The input time should be a valid RFC3339 formatted string (e.g., "2006-01-02T15:04:05Z07:00").
- The duration should be a string representing a duration (e.g., "1h30m", "5m").
- The format string should be a valid Go time format string (e.g., "2006-01-02 15:04:05").
- The program should execute within 1 second for typical inputs.
Notes
- The
time.Parsefunction is useful for converting strings totime.Timeobjects. - The
time.LoadLocationfunction is used to load a time zone location. - The
time.Addandtime.Subfunctions are used for adding and subtracting durations. - The
time.Formatfunction is used for formatting atime.Timeobject into a string. - Consider using a helper function to parse durations from strings. This will improve code readability and maintainability.
- Error handling is critical. Always check for errors and handle them appropriately. Returning errors from your functions allows the calling code to gracefully handle failures.