Hacker Read top | best | new | newcomments | leaders | about | bookmarklet login

I noticed a bug in another tool today, caused by the week-of-the-year value returned by strftime() returning '00' until the 6th. Possibly related?


sort by: page size:

So, people don't always pay attention to what they're reading?... strftime(3) says[0]:

"This has the same format and value as %Y, except that if the ISO week number belongs to the previous or next year, that year is used instead. (TZ (Calculated from tm_year, tm_yday, and tm_wday.)"

[0]: http://man7.org/linux/man-pages/man3/strftime.3.html


strftime has had this forever (my man page implies since C89), it's %c. of course, man pages being man pages, the recommendation to actually use it is neither at the top, nor the bottom, but hidden in the BUGS section.

date parsing is available in strptime. you have to write "%Y-%m-%d %T" (since for some reason only strftime supports %F), but that's not too bad. the major pain is handling locales in a thread-safe manner, which is unnecessarily painful in C.


> I still can't remember %m vs %M without thinking twice about it

The time component is "%H:%M:%S" — it's all capitals, and I always think "her majesty's ship" when I type it for some odd reason. At any rate, that's how I remember it.

Once I work out that time component, then the date falls into place: "%m-%d-%Y".

But yes, strftime's formatting strings aren't memorable.

(I'm also a server-side engineer, so I active try to avoid formatting dates into anything other than ISO-8601…)


Anything 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.

Dates can be weird just about anywhere.

I recently hit a bug where all data was being checked to see if it was a datetime, and python interpreted `st1` as September 1st of the current year.

https://github.com/mozilla/frost/pull/330


That could just be using a date() -type function.

Interesting. Given the documentation of strptime, I would have expected that to not be possible at all (since constructing a datetime without a year isn't possible directly)

Here is how to format an ISO week date with strftime:

“%G-W%V-%u”

This is in POSIX

https://pubs.opengroup.org/onlinepubs/9699919799.2018edition...

and C99

http://www.open-std.org/JTC1/SC22/WG14/www/docs/n1256.pdf

Python’s documentation explicitly says it only describes the C89 formatting codes and that C99 has more features. https://docs.python.org/2.7/library/datetime.html#strftime-s...


Why would you bother with that when you can just call `date.strftime("%A")`? (or `date.isoweekday()` if you want the numerical index)

I fixed the same bug today, it happens when using YYYY instead of yyyy as a dateformat. YYYY is the year of the week the day falls in.

Can't remember how to format your dates and times using strftime? Ruby, C, and PHP programmers: rejoice!

In Java it's worse, for SimpleDateFormat (the most often used date formatter in Java) YYYY (uppercase) is "week year" (strftime %G), the correct way to get the year (strftime %Y) is yyyy (lowercase). It's very easy to get it wrong; the correct way to get the full ISO timestamp is "yyyy-MM-dd'T'HH:mm:ss.SSSXXX".

Sounds like the date-stored-as-integer “bug” strikes again.

And here I thought something this blatantly stupid was just an urban myth among programmers. Huh.


I assume they are using date-fns - Saw this bug in that lib once when doing a demo and it was really embarrassing. You would expect a date processing library to somehow manage to not have bugs like this.

In addition:

> The WEEKDAY function returns incorrect values for dates before March 1, 1900. Because most users do not use dates before March 1, 1900, this problem is rare.


Here's a strftime fix for Python:

    >>> import strftime_fix
    >>> import datetime
    >>> datetime.datetime(year=2018, month=12, day=31).strftime('%G-%m-%d')
    '2018-12-31'
    >>> datetime.date(year=2018, month=12, day=31).strftime('%G-%m-%d')
    '2018-12-31'
https://github.com/Mortal/strftime-fix

In my case I had a date that I needed to parse from JSON. It was in this format: 2012-01-21. I parsed it using YYYY-MM-dd. It should have been yyyy-MM-dd for the reasons above.

It is not technically a bug but it real easy to get wrong. Perhaps they should have picked sometimg other than YYYY for week years.


Been there, a TCL web script, IIRC. This led to religious processing of input with "regsub" (this predated TCL 8). Yuck.

Also once fixed an inherited awk script that malfunctioned at the end of year: a hardcoded month name array with November missing. To this day I always populate any such lookup with a loop and strftime() or appropriate.


I wonder if this would cause problems for daily cron scripts that run tasks by checking the date against == rather than than <. Not to mention monthly scripts that happen to run on that particular lost day.

Anyway, I love the passionate discussion programmers have when it comes to dates. Remember, it's only 86400 seconds that they are losing.

next

Legal | privacy