๐Ÿ‘จโ€๐Ÿณ Time Handling Cookbook

Practical recipes for common time handling scenarios. Copy, paste, and adapt these patterns to avoid time-related bugs.

๐Ÿ’พ 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.