Overview
Thetimezone middleware detects user timezone from headers, cookies, or client hints for proper time display.
Use it when you need:
- Time localization
- Timezone-aware scheduling
- User time preferences
Installation
Quick Start
Configuration
Options
| Option | Type | Default | Description |
|---|---|---|---|
Default | string | "UTC" | Default timezone |
Header | string | "X-Timezone" | Timezone header |
CookieName | string | "tz" | Cookie name |
Examples
Basic Detection
Custom Header
With Default
Client-Side Detection
API Reference
Functions
Common Timezones
| Timezone | Description |
|---|---|
UTC | Coordinated Universal Time |
America/New_York | Eastern Time |
Europe/London | UK Time |
Asia/Tokyo | Japan Time |
Technical Details
Implementation
The timezone middleware uses a multi-source detection strategy to identify the user’s timezone:- Context Storage: Timezone information is stored in the request context using a type-safe context key
- Info Structure: Contains three key pieces of information:
Name: IANA timezone identifier (e.g., “America/New_York”)Location: Parsedtime.Locationobject for time conversionsOffset: UTC offset in seconds for the current time
- Lookup Order: Configurable detection precedence via the
Lookupoption (default: “header,cookie,query”) - Validation: Invalid timezone names automatically fall back to the configured default
- Cookie Management: Optional automatic cookie setting with configurable max age (default: 30 days)
Detection Flow
Helper Functions
The middleware provides several convenience functions:Get(c): Returns complete timezone infoLocation(c): Returnstime.Locationfor time operationsName(c): Returns timezone name stringOffset(c): Returns UTC offset in secondsNow(c): Returns current time in detected timezone
Convenience Constructors
FromHeader(header): Only check specific headerFromCookie(name): Only check specific cookieWithDefault(tz): Set custom default timezone
Best Practices
- Default to UTC for storage
- Convert to user timezone for display
- Use IANA timezone names
- Detect via JavaScript for accuracy
Testing
The middleware includes comprehensive test coverage for all functionality:| Test Case | Description | Expected Behavior |
|---|---|---|
TestNew | Default middleware creation | Returns UTC timezone when no timezone is detected |
TestWithOptions_FromHeader | Timezone detection from header | Reads timezone from X-Timezone header (America/New_York) |
TestWithOptions_FromCookie | Timezone detection from cookie | Reads timezone from cookie named “timezone” (Europe/London) |
TestWithOptions_FromQuery | Timezone detection from query parameter | Reads timezone from “tz” query parameter (Asia/Tokyo) |
TestWithOptions_SetCookie | Cookie setting functionality | Automatically sets timezone cookie when SetCookie=true |
TestWithOptions_InvalidTimezone | Invalid timezone handling | Falls back to UTC default when invalid timezone provided |
TestWithOptions_CustomDefault | Custom default timezone | Uses America/Los_Angeles as default instead of UTC |
TestLocation | Location helper function | Returns correct time.Location object (Europe/Paris) |
TestName | Name helper function | Returns timezone name string (Australia/Sydney) |
TestOffset | Offset helper function | Returns correct UTC offset in seconds (0 for UTC) |
TestNow | Now helper function | Returns current time in detected timezone location |
TestFromHeader | FromHeader constructor | Creates middleware that only reads from custom header (X-User-Tz) |
TestFromCookie | FromCookie constructor | Creates middleware that only reads from custom cookie (user_tz) |
TestWithDefault | WithDefault constructor | Creates middleware with custom default (Asia/Singapore) |
TestLookupPrecedence | Lookup order precedence | Header takes precedence over cookie and query when all are present |
Related Middlewares
- language - Language detection