Skip to content

Yet another attempt to refactor libstd's Duration #18416

Closed
@1-more

Description

@1-more

At the moment the Duration type looks like

pub struct Duraion {
    secs: i64,
    nanos: i32
}

According to this, one can expect that maximum possible duration value is at least i64::MAX seconds. But in fact, the full range that i64 seconds gives us is restricted to i64::MAX milliseconds (see #16626 for more details).

I offer to change the internal representation of Duration to

pub struct Duraion {
    ticks: i64,  // A single tick represents 100 nanoseconds  
    nanos: i8  // to hold values from -99 to 99
}

Some pros:

  1. Internals of struct Duraion are not exposed. So, we are free to change them.
  2. New representation is 3 bytes smaller.
  3. Methods like num_milliseconds etc will return correct i64 values (see libstd: Refactor Duration. #16626).
  4. TimeSpan in .NET and Duration in Joda-Time types do the same.

Some cons:

  1. The overall range of values for Duration will be shorter:
    • i64::MAX seconds gives us 106751991167300 days. We don't use need it.
    • i64::MAX milliseconds gives us 106751991167 days. This is our current choice.
    • i64::MAX ticks gives us 10675199 days. The range is still long enough, who cares?
  2. ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions