Ask HN: Resources for learning/teaching time zone conversions and datetime math?
OP here, providing those examples... sorry this is so long:<p>- Sometimes a user is traveling, and their browser time (which we don&#x27;t really care about) will be different from their home time zone (which is what we want).<p>Workaround: We manually do the math to convert browser time to their home time zone, i.e. if their input is &quot;Jan 1, 00:00&quot;, Javascript assumes that&#x27;s in their browser time and saves it in memory as a UNIX epoch (milliseconds UTC). We have to then reverse that epoch time into an object representation (year, date, month), and create a new datetime using a helper library (Moment and&#x2F;or Luxon) that&#x27;s &quot;Jan 1, 00:00 in the home time zone we fetched from the profile API, disregarding whatever the browser said&quot;.<p>- Most of our backend databases store data points as ISO 8601 timestamps, which use offsets (-07:00) and not IANA zones (America&#x2F;Los_Angeles). These aren&#x27;t always the same, for example because of Daylight Savings Time changes. For part of the year, Los Angeles time is -7 UTC, but the rest of the year it&#x27;s -8 UTC. That means that with a given timestamp of 2020&#x2F;01&#x2F;01 00:00:00-07:00, we don&#x27;t know whether that site&#x27;s in Pacific Standard Time, Pacific Daylight Time, Mountain Standard Time during the winter, or Mountain Standard Time year-round because they&#x27;re in Arizona (which doesn&#x27;t observe daylight savings). To further complicate things, DST is something that lawmakers occasionally change on a regional or national basis, so a given time zone&#x27;s DST schedule isn&#x27;t fixed in stone but will change over the years.<p>Workaround: Mostly we just ignore this issue and pretend it doesn&#x27;t exist and pray for the best. For the most part it seems OK, since ISO timestamps are basically milliseconds, and if we need to manually calculate a given range from browser time, we can convert that using their home time zone and request that. But that&#x27;s a pretty ugly hack because it&#x27;s never really clear when a stored timestamp&#x27;s offset is meaningful vs when it&#x27;s not. Some devs will forget that conversion step because they assume that an offset is the same as a time zone, but that breaks because of daylight savings time.<p>- Complicating the previous point, sometimes we do data aggregations by day or week, but right now those aggregations disregard local midnights and combine all datapoints from UTC midnight to UTC midnight.<p>Workaround: I don&#x27;t think we have one right now, beyond hypothetically fetching both the target date and the days immediately before &amp; after, then recombining the datapoints on the client side to form a 24-hour day local to the user&#x27;s home time zone. Not nice =&#x2F;<p>- Further complicating the above, some of our subsystems don&#x27;t even store an offset, instead recording all their datetimes pegged to some other zone... sometimes UTC but not always, sometimes it&#x27;s just in whatever the original developer&#x27;s home time zone was.<p>Workaround: We have to manually convert these on a case-by-case basis. A lot of it lives as undocumented institutional knowledge and I worry these tribal secrets will be forgotten with turnover.<p>- Back on the frontend side, sometimes we also have to do duration math. Like a user might initially select Jan 1, 2020 to Mar 1, 2020, but then want to pan forward or back. But when they say &quot;forward a month&quot;, does that mean +90 days? Do we need to account for Feb 29 since 2020 was a leap year? Do we add +3 months given whatever local definition of &quot;month&quot; that time zone has, like gaining&#x2F;losing an hour from Daylight Savings Time?<p>Workaround: We use Luxon to help with this as much as possible (and it&#x27;s tremendously beneficial), but we still have to make some UI decisions about whether to be consistent about the technical implementation of the selected period of time (+&#x2F;- x milliseconds) or whether it&#x27;s more important to abide by the user&#x27;s intent (a &quot;month&quot; isn&#x27;t really 30 days, is sometimes longer and shorter and varies both within a year and across years). Ultimately we try to guess the most &quot;intuitive&quot; behavior for any given pan interaction, but it causes discrepancies between the daily&#x2F;7-day-&#x2F;30-day&#x2F;12-month views. The length of time selected will dynamically change based on what how granular a date range they&#x27;re looking at. Sometimes this will cause missing datapoints (like when DST kills an hour), but those will &quot;smooth out&quot;&#x2F;disappear if you zoom out enough to the weekly or monthly views.<p>---------------------<p>Phew. Those are just some of the examples I can remember off the top of my head. I&#x27;m sure there are other things I&#x27;m missing, but those are some of the subtle gotchas that we ran into. I&#x27;d super appreciate any resources you might know of that can help de-discombobulate this for us...