Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to use a strftime-like function for dates before 1900 in Python?

I didn't realize this, but apparently Python's strftime function doesn't support dates before 1900:

>>> from datetime import datetime
>>> d = datetime(1899, 1, 1)
>>> d.strftime('%Y-%m-%d')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: year=1899 is before 1900; the datetime strftime() methods require year >= 1900

I'm sure I could hack together something myself to do this, but I figure the strftime function is there for a reason (and there also is a reason why it can't support pre-1900 dates). I need to be able to support dates before 1900. I'd just use str, but there's too much variation. In other words, it may or may not have microseconds or it may or may not have a timezone. Is there any solution to this?

If it makes a difference, I'm doing this so that I can write the data to a text file and load it into a database using Oracle SQL*Loader.

I essentially ended up doing Alex Martelli's answer. Here's a more complete implementation:

>>> from datetime import datetime
>>> d = datetime.now()
>>> d = d.replace(microsecond=0, tzinfo=None)
>>> str(d)
'2009-10-29 11:27:27'

The only difference is that str(d) is equivalent to d.isoformat(' ').

like image 935
Jason Baker Avatar asked Oct 29 '09 14:10

Jason Baker


People also ask

What can I use instead of Strftime?

gmstrftime function is similar to strftime , except that it returns the time in Greenwich Mean Time (GMT).

How do I change one date format in Python?

Use datetime. strftime(format) to convert a datetime object into a string as per the corresponding format . The format codes are standard directives for mentioning in which format you want to represent datetime. For example, the %d-%m-%Y %H:%M:%S codes convert date to dd-mm-yyyy hh:mm:ss format.

What does Strftime mean in Python?

The strftime() function is used to convert date and time objects to their string representation. It takes one or more input of formatted code and returns the string representation. Syntax : strftime(format) Returns : It returns the string representation of the date or time object.

How do I use Strftime and Strptime in Python?

Python time strptime() MethodThe format parameter uses the same directives as those used by strftime(); it defaults to "%a %b %d %H:%M:%S %Y" which matches the formatting returned by ctime(). If string cannot be parsed according to format, or if it has excess data after parsing, ValueError is raised.


2 Answers

isoformat works on datetime instances w/o limitation of range:

>>> import datetime
>>> x=datetime.datetime(1865, 7, 2, 9, 30, 21)
>>> x.isoformat()
'1865-07-02T09:30:21'

If you need a different-format string it's not too hard to slice, dice and remix pieces of the string you get from isoformat, which is very consistent (YYYY-MM-DDTHH:MM:SS.mmmmmm, with the dot and following microseconds omitted if microseconds are zero).

like image 93
Alex Martelli Avatar answered Oct 05 '22 20:10

Alex Martelli


The documentation seems pretty clear about this:

The exact range of years for which strftime() works also varies across platforms. Regardless of platform, years before 1900 cannot be used.

So there isn't going to be a solution that uses strftime(). Luckily, it's pretty straightforward to do this "by hand":

>>> "%02d-%02d-%02d %02d:%02d" % (d.year,d.month,d.day,d.hour,d.minute)
'1899-01-01 00:00'
like image 20
Dave Webb Avatar answered Oct 05 '22 20:10

Dave Webb