Live data from Hacker News

Do not use 'week year': YYYY

github.com

51–60 of 94 posts

Re: Do not use 'week year': YYYY

#51
post #18

"week year" was the cause of a big Twitter outage during the time I worked there. In the immediate aftermath there was a "see? this is why we need a monorepo" exhortation from leadership.

I feel like I'm missing a few dots to connect that problem with the proposed solution...

Fix in one place, and it's fixed everywhere. Allegedly.

Re: Do not use 'week year': YYYY

#52
post #30

Earlier quoted context omitted.

> Is it just me for thinking that using format strings instead of some strongly typed interface with verbose names for this is not great? Yes, but it would not have helped with the bug in question. The parallel bug for a strongly typed interface would be "year()" returning this monstrosity, while "iso_year()" or some other poorly named variant returning the expected year. No API is immune to footguns and bad design d…

I forgot how bad JS's date API was until I tried getting the year out of a date. var date = new Date() date.getYear() // 122 I plugged this into another object and couldn't understand why it was setting the date to 0122. Like, what significance does 122 years from 1900 even have? Turns out I had to use get Full Year, which is of course perfectly intuitive.

Back then it was quite common to only care about two-digit years, and by implication only years from the 20th century.

This kind of feature[1] lead to many potential Y2K bugs.

[1] Which it shares with Perl and probably many other languages.

Re: Do not use 'week year': YYYY

#53
post #30

Earlier quoted context omitted.

> Is it just me for thinking that using format strings instead of some strongly typed interface with verbose names for this is not great? Yes, but it would not have helped with the bug in question. The parallel bug for a strongly typed interface would be "year()" returning this monstrosity, while "iso_year()" or some other poorly named variant returning the expected year. No API is immune to footguns and bad design d…

I forgot how bad JS's date API was until I tried getting the year out of a date. var date = new Date() date.getYear() // 122 I plugged this into another object and couldn't understand why it was setting the date to 0122. Like, what significance does 122 years from 1900 even have? Turns out I had to use get Full Year, which is of course perfectly intuitive.

I used the JS Date API for the first time a few months ago, and wrote something like

    date.getYear() + "-" + date.getMonth() + "-" + date.getDay()
in the hope of getting something like "2022-1-4".

The actual result for that date is "122-0-2" - not a single component of the date was what I expected.

Re: Do not use 'week year': YYYY

#54
Since week year is so rarely used and since SimpleDateFormat() can't be changed perhaps the compiler could give a warning if it sees week year hardcoded in a call to SimpleDateFormat(). If its really wanted an annotation could turn off the warning.

Re: Do not use 'week year': YYYY

#55
post #17

I understand that "week year" is basically a payroll creation, but I am a bit concerned about the last few days of 2021 were logged as 2022, and Jan 1st 2022 was even logged as 2023. The last few days of 2021 logged as 2022 makes sense for payroll, but how the heck is Jan 1st 2022 put in the 2023 year?

> but how the heck is Jan 1st 2022 put in the 2023 year? This only happened shortly after midnight on January 1st. So I guess this is somehow related to different time zones/offsets.

Yes! I just looked into it and the log itself was still 2022:

2022-12-31 23:16:54.013

And then our UI converted this entry into one with 2023

(it correctly assumes that the string is in UTC and then converted it to local time +1h)

Re: Do not use 'week year': YYYY

#56

Earlier quoted context omitted.

There is no date formatting in JavaScript's standard library. External libraries tend to just fix this kind of thing with a breaking change.

There is – here’s the spec: https://tc39.es/ecma402/#datetimeformat-objects

Ah yes. I can't use that yet due to lack of browser support so I forgot it existed.

Re: Do not use 'week year': YYYY

#57
post #36

Is it just me for thinking that using format strings instead of some strongly typed interface with verbose names for this is not great? I would take `format(year(), '/', month(), '/', day())` over ad-hoc format strings by various APIs. Reading the docs further this also stands out: > For parsing with the abbreviated year pattern ("y" or "yy"), SimpleDateFormat must interpret the abbreviated year relative to some cent…

Format strings are intended to be configurable (possibly per user-interface language) and not necessarily hardcoded. A strongly-typed builder API might be useful, but if you need both programmatic specification and external configuration, format strings can fulfill both purposes, whereas only having a builder API doesn’t.

That's perhaps true, but there is no reason why we can't have both. There are many hard-coded format strings in practice.

Re: Do not use 'week year': YYYY

#58
This sort of thing gets developers thinking about how they could improve on the way dates and time are handled by various programming languages. Don't do it! I find it best to just change jobs whenever encountering any sort of task that requires dealing with dates, time, or worse - time zones.

Re: Do not use 'week year': YYYY

#59
post #17

Earlier quoted context omitted.

> but how the heck is Jan 1st 2022 put in the 2023 year? This only happened shortly after midnight on January 1st. So I guess this is somehow related to different time zones/offsets.

Yes! I just looked into it and the log itself was still 2022: 2022-12-31 23:16:54.013 And then our UI converted this entry into one with 2023 (it correctly assumes that the string is in UTC and then converted it to local time +1h)

Ok, you had me a bit worried on that one.

A linter should probably flag any format that mixes "week year" with month and days. It's really for use by itself or with payroll week number.

Re: Do not use 'week year': YYYY

#60
post #36

Earlier quoted context omitted.

Format strings are intended to be configurable (possibly per user-interface language) and not necessarily hardcoded. A strongly-typed builder API might be useful, but if you need both programmatic specification and external configuration, format strings can fulfill both purposes, whereas only having a builder API doesn’t.

That's perhaps true, but there is no reason why we can't have both. There are many hard-coded format strings in practice.

Right, my intent was to explain that format-string APIs are there for good reasons and not an arbitrary choice.
Post reply on HN