Commit graph

347 commits

Author SHA1 Message Date
Andrew Gallant
8d9ff39020
0.2.5 2025-03-22 20:40:58 -04:00
Andrew Gallant
86e1497117
deps: bump to jiff-tzdb-platform 0.1.3 2025-03-22 20:40:14 -04:00
Andrew Gallant
939549520e
jiff-tzdb-platform-0.1.3 2025-03-22 20:39:37 -04:00
Andrew Gallant
ebd7ee41f3
deps: bump to jiff-tzdb 0.1.4 2025-03-22 20:39:28 -04:00
Andrew Gallant
3ff4513edf
jiff-tzdb-0.1.4 2025-03-22 20:38:00 -04:00
Andrew Gallant
a93e71a5eb
changelog: 0.2.5 2025-03-22 20:37:14 -04:00
Andrew Gallant
e616bf975b jiff-tzdb: update to tzdb 2025b
Note that this adds a new time zone with
identifier `America/Coyhaique`.

Ref: https://lists.iana.org/hyperkitty/list/tz-announce@iana.org/thread/6JVHNHLB6I2WAYTQ75L6KEPEQHFXAJK3/
2025-03-22 20:35:11 -04:00
Andrew Gallant
83c068803e
fmt: fix ISO 8601 week date example
I was using `%w` to format the day of the week in examples referencing
ISO 8601 week days, but the correct directive is `%u`. The difference
is that the former uses `0` for Sunday and the latter uses `7` for
Sunday. (So in the examples given, this doesn't actually change the
output.)
2025-03-16 14:32:48 -04:00
Andrew Gallant
deee5c4229 doc: clarify the guaranteeds for Zoned::{since,until}
Specifically, that you can't get any units bigger than hours *by
default*.

We also fix what was probably a copy-and-paste error in the `Sub` trait
implementation docs for `Zoned`. (I probably copied it from
`jiff::civil::DateTime`, where it can return days by default.)

And we apply similar clarifications to the `jiff::civil::DateTime` docs
as well.

Ref https://github.com/microsoft/openvmm/pull/1028#discussion_r1994483092
2025-03-14 08:39:55 -04:00
Andrew Gallant
4404fb0f6b
0.2.4 2025-03-10 18:35:37 -04:00
Andrew Gallant
b9119a3b21 rangeint: remove PartialEq and PartialOrd impls for i{8,16,32,64,128}
Unfortunately, these impls can cause inference regressions when
non-robust code is written that assumes there is only one
Partial{Eq,Ord} impl for a particular integer type.

It would be one thing if these trait impls were external or somehow
fundamental to Jiff's design. But they only existed as a convenience. So
we remove the trait impls and take our medicine. We already had a
`Constant` wrapper type (also used for trait impls), so we just switch
all equality and inequality comparisons over to that.

I tested this with the following program:

```rust
use env_logger;

fn main() {
    let x: u64 = 1;
    let y: i128 = 0;
    assert!(y < x.into());

    let x: u32 = 1;
    let y: i64 = 0;
    assert!(y < x.into());

    let x: u16 = 1;
    let y: i32 = 0;
    assert!(y < x.into());

    let x: u8 = 1;
    let y: i16 = 0;
    assert!(y < x.into());
}
```

And this `Cargo.toml`:

```toml
[package]
publish = false
name = "jiff-inference-regression"
version = "0.1.0"
edition = "2024"

[patch.crates-io]
jiff = { path = "/home/andrew/rust/jiff/fixit" }

[dependencies]
env_logger = { version = "0.11.7", features = ["humantime"] }

[[bin]]
name = "jiff-inference-regression"
path = "main.rs"

[profile.release]
debug = true
```

I took this path because it's either this or the reporter fixes their
code. Arguably, the reporter should fix their code since it's likely
their code will break when or if some other crate adds similar trait
impls. But as I said, these trait impls are just for convenience, so
the pragmatic trade-off is to remove them and thus not be the source of
whatever problems folks hit.

[I asked the lang team about this problem][lang-zulip-question], and
they seem to agree that this is the right course of action. (And there
are ideas swirling around on how to mitigate this problem, but that's
for the future.)

Fixes #293

[lang-zulip-question]: 504689811
2025-03-10 18:35:16 -04:00
Andrew Gallant
0a0b5a0bee
jiff-static: improve README
This was still the copy from `jiff-tzdb`. Update it to be about
`jiff-static`.
2025-03-10 07:54:20 -04:00
Andrew Gallant
460a0bc039 tz: fix comment and remove superfluous repr(align(..))
When I originally wrote the comment on `Repr`, I got the alignment
wrong. But I caught that mistake and fixed it before merging anything
to `master`. I just hadn't updated the comment.

The `repr(align(..))` was leftovers from experimenting with a
`TzifDateTime` that *wasn't* packed. I was trying to get `rustc` to
optimize comparisons automatically to a single integer, but couldn't get
ti to work. So I resorted to bit-packing. Since the representation is
now just an `i64`, an explicit alignment is not needed. (And it didn't
help anyway.)
2025-03-07 16:07:01 -05:00
Andrew Gallant
fc9e02dfdf
0.2.3 2025-03-07 08:43:20 -05:00
Andrew Gallant
4dbc59f354 shared: fix implementation of IDate::yesterday
Basically, it was wrong when the date was the first day of the month.
This only impacts `jiff 0.2.2`. The bug wasn't present in previous
releases.

This was a transcription error during one of my refactors and it looks
like there unfortunately wasn't test coverage for it. Test coverage has
been added in this PR.

Fixes #290
2025-03-07 08:42:41 -05:00
Andrew Gallant
624ffc7cf8
0.2.2 2025-03-06 16:35:45 -05:00
Andrew Gallant
c7edc0a105
jiff-static: fix crate layout
I wasn't sure why `cargo package` wasn't picking up
`crates/jiff-static/shared`. So I switched to a traditional `src`
layout, which shouldn't be necessary. Indeed, that didn't fix things.
Turns out, I had a bunk `include` rule.

But I wanted to switch to the `src` scheme anyway, so leave that.
2025-03-06 16:34:30 -05:00
Andrew Gallant
08571714a9
jiff-tzdb-0.1.3 2025-03-06 16:28:38 -05:00
Andrew Gallant
300886fef3
jiff-diesel-0.1.3 2025-03-06 16:28:21 -05:00
Andrew Gallant
252a0806dc
jiff-sqlx-0.1.1 2025-03-06 16:28:08 -05:00
Andrew Gallant
fdd352efe1
changelog: 0.2.2 2025-03-06 16:25:02 -05:00
Andrew Gallant
5faa976b9c ci: try the Rust 2024 doc test perf improvement for Windows
The win-msvc job now seems to be the longest... So let's see if we can
speed it up.
2025-03-06 16:05:00 -05:00
Andrew Gallant
c789dd50b9 fmt: disable snapshot tests on core-only
For whatever reason, these seem to take a hideously long time to run in
CI. They even take a long time to run locally, *relatively* speaking. In
core-only, `insta` doesn't support snapshotting at all, which is a huge
bummer. So we just tell insta to force the tests to pass and don't do
any updating. So these tests weren't really being run anyway.

I'm not sure what insta is doing here to be honest, and I don't really
understand why insta can't handle the core-only tests. I mean, I am
still importing the standard library when tests are run, even in
core-only mode. Maybe the insta macros assume the standard library
prelude is present or something? IDK.
2025-03-06 16:05:00 -05:00
Andrew Gallant
35b0c4d422 ci: break ./scripts/test into pieces...
... so that we can run each piece in its own job in CI.

This creates an obscene number of jobs, but I'm really hoping this cuts
down on the total wall clock time.
2025-03-06 16:05:00 -05:00
Andrew Gallant
9dd4b9337d ci: bump to wasmtime 30.0.1
It's what's on my local system, so hopefully it works.
2025-03-06 16:05:00 -05:00
Andrew Gallant
fa62c4fbd7 scripts: fix test-wasm
I'm not sure why this used to work, but now it matches CI and actually
works.
2025-03-06 16:05:00 -05:00
Andrew Gallant
1471f752e5 test: move test and test-wasm into scripts
We are going to try and break `test` apart in order to speed up CI
builds. I don't want to pollute the root project directory with more
random test scripts, so let's tuck them away for now.
2025-03-06 16:05:00 -05:00
Andrew Gallant
eb61588126 zoned: improve docs for Zoned::subsec_nanosecond
It is tempting to think of this method as just being a shortcut for
`zdt.timestamp().subsec_nanosecond()`, but it actually isn't! It's
returning the fractional seconds on the *civil* datetime, not the
timestamp. These are usually the same for times after the Unix epoch,
but can differ for times before it.

While the original request in #283 asks for `subsec_millisecond()` and
`subsec_microsecond()` on `Zoned` in order to be consistent with
`Timestamp`, I'm going to pass on those for now. In particular, since
these would return the fractional second value from the *civil*
datetime, `subsec_millisecond()` would always be equivalent to
`millisecond()`. `subsec_microsecond()` wouldn't be the same as
`microsecond()` (just like `subsec_nanosecond()` isn't the same as
`nanosecond()`), but I find that this just overall adds to the confusion
of the methods here. And if you do need `subsec_microsecond()`, you can
just do `subsec_nanosecond() / 1_000`.

The reason that these additional methods make sense for `Timestamp` is
that `Timestamp` doesn't have a civil datetime. So there are no
individual `millisecond()` or `microsecond()` units. A `Timestamp` is
closer to a `SignedDuration` than a `civil::DateTime`.

Closes #283
2025-03-06 16:05:00 -05:00
Andrew Gallant
0b0a9a3088
test: OMG, remove exit 0
I have no idea how this wound up here. But this means that CI times are
actually longer than what I've been observing. *sob*
2025-03-06 10:11:16 -05:00
Andrew Gallant
77554cfb9b ci: split the "main" job into more pieces
This is the longest job by far, so I think it's worth splitting out some
of its tasks to see if we can reduce overall wall clock time on CI.
2025-03-06 10:09:11 -05:00
Andrew Gallant
9a23a442af jiff-static: update shared copy of code 2025-03-06 10:09:11 -05:00
Andrew Gallant
2bf9eb6ebc ci: check that jiff-cli generate doesn't produce diffs
This should help ensure that generated code doesn't get stale.

This is especially pertinent with the new `src/shared` module, which has
to be copied over to `crates/jiff-static/shared` any time a change is
made. Not all changes result in breakage (theoretical or otherwise), so
it's easy to forget to do.
2025-03-06 10:09:11 -05:00
Andrew Gallant
d6959a2664 changelog: mention perf improvements 2025-03-05 18:48:44 -05:00
Andrew Gallant
bd4ef644e8 bench: make some small updates to benchmarks
And also include updated benchmark results from `time 0.3.38`.
2025-03-05 18:48:44 -05:00
Andrew Gallant
98164d6263 tz: pack TZif civil datetime into i64
This leads to amazing speed-ups for TZ lookups on civil datetimes:

    tz/posix_datetime_to_offset/jiff               1.00     32.6±0.70ns        ? ?/sec    1.01     32.9±0.28ns        ? ?/sec
    tz/posix_timestamp_to_offset/jiff              1.00     21.8±0.17ns        ? ?/sec    1.04     22.6±0.15ns        ? ?/sec
    tz/tzif_bundled_datetime_to_offset/jiff        2.57     23.4±0.19ns        ? ?/sec    1.00      9.1±0.06ns        ? ?/sec
    tz/tzif_bundled_timestamp_to_offset/jiff       1.00      6.0±0.05ns        ? ?/sec    1.04      6.2±0.05ns        ? ?/sec
    tz/tzif_future_datetime_to_offset/jiff         1.35     50.5±0.60ns        ? ?/sec    1.00     37.4±0.67ns        ? ?/sec
    tz/tzif_future_timestamp_to_offset/jiff        1.00     21.2±0.15ns        ? ?/sec    1.00     21.2±0.17ns        ? ?/sec
    tz/tzif_historical_datetime_to_offset/jiff     2.68     23.4±0.17ns        ? ?/sec    1.00      8.7±0.08ns        ? ?/sec
    tz/tzif_historical_timestamp_to_offset/jiff    1.00      6.0±0.05ns        ? ?/sec    1.00      6.0±0.05ns        ? ?/sec

It turns out that comparing civil datetimes is actually quite
expensive. Getting them down to a single integer comparison makes
the binary search much quicker.
2025-03-05 18:48:44 -05:00
Andrew Gallant
f6a5cc6a22 tz: refactor TZif representation to use column storage
This makes binary search for TZ lookups substantially faster.

This is yet another brutal refactor. Changing anything in POSIX time
zones or TZif handling is now a monster pain in the ass because all
of that code is shared in a very awkward way with `jiff-static`.

Ref #271
2025-03-05 18:48:44 -05:00
Andrew Gallant
c9f72a5b56 tz: make tzif::Transition smaller
This is an easy win that uses 64-bit integers to represent a timestamp
instead of 96-bit integers. This is okay because this reflects what the
actual source IANA time zone database uses.

This makes the binary search lookup a fair a bit faster.

Next I'd like to split `Transition` into three sequences: timestamps,
civil datetimes and the local type index. This should make them as
small as possible and further improve binary search lookups (I hope).
2025-03-05 18:48:44 -05:00
Andrew Gallant
5af655e5ea tz: add new enabled-by-default tz-fat feature
When enabled, this feature will "fatten" TZif data by adding more time
zone transitions. This corresponds to what tzdb's `zic` program does
when `-b fat` is given, except Jiff does it at runtime. If the TZif data
has already been fattened, then this has no effect.

The reason for this is that it smooths out performance differences in
time zone runtime lookups between pre-fattened TZif data and "slim"
TZif data. It is unpredictable whether `/usr/share/zoneinfo` is
actually fat or not, so this helps makes performance more predictable
regardless of what the source TZif data looks like.

This uses about 25% more heap memory in my experiments. For a single
time zone, this is, in an absolute sense, likely insignificant. But if
you have thousands of time zones loaded into memory, it can add up. But
that's a somewhat niche use case. However, this can make binary sizes
bigger when the `jiff-static` proc macro is used.

So while unlikely to matter too much, the `tz-fat` feature can be
disabled if you want to prioritize memory usage and binary size.

Fixes #271
2025-03-05 18:48:44 -05:00
Andrew Gallant
092f05ff9f tz/posix: move most code to shared
This was yet another absolutely brutal refactor. But in order to
"fatten" up TZif data after parsing, we need to be able to actually use
POSIX time zones in order to compute missing transitions. And in order
to do that, basically the entire POSIX time zone implementation needs to
be in `shared`. And that means no ranged integers. Which in turn means
implementing several datetime algorithms on just primitives.

This was just overall brutal, and I am getting very close to ripping
out ranged integers.
2025-03-05 18:48:44 -05:00
Andrew Gallant
5817ed298b tz: add back POSIX time zone consistency check
This got lost in the most recently POSIX time zone refactor. We add
it back here.
2025-03-05 18:48:44 -05:00
Andrew Gallant
951a9a1567 span: fix TODO comment in error message
It looks like I never circled back around to fix the error message here
when I added the `SpanRelativeTo::days_are_24_hours()` functionality.
So fix that here.

It's hard to keep `SpanRelativeToKind`'s `Display` impl, because the
error message kinda needs to be rejiggered at a higher level.
Thankfully, this was only used in one place.
2025-03-05 18:48:44 -05:00
Andrew Gallant
fc993ca79a shared: move more date algorithms to itime
This isn't all of them, but I specifically prioritized the ones I
think I'll need for handling POSIX time zones.
2025-03-05 18:48:44 -05:00
Andrew Gallant
843adbea82 shared: define one Error type in shared
I think we're going to use this more. So we might as well put it in
one place.

And also, make it core-only compatible via degraded error messages.
2025-03-05 18:48:44 -05:00
Andrew Gallant
41cd2b8643 shared: move core datetime algorithms
Instead of just free functions operating on tuples, we actually give
them names.

Ranged integers keep pissing me off. The fact that I have "primitive"
datetime types and "ranged" datetime types is just absolutely
infuriating and creating a lot of dissonance.

But at least the new composite stuff makes moving back-and-forth a
little easier now. Of course, the composite stuff is also write-once
and read-never. *heavy sigh*

In the next commit, we're going to start moving some more of our
datetime algorithms to `shared`.

The ultimate goal here is to have enough in `shared` that we can handle
POSIX time zones.
2025-03-05 18:48:44 -05:00
Andrew Gallant
6275d26530 rangeint: add new Composite ranged type
This takes some brutalness out of writing routines for converting
to and from composite types over ranged integers (like `civil::Date`).

This whole mess is a consequence of using ranged integers and
simultaneously implementing our low level datetime algorithms on
primitive integers instead of ranged integers. It's a fucking mess.

I think I am steadily marching toward ripping out ranged integers.
Sigh. Very unfortunate.
2025-03-05 18:48:44 -05:00
Andrew Gallant
da67afe6f4 tz: remove use of String when parsing POSIX time zones
This was just really bugging me. And if we're going to move more of the
POSIX time zone implementation into `shared`, we might as well just bite
the bullet and do this too.

Now I believe the only parts of POSIX time zones that require `alloc`
are parsing the `:blah` implementation defined strings for the `TZ`
environment variable and error messages. Not that it really matters
much I think.
2025-03-05 18:48:44 -05:00
Andrew Gallant
7b724ba380 shared: move ArrayStr into shared module
So that we can use it in `jiff` and `jiff-static`.

This will avoid using a `String` at all when creating or using POSIX
time zones.
2025-03-05 18:48:44 -05:00
Andrew Gallant
cb75cb7a57 shared: move some things around
We're likely going to be moving more `crate::util` code into
`crate::shared::util` (unfortunately), so split `shared/util` apart
to make room for it.
2025-03-05 18:48:44 -05:00
Andrew Gallant
bf01f266ea tz: simplify POSIX time zone types
Now that we eagerly reject unreasonable POSIX time zones, we can
simplify our type definitions. There's no more split between
`PosixTimeZone` and `ReasonablePosixTimeZone`. Everything is
just reasonable.
2025-03-05 18:48:44 -05:00
Andrew Gallant
8c1517b308 tz: reject "unreasonable" POSIX time zones outright
This makes the POSIX time zone parser reject strings like `EST5EDT`.
That is, a time zone with daylight saving time, but without an explicit
rule stating when daylight saving time becomes active/inactive.

We were already doing this, but more explicitly by calling
`PosixTimeZone::reasonable`, so there is no public API breakage here.
The only difference is that `EST5EDT` will be treated as invalid and
will instead be attempted to be used as an IANA time zone identifier.
(Which, incidentally enough, actually exists. Odd, but I suppose
technically more correct than the current behavior of just rejecting it
outright.)

I did this because it makes the type definitions simpler. There was a
lot of cognitive energy on my part being devoted to parsing unreasonable
POSIX time zones successfully and only later asserting that they are
reasonable through a fallible API. But I don't think this was really
buying us anything, and we should just reject them outright.

Interestingly, PostgreSQL does support these "unreasonable" POSIX time
zones[1]:

> If a daylight-savings abbreviation is given but the transition
> rule field is omitted, the fallback behavior is to use the rule
> M3.2.0,M11.1.0, which corresponds to USA practice as of 2020 (that
> is, spring forward on the second Sunday of March, fall back on
> the first Sunday of November, both transitions occurring at 2AM
> prevailing time). Note that this rule does not give correct USA
> transition dates for years before 2007.

But POSIX has literally nothing to say about it[2], despite providing a
grammar that clearly makes the DST transition rule optional even when a
DST abbreviation is provided. Like it doesn't even mention that it's
unspecified, despite bloviating about how certain abbreviation lengths
lead to unspecified behavior. Why does POSIX suck so bad?

Anyway, it seems like there are really only two choices here. We could
either reject unreasonable time zones as invalid POSIX time zone
strings, or we could just "helpfully" assume a particular DST transition
rule. Jiff isn't legacy software (yet), so maybe don't try to be so
helpful that we assume one country's DST transition rules silently for
everyone in the world.

This commit does the bare minimum to reject these time zones.
The next commit will be the payoff.

[1]: https://www.postgresql.org/docs/current/datetime-posix-timezone-specs.html
[2]: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html
2025-03-05 18:48:44 -05:00