9 ms·
ISO-8601, YYYY, yyyy, and why your year may be wrong
- rzzzt 8y agoThe same format string for "week years" was introduced in Java 7: http://www.juandebravo.com/2015/04/10/java-yyyy-date-format/ http://www.juandebravo.com/2015/04/10/java-yyyy-date-format/
- mehrdadn 8y agoI get how the year went forward at the end of the previous year in your example, but how in the world in this post's article did it move back? I'm so confused.
- rzzzt 8y agoI think that's the date parser's behavior in Swift (if no additional information is given in the input string, revert to the first day of the week before the first week of the year). The article I linked to displays what the formatter does.
- justjash 8y agoRan into this type of thing last year. Definitely a weird one to debug by looking at things, but easy to figure out when you print out the dates or read documentation.
- imrehg 8y agoSeems like crushed the site (I get a database error)
- LeifCarrotson 8y agohttps://outline.com/xSadBb https://outline.com/xSadBb TLDR: ww-e-YYYY gives you week number, day in week, and the ISO year in which to count weeks. yyyy-MM-dd gives you the calendar year, month, and day. Using YYYY when you mean yyyy give you unexpected results. YYYY-MM-dd unexpectedly - or expectedly, depending on what you expect from a programming language and ISO spec combo - gives week zero (you didn't specify ww), day zero (again you didn't specify e), which means it gives you the first day of the last full week of the preceding year. 2019-1-1 parsed with YYYY-MM-dd will return a Date of December 23rd, 2018.
- deleted 8y ago[deleted]
- pavlov 8y agoIMO there’s no valid reason for date parsers to accept YYYY by default. There’s no sensible use case where you’d want to mix week-based dates with month-based ones, and the latter type is way more common. So why not have a separate “WeekOfYearDateFormatter” subclass for the rare use case? The default class could then explicitly fail when you’re accidentally using YYYY in a format string, saving you from a weird end-of-year bug that might spoil your holiday.
- ivan_gammel 8y agoIt would reduce value of a pattern-based API as an idea, that you can specify any format with combination of special characters. But you are right: this case is so special that it would make sense to protect majority of developers from misuse. If builder pattern is used for date formatters, then it could throw an exception unless certain flag is also set: var formatter = format(“ww-e-YYYY”) .withWoYCalculations() .build();
- naniwaduni 8y agoOne of the big benefits of using these APIs is often that you implicitly accept a greater range of user formats than you anticipated.
- Waterluvian 8y agoAre these date format strings part of ISO-8601 or standardized in some way? They look pretty much identical in JS and Python. If so, maybe a big help in preventing human error are editor plugins that verbalize what a given string represents. I found the regex websites that do this to be invaluable in learning and validating regex.
- saagarjha 8y ago> Are these date format strings part of ISO-8601 or standardized in some way? They look pretty much identical in JS and Python. They’re part of Unicode. > maybe a big help in preventing human error are editor plugins that verbalize what a given string represents This is a good idea, but it’s easy to get this wrong. There are websites that let you enter a format string and it will format the date for you as a “preview”, and they often have a list of format specifiers that you can pick from. The issue is that it’s easy to pick YYYY because it might end up coming first in the list and have a description like “the year”, which makes it seem no different than the one you’d want to use.
- em500 8y agoThey're very different from the format strings in Python, which takes format strings from the [C standard library](https://en.cppreference.com/w/c/chrono/strftime https://en.cppreference.com/w/c/chrono/strftime), which is different from (and older than) these Unicode format srrings. Same holds for other libc based languages like Perl or R. JS does not have native datetime format strings AFAIK, and 3rd party libs such as moment.js often invent their own strings.
- sam_goody 8y agoThis appears to be about the DateFormatter class in Swift. However, it states there that YY is part of the Unicode standard, so I imagine it might affect other languages as well.
- saagarjha 8y agoFoundation (where DateFormwtter is defined) does use Unicode’s standard, so this should be language agnostic.
- ubernostrum 8y agoA lot of languages take their date/time formatting from C strftime (and quite a few simply use light wrappers around actual strftime), where the format code for ISO year is %G. And FWIW, Python's (strftime-based) datetime library won't let you mix ISO and non-ISO format codes. Trying to use %G with %m, for example, raises an exception, as does trying to use %Y with %V (%V is the ISO week number format code).
- ubernostrum 8y agoAnd just to clarify a bit: the specific restriction Python imposes is that if a strptime() format string contains one of %G (ISO year) or %V (ISO week number), it must also contain the other one, and must contain a day-of-week format code (%A, %a, %u, or %w). Examples: '%G/%m' is illegal; it contains %G without %V, and does not contain a weekday format code. Attempting to call strptime() with this format raises ValueError. '%V/%u' is illegal; it contains a weekday format, but has %V without %G. Raises ValueError. '%G/%V' is illegal; it contains both %G and %V, but does not contain a weekday format code. Raises ValueError. '%G/%V/%u' is legal; it contains both %G and %V, and contains a weekday format code. '%G/%V/%w' is legal; it contains %G and %V and a weekday format code. It's a bad idea, though, because %w numbers days 0-6 starting Sunday, while ISO (%u) numbers them 1-7 starting Monday. If you need to work with ISO week date formats for some reason, you should stick to one of these two format strings: '%G-W%V-%u' or %GW%V%u The date of this comment (December 26, 2018) comes out as either '2018-W52-3' or '2018W523' using those format strings.
- kstenerud 8y agoIt probably would have been better to use entirely different letters (xxxx instead of YYYY) to reduce sources of human error. Notwithstanding these small issues, iso8601 is a godsend, but even with this spec it's amazing how many times we just get it plain wrong dealing with time. Time is hard! It gets even worse in binary formats, dealing with leap second tables, time zones, daylight savings, different epochs etc, which is why I developed the smalltime [1] format. [1] https://github.com/kstenerud/smalltime https://github.com/kstenerud/smalltime
- rodionos 8y agoIn Java `x` is reserved for time zone offset: https://axibase.com/docs/atsd/shared/time-pattern.html#pattern-letters https://axibase.com/docs/atsd/shared/time-pattern.html#patte...
- colejohnson66 8y agoI thought you’re supposed to use Z for that? What’s the difference then?
- Izkata 8y agoThere's an example column on the link, it has to do with the exact formatting of the offset.
- kevin_thibedeau 8y agoZ is just shorthand for for offset +0.
- gpvos 8y agoThe Twitter bug in December 2014 was caused by using %G instead of %Y. So that won't help much. G comes before Y alphabetically, as does almost ant other letter...
- userbinator 8y agoG comes before Y alphabetically That's irrelevant. There's a comment I made on a discussion about this a while ago: https://news.ycombinator.com/item?id=17059958 https://news.ycombinator.com/item?id=17059958 It's sad that most people seem to put the blame on everything else except the developer --- who was simply not exercising any common sense nor thinking critically. There's a very good reason to how the format specifiers were assigned, and anyone who doesn't notice the pattern (and surprising deviations from it) has no one to blame but his/herself.
- geocar 8y agoWhen designing a new API, it makes sense to consider this kind of thing: How are people going to use it, and how are people going to screw it up? Kids these days copy and paste everything so bugs tend to multiply if you (god forbid) make a popular API. Date formatter APIs are opaque and anyone trying to use one in the morning or before the 12th day of a month has to check the documentation, so what do we actually gain with this abstraction? There's a small number of formats you might need that it makes sense to try to enumerate them: * ISO8601 (and get it right, it's a comma not a dot) * that weird ISO8601 variant that uses a dot * kdb's .z.p * DJB's TAI * ISO/IEC 9899 asctime/ctime * IEEE 1003.1 "ls" format * Yankee-doodle format/other localised formats * RFC1123/RFC7231 * RFC2109 * RFC822/RFC2822 * Fancy "X units since/until" relative time Do you really need so many others that you should have "yyyy-MM-dd" anywhere in your code? Each of these are trivial to construct from a struct tm/Date object, that you'll end up with fewer bugs if you stop making up mini-languages for dates and just do it directly. Oh and your code will be faster.
- _trampeltier 8y agoAnd then is the funny idea with an unknown date of birth, when you end up with 00-00-yyyy in your passport.
- geocar 8y agoYes! That's a very good point! How is a data-formatting mini-language supposed to sensibly deal with this? RFC3339 specifically prohibits the idea of unknown dates/months...
- acdha 8y agoThere are proposed extensions — for example, the Library of Congress[1] has proposed "X" for unknown field values and ".." for when only one part of a range is known: https://www.loc.gov/standards/datetime/edtf.html https://www.loc.gov/standards/datetime/edtf.html 1. Disclaimer: my employer but I don't work in that group or on that project
- Pxtl 8y agoCase sensitivity is a mistake in every platform that embraces it. I will die on that hill.
- smilliken 8y agoUnix filesystems? Names in programming languages? English? Care to elaborate?
- Pxtl 8y agoCase-sensitivity is such a rare concept in English that any API or programming language that is based on it cannot be semantically thought of as English, even though it uses English words for all its function and class names. I've used systems that are case sensitive and systems that are case insensitive, and the former are responsible for an order of magnitude number of bugs more than the latter.
- dtech 8y agoCasE-SenSITIVITY IS rarE IN ENGLISH? I WiSh SOMEone would'VE TOLD mE EARliER!
- palunon 8y agoTell it to the lawyers writing disclaimers in uppercase.
- drdaeman 8y agoThat's an artifact of the years past, still alive because everyone is too afraid to do something about it, and most lawyers just don't care about styling. Every time I copy a license to the project, I edit the formatting to use Markdown bold (or sometimes italic/emphasis) instead of ALL THAT UPPERCASE SCREAMING. Much easier for the eyes. IANAL, but I believe that was the intent. Just to highlight the important sections to be even safer with "you have been warned about this and can't say you haven't noticed it". If any lawyer out there knows if this is correct understanding or not - would be interesting to hear.
- sneak 8y agoSeems like a good candidate for a GitHub sitewide search, with (manually filed) issues against improper uses (maybe a cut and paste) to alert callers who are using it incorrectly.
- glitchc 8y agoMaybe YYYY should be changed to IIII minimizing confusion. Related article (heavily discussed on HN in the past): http://rachelbythebay.com/w/2018/04/20/iso/ http://rachelbythebay.com/w/2018/04/20/iso/
- marcosdumay 8y agoIt would be nice if anywhere at the beginning of the post or around it the author said it was about swift, or that the blog is focused on it. I understand other languages may have similar problems, but they will certainly have very different formats.
- wool_gather 8y ago> they will certainly have very different formats Not necessarily; as the article discussed, the format characters are part of a Unicde standard. Apple/Swift Foundation uses the ICU library[0] for the heavy lifting of date formatting, which is certainly widely available (by design). [0]:http://site.icu-project.org/ http://site.icu-project.org/
- marcosdumay 8y agoThat reference seems to be offline at the moment... I don't think it's widely used. It is great that some standards body took that problem into their scope, but it may take a long while until most languages agree on anything here.
- tedunangst 8y agoYeah, what crazy obscure languages expose a binding to strftime???
- deleted 8y ago[deleted]
- saagarjha 8y ago> I don't think it's widely used. You may wish to look at this: http://site.icu-project.org/#TOC-Who-Uses-ICU- http://site.icu-project.org/#TOC-Who-Uses-ICU-
- hafthor 8y agoJava affected in exactly the same way.
- giancarlostoro 8y agoThis sounds to me like a really weird YYYY2k problem. In all seriousness, that is weird. In JavaScript you can't even format a datetime string as far as I've researched, the only way is to import some third party library, otherwise you're concatenating a bunch of function calls to piece together the string you want.
- tatersolid 8y agoPerhaps not part of ECMA standards, but browsers/node have Intl.DateTimeFormat as the standard library https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat https://developer.mozilla.org/en-US/docs/Web/JavaScript/Refe...
- giancarlostoro 8y agoI believe I've tried that approach before too, but fell short when my boss wanted a specific format that just wasn't doable with that approach. I'm still a little shocked that this isn't something part of ECMA somehow given how many more other programming languages support using time formatted strings.
- tatersolid 8y agoThe whole point of Intl.DateTimeFormat is to intentionally not give you the specific format your boss wants, because your boss is almost certainly wrong about how a date should be formatted in many cases. You tell the API you want the date and the hours and minutes, but not seconds. It localizes the result correctly for the vistor’s preferred culture (language-country). If you show someone from England “4/5/2019”, they will think it represents a date 31 days after someone from the USA given the same string.
- estebank 8y agoThis is one of those bugs that scare me the most, because they are literally a time bomb. Twitter was affected by this a few years back https://www.google.com/amp/s/amp.theguardian.com/technology/2014/dec/29/twitter-2015-date-bug https://www.google.com/amp/s/amp.theguardian.com/technology/...
- macintux 8y agoAgreed. Calendar/time-related bugs are brutally hard to test for, one reason I was so pessimistic about Y2K.
- scottlamb 8y agoHow hard would it be to make a liner for the common cases if this? I'd expect most format strings to be literals (no concatenation or anything), passed to the formatting API in a way that can be easily statically determined. Any use of YYYY without we should be assumed to be a mistake. It can also be detected at runtime. Debug builds could warn it crash. Future APIs just shouldn't have YYYY mean this. Use another letter.
- scottlamb 8y agoBad autocorrect / typos: * s/liner/linter/ * s/we/ww/ * s/warn it crash/warn or crash/ Sorry; I typed this comment on my phone in a hurry, then didn't look at it again until I could no longer edit it.
- tnorthcutt 8y agoA little amusing that PHP, everyone’s favorite whipping language, eliminates this particular footgun (it uses ‘o’ for the week-based year).
- rachelbythebay 8y agoI’ve seen it done. Had to patch a few, too.
- saagarjha 8y ago‘o’ comes first alphabetically, so I can see how this can still be an issue.
- pjungwir 8y agoIn Postgres `extract(week from t)` has a similar danger. If you combine it with `extract(year from t)` then you land in the wrong place. You need to combine it with `extract(isoyear from t)` instead. That bug is sort of the opposite of this one: instead of parsing a date and using the ISO year by mistake, it's formatting a date and omitting the ISO year by mistake.
- naniwaduni 8y agoThis seems substantially less likely to be a problem since (a) G-V-u is relatively rare compared to Y-m-d (b) The choice-of-year problem is inherent to week-based dates, but not to month-based dates, since years are not week-aligned, so knowing the difference is table stakes for implementors of week-based timestamps Now, "isoyear" seems like an awful name in various ways, not least of which is people seeing "iso" and assuming that this is what they really want (i.e. the same problem all over again)...
- hafthor 8y agoJava: new java.text.SimpleDateFormat("YYYY-MM-dd").parse("2018-12-30") returns Sun Dec 31 2017 new java.text.SimpleDateFormat("YYYY-MM-dd").format(new java.util.Date("12/30/2018")) returns 2019-12-30 java.time from Java8 also affected. java.time.format.DateTimeFormatter.ofPattern("YYYY-MM-dd").format(java.time.LocalDate.parse("2018-12-30")) returns 2019-12-30
- adamvoncorswant 8y agoIsn't it fun that while we're all discussing 'yyyy' vs 'YYYY', it sees like we're missing out that what everyone actually should use most of the time is rather 'uuuu'?
- lstamour 8y agouuuu appears to be Java-specific: https://stackoverflow.com/questions/41177442/uuuu-versus-yyyy-in-datetimeformatter-formatting-pattern-codes-in-java https://stackoverflow.com/questions/41177442/uuuu-versus-yyy...
- adamvoncorswant 8y agoYes, indeed so. That's why I answered in the java thread. Sorry for not making that clear.
- bassman9000 8y agoJavadoc uses yyyy for year since at least 1.5 https://docs.oracle.com/javase/1.5.0/docs/api/java/text/SimpleDateFormat.html https://docs.oracle.com/javase/1.5.0/docs/api/java/text/Simp... I've never seen YYYY used in Java.
- dmitrygr 8y agoAs always, the 2-part "Falsehoods programmers believe about time" is a good read: https://infiniteundo.com/post/25326999628/falsehoods-programmers-believe-about-time https://infiniteundo.com/post/25326999628/falsehoods-program... https://infiniteundo.com/post/25509354022/more-falsehoods-programmers-believe-about-time https://infiniteundo.com/post/25509354022/more-falsehoods-pr...
- monochromatic 8y agoSome additional stories I enjoyed about this “feature” (which I’d never heard of): https://rachelbythebay.com/w/2018/04/20/iso/ https://rachelbythebay.com/w/2018/04/20/iso/ https://rachelbythebay.com/w/2018/05/13/dates/ https://rachelbythebay.com/w/2018/05/13/dates/
- robrichard 8y agoFormatting dates with a format string is an anti-pattern. Format strings are too easy to get wrong and don't scale when you need to account for different locales. JavaScript's Intl functions do this correctly by providing an api that accepts a locale and options like weekday: narrow|short|long, year: numeric|2-digit, etc.
- jimmychangas 8y agoSoftware i18n is still hard to do, even in 2018. Not completely related to the post, but sometime ago I had to build a calendar widget, in javascript, capable of displaying ISO and Hijri dates simultaneously. It turns out the most reliable way to convert between the two is to use a lookup table, similar to what Java Time does. Algorithmic implementations available started to drift in odd ways after a certain number of days.
- emmelaich 8y agoGolang's time formatting seemed odd. Now it looks like genius.
- Svip 8y agoUsing 'e' in the 'ww-e-YYYY' example is pretty bad form, as it is dependant on your local system. So on a US system, 1 in the 'e' field means Sunday, but in say a German system, it would mean Monday. Although, I cannot figure out which one would be system independent.
- djhworld 8y agoGot bitten by this one, I can't remember the specific details other than the bug was due to using YYYY in a system that parsed log lines. The bug manifested itself around this time of the year, so not a great time to be called out to find billions of log lines are being rejected by the system!
- Groxx 8y agoMost people, for their entire career, should literally never write out a date/time-format string. Even once. If you want ISO8601, use that constant, don't write it out. Use pre-defined formats, like 8601, 3339, Long, Short, etc. Or datetime skeletons if your system supports it and you MUST do something non-standard, and even then do a day or three of research before typing the first letter. Basically nothing else is even remotely acceptable for internationalization (and whatever the "intercalendarization" equivalent would be) and stands a good chance to get even the extreme basics like "yyyy" wrong.
- paulryanrogers 8y agoIIRC, ISO8601 isn't a single constant, fixed format. It's a variety of optional representations that generally follow the rule least-to-most specific. For example 19991231 would be as valid as 1999-12-31.
- Groxx 8y agoYeah, good point on 8601. But that's even more of a reason to use a pre-defined 8601-parser/printer/whatever instead of a hand-written format string. If you're using 8601 for an interchange format... don't. But if you're forced to emit a hard-coded format for whatever consumer can't be forced to do things sanely, this would be one of those exceptions to "most people". And it should immediately stop after that exception.
- bmn__ 8y agohttps://tools.ietf.org/html/rfc3339 https://tools.ietf.org/html/rfc3339 exists as a profile of ISO 8601 for precisely that reason, its representations are much more restricted.
- firethief 8y agoThis website degrades unusually poorly with 3rd-party JS disabled. It looks fine, and I was really confused until I realized the main content was in images that had been omitted without any kind of placeholders.