# Swift Foundation Date Representation

**URL:** <https://forums.swift.org/t/swift-foundation-date-representation/77954>\
**Category:** Core Libraries\
**Tags:** date, databases\
**Created:** [February 19, 2025, 1:45am UTC](https://forums.swift.org/t/swift-foundation-date-representation/77954 "2025-02-19T01:45:26Z")\
**Posts on this page:** 9\
**Page:** 2

<div class="post-metadata">

**Author:** ![0xTim](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/0xtim/32/27968_2.png) [@0xTim](https://forums.swift.org/u/0xTim)\
**Post date:** [February 20, 2025, 3:31pm UTC](https://forums.swift.org/t/swift-foundation-date-representation/77954/21 "2025-02-20T15:31:10Z")

</div>

> [@xwu](#):
>
> If in your use case storage has to be in the database-native date format (rather than the raw bit pattern) and that format doesn't support sub-microsecond precision, then on any platform that supports sub-microsecond precision you're going to have this roundtripping problem regardless of what Swift does with its internal representation or serialization, no?

The problem I've had is that even if you truncate the date to only use microseconds, Double ends up rounding it differently so comparisons fail. If we used a type that didn't have this precision you could clamp your date to microseconds/milliseconds and it should 'just work' ™

---

<div class="post-metadata">

**Author:** ![stemmetje](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/stemmetje/32/37872_2.png) [@stemmetje](https://forums.swift.org/u/stemmetje)\
**Post date:** [February 20, 2025, 4:51pm UTC](https://forums.swift.org/t/swift-foundation-date-representation/77954/22 "2025-02-20T16:51:48Z")

</div>

> [@tera](#):
>
> That's true for Float / Double themselves as well? If so, I wonder why is that not a problem, just date?

Arguably, it's less of an issue if Float / Double types exhibit this behavior; that is the nature of floating point numbers. The whole point of [IEEE 754](https://en.wikipedia.org/wiki/IEEE_754)-style floating point numbers is that for many applications close approximations are good enough and therefore the speed benefits outweigh the inexactness problem. If you are aware of and agree to that tradeoff, then no harm done.

The fundamental problem with having a `Date` type that wraps a floating point number is that _dates shouldn't behave like floating point numbers_. The issue mentioned above is just one manifestation of the problems this fundamental type mismatch causes.

---

<div class="post-metadata">

**Author:** ![scanon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/scanon/32/2445_2.png) [@scanon](https://forums.swift.org/u/scanon)\
**Post date:** [February 24, 2025, 4:39pm UTC](https://forums.swift.org/t/swift-foundation-date-representation/77954/23 "2025-02-24T16:39:16Z")

</div>

A quick informal poll about how people use Date / want to use Date that will help inform consideration of the possible designs here:

"I need Date to be round-trippable to other formats down to \_\_\_\_\_\_\_ precision (i.e. if choosing seconds, I would expect a round-trip between Date and String to exactly preserve Dates with no fractional seconds component to be preserved exactly)"

_Poll ([view on site](https://forums.swift.org/t/swift-foundation-date-representation/77954/23))_

---

<div class="post-metadata">

**Author:** ![bbrk24](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/bbrk24/32/27370_2.png) [@bbrk24](https://forums.swift.org/u/bbrk24)\
**Post date:** [February 24, 2025, 6:43pm UTC](https://forums.swift.org/t/swift-foundation-date-representation/77954/24 "2025-02-24T18:43:14Z")

</div>

One special unit that isn't any of the above that I could see an argument for: if the system is interoperating with C# (say, an iOS app hitting a .NET server), C#'s `DateTime` type has a resolution of a tenth of a microsecond (= 100 ns).

Personally, I don't ever expect anything beyond the millisecond to be very meaningful unless the API advertises itself as being high-precision and suitable for such measurements. That being said I would interpret `TimeInterval`'s documentation ("sub-millisecond precision over a range of 10,000 years") to make such a guarantee.

---

<div class="post-metadata">

**Author:** ![CrownedPhoenix](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/crownedphoenix/32/21940_2.png) [@CrownedPhoenix](https://forums.swift.org/u/CrownedPhoenix)\
**Post date:** [February 28, 2025, 10:53pm UTC](https://forums.swift.org/t/swift-foundation-date-representation/77954/25 "2025-02-28T22:53:07Z")

</div>

I think for this poll it is important to make a distinction between the existing `Date` type being changed to be precise to a particular granularity vs adding a new type designed for much more precision (like ContinousClock.Instant).

I think it's perfectly reasonable that the builtin `Date` has limited precision (even down to milliseconds) so long as there does still exist a type for the use cases that do require more precision beyond what the default `Date` type facilitates.

My rationale for this is mostly a semantic thing where when I think about what a date is from a more "philosophical" standpoint, it isn't something that I would expect has arbitrary precision. For example, ISO8601 strings in the wild don't typically have precision beyond seconds or milliseconds.

But that doesn't change that I do want _some_ type that allows me greater precision for representing of points in time.

So as far as "how people use Date / want to use Date" - I think it's more a question of for workflows with high precision needs, how much precision is desired? And this I think is reflected in the current majority answering arbitrary precision.

I'm mentioning this out because I can totally understand if changing the existing Date is kind of a non-starter - but I don't think that makes the answers that have been provided any less valuable.

(Sorry if I'm pointing out the obvious)

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [February 28, 2025, 11:01pm UTC](https://forums.swift.org/t/swift-foundation-date-representation/77954/26 "2025-02-28T23:01:34Z")

</div>

> [@CrownedPhoenix](#):
>
> I'm mentioning this out because I can totally understand if changing the existing Date is kind of a non-starter - but I don't think that makes the answers that have been provided any less valuable.

Well, if you head over to the pitch for Foundation `UTCClock`, it's creating another distinct type that could live alongside `Date` that is considered a non-starter. So if both are non-starters, well...

---

<div class="post-metadata">

**Author:** ![amyworrall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/amyworrall/32/23701_2.png) [@amyworrall](https://forums.swift.org/u/amyworrall)\
**Post date:** [March 1, 2025, 1:04pm UTC](https://forums.swift.org/t/swift-foundation-date-representation/77954/27 "2025-03-01T13:04:31Z")

</div>

Would the issue here be 'solved' by adding a granularity-sensitive compare function to Date? Like:

```swift
date.compare(otherDate, granularity: .microsecond)

```

Then anyone can compare with whatever granularity works for their business logic and storage system…

---

<div class="post-metadata">

**Author:** ![davedelong](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/davedelong/32/38708_2.png) [@davedelong](https://forums.swift.org/u/davedelong)\
**Post date:** [March 1, 2025, 4:02pm UTC](https://forums.swift.org/t/swift-foundation-date-representation/77954/28 "2025-03-01T16:02:59Z")

</div>

Granularity comparison requires a Calendar to define the scales and ranges that correspond to each granularity, and a Date does not intrinsically have a Calendar. That's why granularity comparisons are instance methods on the Calendar type.

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [March 3, 2025, 8:12pm UTC](https://forums.swift.org/t/swift-foundation-date-representation/77954/29 "2025-03-03T20:12:37Z")

</div>

Open question: do we need the property of "linear growth" for Date?

Example that shows it's not linear now:

```swift
let a = Date() // now
let b = a.addingTimeInterval(0.0000002)
precondition(a != b) // ✅

let c = Date().addingTimeInterval(100*365*24*60*60) // ~ 100 years from now
let d = c.addingTimeInterval(0.0000002)
precondition(c != d) // 🛑

```

probably just because Date is backed by Double internally.

[Previous page](https://forums.swift.org/t/swift-foundation-date-representation/77954.md?page=1)
