{"id":31353,"date":"2021-01-18T15:48:33","date_gmt":"2021-01-18T20:48:33","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=31353"},"modified":"2021-01-18T15:48:33","modified_gmt":"2021-01-18T20:48:33","slug":"storing-the-time-zone-with-a-date","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2021\/01\/18\/storing-the-time-zone-with-a-date\/","title":{"rendered":"Storing the Time Zone With a Date"},"content":{"rendered":"<p><a href=\"https:\/\/harshil.net\/blog\/foundation-date\">Harshil Shah<\/a> (<a href=\"https:\/\/twitter.com\/_HarshilShah\/status\/1350028162550272001\">tweet<\/a>):<\/p>\n<blockquote cite=\"https:\/\/harshil.net\/blog\/foundation-date\">\n<p>Going back to how <code>Date<\/code> works, it doesn&rsquo;t model the actual clock time but rather a fixed point in time that can be interpreted in any time zone. And so what&rsquo;s happening here is that the data is being interpreted as if it happened in my current time zone, which is the default time zone that <code>Calendar<\/code> and <code>DateFormatter<\/code> use.<\/p>\n<p>And as such, a <code>Date<\/code> alone isn&rsquo;t sufficient for modelling historical data, or at least personal historical data: You need time zone information too.<\/p>\n<p>HealthKit acknowledges this too. You do have the ability to specify a time zone when constructing the appropriate <code>HKSample<\/code> subclass for the health data you&rsquo;re modelling. It just so happens that while you are required to submit the start and end dates for any sample, the time zone information is entirely optional and buried within a metadata dictionary, that you can even omit entirely.<\/p>\n<p>All of the step data shown in the screenshot was captured by the Health app right on my phone, stored in HealthKit, and displayed by the Health app. Somewhere in this pipeline, the time zone information was ignored or discarded.<\/p>\n<\/blockquote>\n\n<p><a href=\"https:\/\/twitter.com\/nicklockwood\/status\/1350096931016601602\">Nick Lockwood<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/nicklockwood\/status\/1350096931016601602\"><p>I was just talking about this a few minutes ago, specifically the bad decision Apple made of having a default locale\/timezone in most of their date-related APIs, which helps to perpetuate the misconception that a Date object is a self-contained representation of a calendar date.<\/p><\/blockquote>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2020\/03\/02\/time-swift-package\/\">&ldquo;Time&rdquo; Swift Package<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2017\/11\/10\/building-a-better-datetime-library-for-swift\/\">Building a Better Date\/Time Library for Swift<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2016\/02\/10\/dates-done-right\/\">Dates, Done Right<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2014\/12\/09\/date-formatters-calendars-and-locales\/\">Date Formatters, Calendars, and Locales<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Harshil Shah (tweet): Going back to how Date works, it doesn&rsquo;t model the actual clock time but rather a fixed point in time that can be interpreted in any time zone. And so what&rsquo;s happening here is that the data is being interpreted as if it happened in my current time zone, which is the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2021-01-18T20:48:37Z","apple_news_api_id":"30926edf-8905-4f3c-bb02-b08636c5f5f2","apple_news_api_modified_at":"2021-01-18T20:48:37Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/AMJJu34kFTzy7ArCGNsX18g","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[69,1202,920,31,1837,30,1891,71,364],"class_list":["post-31353","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-cocoa","tag-health-app","tag-healthkit","tag-ios","tag-ios-14","tag-mac","tag-macos-11-0","tag-programming","tag-time"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/31353","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=31353"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/31353\/revisions"}],"predecessor-version":[{"id":31354,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/31353\/revisions\/31354"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=31353"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=31353"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=31353"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}