7 ms·
If you're using YYYY in your JVM service or %G in anything, fix it now
The single character G instead of Y took down Twitter's API today.
If you're using YYYY in your JVM service or %G in anything, fix it now. You're very likely using the wrong year format.
date
Mon Dec 29 00:44:45 EST 2014
date -u "+%G"
2015
ISO 8601 week numbering has 2015 start this week.
- gred 12y agoWe ran into this bug in production today (I don't work at Twitter).
- warrenm 12y agosource for this statement?
- spatulon 12y agohttps://twitter.com/jmhodges/status/549430032616017921 https://twitter.com/jmhodges/status/549430032616017921 Also https://twitter.com/jmhodges/status/549431554879938560 https://twitter.com/jmhodges/status/549431554879938560 : "(The correct formats are "yyyy" and "%Y". Yes, this matters and is happening right now. "Week-based years" are a thing.)"
- warrenm 12y agothanks :)
- mrweasel 12y agoNot much of a source really, it's just some random dudes tweet, with no real explanation. Also it assume (correctly in most case) that %G or YYYY isn't what you where looking for. If you're going to throw something like this out on the internet, I would like it to be more detailed. Just blasting it out on Twitter is pretty useless. Reproduction: `date -u "+%G"`, sorry but that does EXACTLY what it's suppose to: [http://pubs.opengroup.org/onlinepubs/009695399/functions/strftime.html http://pubs.opengroup.org/onlinepubs/009695399/functions/str...]. Depending on your operating system it's even documented. OpenBSD has a pretty good documentation for it, while Mac OS X is somewhat lacking, but to the point where you wouldn't use it without checking what it actually means.[http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man3/strftime.3?query=strftime&sec=3 http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man3/...] Why would you even pick %G if that wasn't what you meant, %Y seems more obvious. Using YYYY I can understand, it's a bit of gotcha that YYYY is one thing and yyyy is something less. It even test correctly more often than not.
- dmourati 12y ago* Random dude who worked at twitter and cited the people currently working at twitter who found and fixed the problem during the time he tweeted it.
- valevk 12y agoSo, how do I fix it?
- pkinsky 12y agoSimpleDateFormat("YYYY-MM-dd'Z'") -> SimpleDateFormat("yyyy-MM-dd'Z'")
- valevk 12y agoThanks. This seems like a no brainer when you check the Javadoc [0], but I wouldn't have spent a minute giving it a deeper thought when writing it down. [0] http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDateFormat.html http://docs.oracle.com/javase/7/docs/api/java/text/SimpleDat...
- quicksilver03 12y agoJust to reformat your commands: date Mon Dec 29 00:44:45 EST 2014 date -u "+%G" 2015
- reverend_gonzo 12y agoHere's some more information: The year number of the ISO week very often differs from the Gregorian year number for dates close to 1 January. For example, 29 December 2014 is ISO 2015-W1-1, i.e., it is in year 2015 instead of 2014. http://en.wikipedia.org/wiki/ISO_week_date http://en.wikipedia.org/wiki/ISO_week_date Specifically, you should be using +%Y instead of +%G when passed into date, and 'yyyy' instead of 'YYYY' when used in Java.
- pyre 12y agoI don't think that %F is what you're looking for: ➜ date +%F 2014-12-29 ➜ date +%G 2015 ➜ date +%Y 2014
- reverend_gonzo 12y agoRight, fixed. Thanks.
- danielweber 12y agoI always wondered "who in the world uses that stuff?" when I read date library docs. Now I know. I also remember at one startup when I asked the payroll guy how many pay periods there were that year, he scoffed "26, it's always 26." Well, not with years having fractionally more than 26 ^H^H^H 52 weeks there aren't.
- nirvdrum 12y agoEvery year has more than 26 weeks ;-) But to your point, I suspect that's why a lot of companies have switched to semi-monthly payroll. It's always 24 pay periods but the pay date itself can shift.
- danielweber 12y agoEnglish is weird. I meant "it's not true that there are 26 pay periods every year, because years have more than 52 weeks, which means there must be a 'leap pay-period'" every once in a while." But your interpretation was entirely valid based on the way I typed mine.
- sirwolfgang 12y agoRelevent: http://xkcd.com/1179/ http://xkcd.com/1179/
- claar 12y agoUnix date command: Use +%Y instead of +%G Java: Use yyyy instead of YYYY Python: Use %Y (ISO-8601 year number is not available) PHP: Use Y instead of o Please contribute other languages in replies and I will add them. (edit: This post had originally suggested the post's title be changed to mention Java, thus vitno's reply below)
- vitno 12y agoalthough this post references Java, this doesn't just affect java. This affects anything that uses ISO 8601.
- hobbified 12y agoAnything at all with strftime: Use %Y instead of %G. It's a C function, but many languages (Perl/Python/Ruby/etc.) expose it through their own datetime libraries.
- bbcbasic 12y ago.NET: Use yyyy (ISO-8601 year number is not available)
- pyre 12y agoFrom strftime(3): %G is replaced by a year as a decimal number with century. This year is the one that contains the greater part of the week ( Monday as the first day of the week).
- s_kilk 12y agoI'm puzzled as to why the behaviour of %G would be desirable. Can anyone offer an example?
- danielweber 12y agoSomeone else posted: http://en.wikipedia.org/wiki/ISO_week_date http://en.wikipedia.org/wiki/ISO_week_date
- valevk 12y agoIt is used to get the year, which a week number belongs to. The current week number (1) belongs to 2015, even though it's 2014. Here, check out this calendar [0]. We are already in week 1 of year 2015. That's why you get the year 2015. Why is it useful you might ask: when you order a time-series by week number, or if you are grouping by week numbers, you would need the week number of course. But if the data is spanning over two years, you would need also the year for grouping, because you would add up weeks from different years. If you use week number + current year you get errors around the end of some years. [0] http://www.calendar-365.com/2015-calendar.html http://www.calendar-365.com/2015-calendar.html
- gpvos 12y agoNote that these are international (ISO) week numbers. US-American week numbers are different, I'm told.
- mrweasel 12y agoI image that it would be handy in some forms of accounting software. Some countries are very found of using week numbers, pretty much any government employee in Denmark uses week numbers, rather than actual dates, so you end up needing to known which year the current week belongs to, in this case 2015.
- ddlatham 12y agoUnless you're using Joda Time, where YYYY means "year of era" and will be the same as yyyy for years since 1 CE. http://joda-time.sourceforge.net/apidocs/org/joda/time/format/DateTimeFormat.html http://joda-time.sourceforge.net/apidocs/org/joda/time/forma... http://joda-time.sourceforge.net/field.html#yearOfEra http://joda-time.sourceforge.net/field.html#yearOfEra
- skeltoac 12y agoSearched my github repo for "YYYY", found two. OH NO! Wait, everything's working. Huh? Oh, Joda Time. Sweet.
- fdsfsdaffdsa 12y agoyoda time seems not to be affected: * Y year of era (>=0) year 1996 * x weekyear year 1996 * y year year 1996
- ddlatham 12y agoIf you mean Joda time, then "xxxx" is also affected (which is correct behavior): public static void main(String[] args) { long now = System.currentTimeMillis(); System.out.println("YYYY: " + DateTimeFormat.forPattern("YYYY").print(now)); System.out.println("yyyy: " + DateTimeFormat.forPattern("yyyy").print(now)); System.out.println("xxxx: " + DateTimeFormat.forPattern("xxxx").print(now)); } prints YYYY: 2014 yyyy: 2014 xxxx: 2015
- Dylan16807 12y agoNobody will use xxxx by accident, so it's not 'affected' by any problem/confusion, it's just there doing what the developer intended.
- abrodersen 12y agoClearly, Yoda has 900 years of experience dealing with datetime bugs.
- smackfu 12y agoY and y are only different for BC dates in the Gregorian calendar, so using the incorrect one generally isn't going to cause problems.
- K0nserv 12y agoThis affects Objective-C too as we discovered when a test "randomly" started failing today. Luckily I procrastinated some on HN and found this post. https://gist.github.com/k0nserv/f53084ebb6a753c4905f https://gist.github.com/k0nserv/f53084ebb6a753c4905f
- protomyth 12y agoUnless of course you read the docs and are using YYYY or %G for a proper use. Gave me a bit of a scare with that headline given one of our scripts uses %G for reporting purposes. I thought there was an error in %G for this year.
- raverbashing 12y agoAnd what's an example of proper use of this? (According to man on Mac OSX for G - "This year is the one that contains the greater part of the week (Monday as the first day of the week)"
- jakobegger 12y agoI like to look at weekly sales numbers, so in my SQL query I just group by iso week and iso year.
- mason55 12y agoSay you're doing weekly data processing that always needs to be Sun - Sat. You'd want yesterday to be the start of week 1 for 2015. Or any sort of data slicing and dicing that allows viewing by week, you'd want everything from Sun - Weds of this week to roll up to week 1 of 2015.
- danielweber 12y ago> Say you're doing weekly data processing that always needs to be Sun - Sat Shouldn't that be Mon-Sun? The Sun-Sat week we are currently in is majority 2014, but the Mon-Sun week that started today is majority 2015.
- Sharlin 12y agoYes; ISO weeks are always Mon-Sun.
- protomyth 12y agoThe OpenBSD manual under strftime (referenced in the date man page) shows "%G is replaced by the ISO 8601 year with century as a decimal number." Which leads to http://en.wikipedia.org/wiki/ISO_8601 http://en.wikipedia.org/wiki/ISO_8601 and http://en.wikipedia.org/wiki/ISO_week_date http://en.wikipedia.org/wiki/ISO_week_date which says "The ISO 8601 definition for week 01 is the week with the year's first Thursday in it." Our example is a couple of activity reports we send to the government where we need to have full weeks and they want calendar years and not budget years[1]. I seem to remember payroll for some uses the same scheme (yep, we pay weekly). Plus some internal reporting the the ISO date makes nice since we have a 2 week break which includes Xmas and New Years. 1) These are a bit of odd ducks actually. Most government reporting we do is based on Federal Fiscal years which start Oct 1. I remember in the 90's one foxbase function that did a similar algorithm except centered on Oct 1 and defined the first day of the week as Sunday.
- jontas 12y agoThis also seems to affect PHP when using the formatting character 'o', which is PHP's version of the ISO-8601 year number. Using 'Y' instead is returning 2014. php > echo date('Y'); 2014 php > echo date('o'); 2015
- teleclimber 12y agoYup. Got bitten by that last year. When reading the docs [1] it's pretty obvious which one should be used so I am left wondering why I ended up with 'o' in my code. I suspect I copy-pasted something from some random blog post or whatever. Serves me right. :( [1] http://php.net/manual/en/function.date.php http://php.net/manual/en/function.date.php
- NelsonMinar 12y agoThis same bug is probably what broke iPhones coming out of Do Not Disturb mode in January 2013. http://arstechnica.com/apple/2013/01/ask-ars-why-will-apples-do-not-disturb-bug-fix-itself-next-week/ http://arstechnica.com/apple/2013/01/ask-ars-why-will-apples...
- fredgrott 12y agointeresting little bug...data formats are always fun for interesting bugs like this
- starwarsrebles 12y agoumm that is weird all I did was put in g for googleand it brought up this website saying that there was hacking going on I did not want all of this
- starwarsrebles 12y agoI know right
- artenix 12y agoAny confirmation if that was the error which took Twitter down yesterday?
- Tharkun 12y agoThank you, kind stranger, you just saved my job.