Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!weretis.net!feeder4.news.weretis.net!feeder1.news.weretis.net!news1.tnib.de!feed.news.tnib.de!news.tnib.de!newsfeed.in-ulm.de!not-for-mail From: Christian Neukirchen Newsgroups: comp.misc Subject: Re: The 8-Byte Two-Step Date: Tue, 23 Sep 2014 18:26:59 +0200 Organization: [ posted via ] IN-Ulm Lines: 39 Sender: chris@juno Message-ID: <87lhpawbe4.fsf@gmail.com> References: <01T8OImfoeUtayhTm1xJJ0vS@dont-email.me> <87ppeq2car.fsf@elektro.pacujo.net> <87d2ap2pjn.fsf@elektro.pacujo.net> Mime-Version: 1.0 Content-Type: text/plain X-Trace: news.in-ulm.de AF9A86FC9BE6E5985520A22B97EF109C In-Reply-To: <87d2ap2pjn.fsf@elektro.pacujo.net> (Marko Rauhamaa's message of "Sun, 21 Sep 2014 14:14:52 +0300") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Xref: csiph.com comp.misc:5053 Marko Rauhamaa writes: > Bob Eager : > >> On Sun, 21 Sep 2014 00:48:44 +0300, Marko Rauhamaa wrote: >>> def gregorian_day_count(isodate): >>> year, month, day = map(int, isodate.split('-')) >>> a, b = divmod(12 * year + month + 1185, 12) >>> return int(a * 365.2425) + int(30.573 * b + 0.706) + day >> >> [...] >> >> Didn't know that one...thanks! > > Except, you shouldn't believe everything you read on the Internet :) -- > the above code had a bug which I noticed when I actually tested it. > > Here's a correction: > > def gregorian_day_count(isodate): > year, month, day = map(int, isodate.split('-')) > a, b = divmod(12 * year + month - 3, 12) > yeardays = a * 365 + a // 4 - a // 100 + a // 400 > return yeardays + int(30.573 * b + 0.706) + day > > If you intend to use the code, you're well advised to test it yourself. I found this one, which is similar in spirit but doesn't depend on floats: http://alcor.concordia.ca/~gpkatch/gdate-algorithm.html function g(y,m,d) m = (m + 9) % 12 y = y - m/10 return 365*y + y/4 - y/100 + y/400 + (m*306 + 5)/10 + ( d - 1 ) -- Christian Neukirchen http://chneukirchen.org