Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Represent DateTimes using structs #463

Open
sharkdp opened this issue Jun 11, 2024 · 0 comments
Open

Represent DateTimes using structs #463

sharkdp opened this issue Jun 11, 2024 · 0 comments

Comments

@sharkdp
Copy link
Owner

sharkdp commented Jun 11, 2024

I was recently thinking that it might be nice to consider representing DateTimes using Numbat structs.

This would obviously still require some FFI code. We do not want to implement DateTimes in Numbat, we want to use chrono jiff for that. But representing them as structs would have the advantage that we could remove the specialized DateTime code from the type checker and some special operations in the VM. It would also allow us to remove some code that directly refers to Time and second, a connection between the language implementation and the standard library, that I would like to get rid of.

Ignoring timezones for a moment, we could have something like

use core::dimensions
use units::si

struct DateTime {
  unixtime: Time
}

fn get_current_unixtime() -> Scalar  # Implemented via FFI

fn now() = DateTime { unixtime: get_current_unixtime() * second }

Then, if we add full support for Add/Sub type classes (see #462), we could implement addition/subtraction:

impl Add<Time> for DateTime {
  type Output = DateTime

  fn add(lhs: DateTime, rhs: Time) -> DateTime = DateTime { unixtime: lhs.unixtime + rhs }
}

impl Sub<Time> for DateTime {}

impl Sub<DateTime> for DateTime {
  type Output = Time

  fn sub(lhs: DateTime, rhs: DateTime) -> Time = lhs.unixtime - rhs.unixtime
}

Timezone conversions would still work using the x -> f == f(x) trick, but would obviously also go through FFI functions. And parsing/formatting would also go through the FFI.

But the nice thing would be that we could use a unixtime-as-float (+ utc offset for timezone things) interface for that and implement the higher-level stuff in Numbat itself. DateTimes would be a higher-level construct implemented in Numbat alone.

To make this really nice, we would probably need a lot of new language features. In addition to the operator overloading stuff above, we would also require:

  • Something like a Display type class such that we would see nicely formatted times instead of DateTime { unix_time: 1_718_127_106 s }.
  • Private struct fields to prevent unwanted access to the internal representation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant