mktime, how to handle dates before 01-01-1970 ?

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Oct 6 02:35:21 CEST 2009


En Mon, 05 Oct 2009 20:54:18 -0300, Stef Mientki <stef.mientki at gmail.com>  
escribió:

> I want to handle datetime vars in a general way, so I use the default  
> time-format,
> so I can use the standard cinversion procedures.
> Time format is the time difference since 1-1-1970.
>
> So how do I handle dates before 1-1-1970 ?

Use the datetime module and manage the dates as actual datetime objects,  
not as formatted strings.

py> import datetime
py> birthdate = datetime.datetime(1914, 7, 23)
py> birthdate
datetime.datetime(1914, 7, 23, 0, 0)
py> now = datetime.datetime.now()
py> now - birthdate
datetime.timedelta(34773, 77146, 765000)
py> _.days // 365
95


py> datetime.datetime(1931, 8, 23)
datetime.datetime(1931, 8, 23, 0, 0)
py> datetime.datetime(1851, 6, 12)
datetime.datetime(1851, 6, 12, 0, 0)
py> datetime.datetime(1492, 10, 12)
datetime.datetime(1492, 10, 12, 0, 0)
py> a = datetime.datetime(1492, 10, 12)
py> b = datetime.datetime(2009, 10, 5)
py> (b - a) // 365
datetime.timedelta(517, 27932, 54794)
py> b = datetime.datetime.now()
py> _.days
34773
py> _.days // 365
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
AttributeError: 'int' object has no attribute 'days'
py> now - birthdate
datetime.timedelta(34773, 77146, 765000)

> I'ld expect times before 1-1-1970, simply to become negative numbers
> (I'm interested in the age of living people, so that would suffice).
>
> Is there a general solution, (other library)
> or would it be better to handle all dates in the Delphi format (number  
> of days since  1-1-1900

py> birthdate.toordinal()
698912

(number of days since 1 AD, approximately)

-- 
Gabriel Genellina




More information about the Python-list mailing list