๐จโ๐ณ Time Handling Cookbook
Practical recipes for common time handling scenarios. Copy, paste, and adapt these patterns to avoid time-related bugs.
๐ Recipes
๐พ Storage & Retrieval
๐ฏ Store UTC, Display Local
The golden rule: always store timestamps in UTC, convert to local time only for display.
โ Common Mistakes
- โข Storing local time without timezone information
- โข Using naive datetime objects
- โข Hardcoding timezone offsets instead of using timezone names
- โข Converting to local time before storage
๐๏ธ Database Timezone Best Practices
Configure your database correctly to handle timezones and avoid data corruption.
๐ Timezone Conversion
๐ Safe Timezone Conversion
Convert between timezones safely, handling DST transitions and edge cases.
๐ Handling DST Transitions
Special handling for ambiguous and non-existent times during DST transitions.
๐ Parsing & Formatting
๐ Robust Date Parsing
Parse various date formats safely with proper error handling and validation.
โ Date Arithmetic
๐งฎ Safe Date Arithmetic
Perform date calculations safely, accounting for DST, leap years, and month boundaries.
โ Validation
๐ก๏ธ Input Validation
Validate date inputs thoroughly to prevent invalid data from entering your system.
๐งช Testing
๐ฌ Testing Time Logic
Write comprehensive tests for time-related functionality, including edge cases.