Live data from Hacker News

Never write your own date parsing library

zachleat.com

271–280 of 333 posts

Re: Never write your own date parsing library

#271

When ever i see "never implement your own...", i know i want to implement it myself. People say that about hard things, and I only want to do hard things. Nobody wants people who can do easy things, people want people who can do hard things. The only way to learn how to do hard things, is to do hard things, so do the hardest things. So go ahead, write your own date library, your own Unicode font rendering, compiler,…

Solve new hard things instead of solved hard things.

Most solutions are garbage though, because they evolved accidentally.

Re: Never write your own date parsing library

#272

Earlier quoted context omitted.

A much more precious clock is used by USA to guide nuclear missiles without GPS. (nucleus of Thorium 229 controlled by a high-precision UV laser?)

That clock hasn't actually been built yet and it wouldn't be useful for guiding nuclear missiles. https://en.wikipedia.org/wiki/Nuclear_clock

I mean, than nucleus is much heavier and much smaller than electron, so it will be much less affected by external forces. We may see no difference between sea level and space based Thorium-229 clocks, or difference will be much smaller.

Re: Never write your own date parsing library

#273
> ... billed as “an extension to the ISO 8601 / RFC 3339” formats

The quote comes from MDN[1], but is wrong on so many levels:

1. RFC 3339 is not a format; it is a meta-format. It specifies properties that any timestring used in internet applications should have. One application could require that "T" be used to separate the date and the time, while another could require a space to be used, and both applications would conform to 3339. Using the same parser to parse times and dates from the two applications would be incorrect, as "1234-12-12 12:34 ..." unambiguously represents the date of December 12, 1234 in the former but the time of 12:34 on the same day in the latter.

2. RFC 3339 is thus not a subset of ISO-8601; there exists a subset of ISO-8601 which satisfies RFC 3339, but there also exist many timestrings that are not subsets of ISO-8601 which also satisfy it (most famously replacing "T" with a space, but that is merely an example for which RFC 3339 conforming applications may deviate from the ABNF in the RFC for readability).

3. It is obvious from both #2 and the table in TFA that RFC 9557 can't be an extension of ISO-8601 given that there are valid ISO-8601 timestrings in the table that are invalid RFC 9557 timestrings.

4. This is a minor nitpick, bu RFC 9557 also alters the semantics of some RFC 3339 timestrings with regards to offsets. RFC 3339 specified "Z" (or "z") for the offset being the same as +00:00, while 9557 has "Z" being the same as -00:00; the meat of RFC 9557 is adding suffix-tags to RFC 3339, so I wouldn't quibble with calling 9557 an extension of 3339.

1: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...

Re: Never write your own date parsing library

#274

Earlier quoted context omitted.

> That's a localized date and time. Note that I'm not arguing against storing the numeric part in a consistent format, of course you should. My point is that right mow in 2025 you don't know the timezone. E.g. if you write that as the zones are now, it would be "2029-07-26T11:00:00.000Z", but if the timezones change, then when a person is looking at their calendar in 2029, they will be an hour early or late to the me…

I agree with the gist of your comment but I think you’re using the wrong words. A time zone, as the name implies, is a zone, a place. The most common way to indicate a timezone is a continent+city combo that is both politically and geographically close, so it’s unlikely to change. Therefore, I very much know the timezone of Frankfurt, both today and in the reasonably close future, namely “Europe/Berlin”. You’re talki…

Well, not really. :) Timezones are the whole area that has the same time. So "Europe/Berlin" and "Europe/Rome" right now can be used in user-facing help texts to make the timezone understandable, but those cities might not be in the help text in the future. That's because:

"Each time zone is defined by a standard offset from Coordinated Universal Time (UTC)." (https://en.m.wikipedia.org/wiki/Time_zone)

So the offset is the timezone.

That's why I stand with my original point: if you want to support "clock on the wall" future dates, a geolocation is needed. And given that Germany was two countries in the recent past and Frankfurt and Berlin were in different countries, I wouldn't solve this by picking a city from the help text that's currently closest to the timezone, but instead use the actual geolocation of the event.

P.s. All this is, of course, for 99.99% of the cases a complete overkill, and as said, I haven't had issues in using unix epoch for all dates. But just wanted to say that timestamps aren't as simple as I felt was suggested in the first comment.

Re: Never write your own date parsing library

#275
post #121

This article doesn't get into some of the special fun of ISO 8601, including relative values, non-Gregorian values, durations... Some of the things in the standard are surprising, like maybe were a special request. At the time, I commented, something like, Somewhere, in the French countryside, there is a person who runs an old family vineyard, that is still stamping their barrels with the timepoint information [...].…

I'd be interested in seeing the Scheme code if it's open source!

Re: Never write your own date parsing library

#276
post #240

> Consider "200". Is this the year 200? Is this the 200th day of the current year? Surprise, in ISO 8601 it’s neither — it’s a decade, spanning from the year 2000 to the year 2010. And "20" is the century from the year 2000 to the year 2100. there is so much wrong with this paragraph, it's a nest of people who shouldn't work on date parsing. there is no way 200 is any kind of date, but if you're going to insist it is…

> 2000 to 2010 is 11 years This is obviously wrong by induction. If 2000 to 2010 is 11 years, then: 2000 to 2009 would be length 10 years ... 2000 to 2001 would have length 2 years and finally 2000 to 2000 would be a span lasting "1 year". But any span with the same start and end point must have length zero, it's nonsensical to have a system without that property. As for the spec, ISO 8601 defines a decade as a perio…

>any span with the same start and end point must have length zero

"a year" is not a point, it's a span. so is a decade.

>Decade 1 is year(s) 10 through 19

but the property you are trying to describe goes hand in hand with 0-based indexing, and thus we see that ISO Decade 0 is >sad tuba< 9 years long

Re: Never write your own date parsing library

#277
post #71

Earlier quoted context omitted.

A second is still originally defined as 1/86400 of an Earth day. That doesn't make it unusable as a cross galactic time unit, and I think the same goes for years and hours.

The issue is that planetary locales will each have their own days and years (and possibly hours), so it would be confusing to adopt that same nomenclature for an interplanetary/interstellar time unit. And since the latter will be inconsistent with local time systems anyway, it’s easier to just have it use powers of ten. At least until we meet aliens that may prefer a different base.

But this makes no sense, humans can't just change their circadian rhythm to match an arbitrary daylight cycle, and clocks aren't necessarily reconfigurable. And with a good enough artificial lighting you don't need to depend on star. Daylight is just weather, it has nothing to do with how calendar works.

Re: Never write your own date parsing library

#278

Earlier quoted context omitted.

I agree with the gist of your comment but I think you’re using the wrong words. A time zone, as the name implies, is a zone, a place. The most common way to indicate a timezone is a continent+city combo that is both politically and geographically close, so it’s unlikely to change. Therefore, I very much know the timezone of Frankfurt, both today and in the reasonably close future, namely “Europe/Berlin”. You’re talki…

Well, not really. :) Timezones are the whole area that has the same time. So "Europe/Berlin" and "Europe/Rome" right now can be used in user-facing help texts to make the timezone understandable, but those cities might not be in the help text in the future. That's because: "Each time zone is defined by a standard offset from Coordinated Universal Time (UTC)." ( https://en.m.wikipedia.org/wiki/Time_zone ) So the offse…

The way I understand it, Continent/City names aren't for pretty dropdowns or help texts (in fact, I'd wager they're kinda meh for that). They're the least bad way people have come up with to store and communicate timezones with future local dates, that doesn't require online geolocation. You're right that geolocation is the best. But offsets aren't a close second, like I feel your comments suggest - they're just a bad idea. To suggest that a timezone "is" an offset, while technically correct if you disregard future changes, is actively harmful to helping people write code with fewer timezone bugs.

Use IANA Continent/City names. They're great, all proper datetime libs support them, your OS supports them, they're reasonably human readable and completely machine readable. They're better for storage than offsets (or terrible future-incompatible terms like CET) in every way.

Really the entire timezone debacle can IMO be summarized as:

- for datetimes in the past, store UTC. convert from/to local in the UI; just assume the system/browser locale is the one unless you know otherwise.

- for global datetimes in the future, eg comet passings and the likes, same thing.

- for local datetimes in the future, at some "wall clock time in a place", store localtime + an IANA Continent/City combo, and keep your servers' tzdata files reasonably updated.

It never makes sense to store or communicate offsets. It hardly ever makes sense to do geolocation unless you think you think the chance that Berlin moves to a different country than Frankfurt is bigger than the chance that you lose access to up-to-date geolocation information.

Note that the above implies that the option for ISO date strings to include offsets (or words like "CET") is, IMO, nearly always the wrong choice. For past dates (ie 99.9% of dates stored by computer systems) just use UTC, and for future dates, IANA timezone names are strictly better.

Re: Never write your own date parsing library

#279
post #136

Earlier quoted context omitted.

I would not be surprised if the state of unit tests on good date parsing libraries are not sufficient to design a new one from scratch. See the number of unit tests in the Linux kernel, for example.

You might be right, I haven't checked. It just seems on the face of it such an easy thing to test. Scalars go in, scalars come out. (This could just be me doing the Dunning-Kruger thing). You could run a fuzzer against two libraries at the same time to find discrepancies....... hmm. That might actually be a good exercise.

[deleted]

Re: Never write your own date parsing library

#280
post #180

Earlier quoted context omitted.

I think that advice makes sense in the context of cryptography, where the consequences for getting it wrong can be quite serious indeed. I don't think it holds true for something as unimportant as a date parsing library.

Correct date handling (including parsing) can be monumentally important. Imagine an app that reminds people when to take their medications, for example

[dead]
Post reply on HN