clockworklabs / SpacetimeDB

Multiplayer at the speed of light
https://spacetimedb.com
Other
4.41k stars 110 forks source link

WIP: Make `Timestamp` a special SATS type #1836

Open gefjon opened 1 month ago

gefjon commented 1 month ago

Description of Changes

Companion PRs:

Still TODO:

API and ABI breaking changes

Expected complexity level and risk

2 - Changing the SATS definitions of types has been shown recently to be potentially scary. On the other hand, this redefinition is layout-compatible, so should be much less breaking than when we redefined Identity.

Testing

Describe any testing you've done, and any testing you'd like your reviewers to do, so that you're confident that all the changes work as expected!

gefjon commented 1 month ago

Having discussed this more with the team, there's an additional wrinkle: while C#'s DateTime is semantically correct as a translation for AlgebraicType::timestamp(), use of that type is fraught because of its locale-dependent printing. I have been encouraged to prefer DateTimeOffset, which maintains locale-independent printing by encoding a time zone, but that's no good because we don't store the time zone in AlgebraicType::timestamp() and so would have to discard it when serializing. We would still be representing the same point in time, but it would be, say, 12:00 UTC rather than 7:00 EST. Implicitly losing information in this way seems undesirable.

My disposition is to avoid these tough questions by retaining spacetimedb::Timestamp as a user-facing type (adding it to the client SDKs) and providing methods on it to convert to and from SystemTime or DateTimeOffset.

I'm also interested in providing a special SATS type analogous to Rust Duration or C# TimeSpan, for storage in ScheduleAt::Interval. I'm not sure what to name this type - I would prefer not to use either Duration or TimeSpan to avoid confusion when writing Rust and C# respectively. I'm considering TimeDuration, which is redundant but clear and unambiguous.

gefjon commented 3 weeks ago

TODO: Switch back to micros: u64, rather than nanos: i64. The latter is likely more correct, but the former is BSATN- and BFLATN-compatible with our old code, which is more valuable than the incremental improvement in precision or the ability to represent negative durations or pre-Unix timestamps.

EDIT: micros: i64, but otherwise done.