A minimalist HTTP microservice that provides timezone-aware current time with automatic detection of upcoming timezone transitions (e.g., daylight saving time changes).
- Returns current time in milliseconds since Unix epoch (compatible with JavaScript, Java, etc.)
- Provides timezone offset in seconds
- Detects next timezone transition within 4 weeks
- Zero external dependencies (Go standard library only)
- CORS-enabled for browser requests
- Docker support with minimal image size
GET /time
| Header | Required | Default | Description |
|---|---|---|---|
X-Timezone |
No | UTC |
IANA timezone name (e.g., America/New_York, Europe/London) |
Content-Type: application/json
Format: JSON array with 4 elements
[
<current time in milliseconds>,
<current offset in seconds>,
<next transition time in milliseconds>,
<next offset in seconds>
]| Index | Type | Description | Nullable |
|---|---|---|---|
| 0 | number |
Current time in milliseconds since Unix epoch | No |
| 1 | number |
Current timezone offset in seconds (negative for west of UTC) | No |
| 2 | number |
Next timezone transition time in milliseconds since Unix epoch | Yes |
| 3 | number |
New timezone offset after transition in seconds | Yes |
Notes:
- Elements at index 2 and 3 are
nullif no timezone transition is found within the next 4 weeks - Timezone offsets are in seconds (e.g.,
-18000for UTC-5,3600for UTC+1) - The service searches up to 4 weeks ahead for timezone transitions
Content-Type: application/json
Cache-Control: no-store
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET,OPTIONS
Access-Control-Allow-Headers: X-Timezone
curl -H "X-Timezone: America/New_York" http://localhost:8080/timeResponse: 200 OK
[1739934000000,-18000,1741503600000,-14400]Interpretation:
- Current time:
1739934000000ms (Tue Feb 18 2025 22:00:00 GMT-0500 (Eastern Standard Time)) - Current offset:
-18000seconds (UTC-5, Eastern Standard Time) - Next transition:
1741503600000ms (Sun Mar 09 2025 03:00:00 GMT-0400 (Eastern Daylight Time)) - New offset:
-14400seconds (UTC-4, Eastern Daylight Time)
curl http://localhost:8080/timeResponse: 200 OK
[1739934000000,0,null,null]Interpretation:
- Current time:
1739934000000ms - Current offset:
0seconds (UTC) - No transition:
null(UTC has no DST transitions)
curl -H "X-Timezone: America/Phoenix" http://localhost:8080/timeResponse: 200 OK
[1739934000000,-25200,null,null]Interpretation:
- Current time:
1739934000000ms - Current offset:
-25200seconds (UTC-7, Arizona doesn't observe DST) - No transition:
null(no DST change in next 4 weeks)
Status: 400 Bad Request
Condition: The X-Timezone header contains an invalid IANA timezone name
Request:
curl -H "X-Timezone: Invalid/Timezone" http://localhost:8080/timeResponse:
invalid timezone
Common causes:
- Misspelled timezone name
- Non-existent timezone
- Invalid format (use IANA names like
America/New_York, not abbreviations likeEST)
Status: 500 Internal Server Error
Condition: JSON encoding fails (extremely rare)
Response:
internal error
go run main.goThe service listens on http://localhost:8080
Build the image:
docker build -t gotime .Run the container:
docker run -p 8080:8080 gotime| Variable | Description | Example |
|---|---|---|
OVERRIDE_CURRENT_TIME |
Override current time (Unix timestamp in seconds) for testing | 1736091600 |
Example:
OVERRIDE_CURRENT_TIME=1739934000 go run main.gogo test -vgo test -coverThe service uses a binary search algorithm to efficiently find the exact moment when a timezone transition occurs. When a request is received:
- Load the requested timezone (or default to UTC)
- Get the current time in that timezone
- Check 4 weeks ahead for offset changes
- If an offset change is detected, use binary search to pinpoint the exact second of transition
- Return current time, current offset, transition time (if any), and new offset (if any)
The binary search ensures efficient detection even for timezones with complex transition rules.
Public domain. See LICENSE.md.