Fix Python – How can I parse a time string containing milliseconds in it with python?

I am able to parse strings containing date/time with time.strptime
>>> import time
>>> time.strptime(’30/03/09 16:31:32′, ‘%d/%m/%y %H:%M:%S’)
(2009, 3, 30, 16, 31, 32, 0, 89, -1)

How can I parse a time string that contains milliseconds?
>>> time.strptime(’30/03/09 16:31:32.123′, ‘%d/%m/%y %H:%M:%S’)
Traceback (most recent call last):
File “

Fix Python – SQLAlchemy default DateTime

This is my declarative model:
import datetime
from sqlalchemy import Column, Integer, DateTime
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class Test(Base):
__tablename__ = ‘test’

id = Column(Integer, primary_key=True)
created_date = DateTime(default=datetime.datetime.utcnow)

However, when I tr….

Fix Python – In Python, how do you convert seconds since epoch to a `datetime` object?

The time module can be initialized using seconds since epoch:
>>> import time
>>> t1=time.gmtime(1284286794)
>>> t1
time.struct_time(tm_year=2010, tm_mon=9, tm_mday=12, tm_hour=10, tm_min=19,
tm_sec=54, tm_wday=6, tm_yday=255, tm_isdst=0)

Is there an elegant way to initialize a datetime.datetime object in the same way?
….

Fix Python – How do I get a value of datetime.today() in Python that is “timezone aware”?

I am trying to subtract one date value from the value of datetime.datetime.today() to calculate how long ago something was. But it complains:
TypeError: can’t subtract offset-naive and offset-aware datetimes

The return value from datetime.datetime.today() doesn’t seem to be “timezone aware”, while my other date value is. How do I get a return val….

Fix Python – Iterating through a range of dates in Python

I have the following code to do this, but how can I do it better? Right now I think it’s better than nested loops, but it starts to get Perl-one-linerish when you have a generator in a list comprehension.
day_count = (end_date – start_date).days + 1
for single_date in [d for d in (start_date + timedelta(n) for n in range(day_count)) if d <= end_d....

Fix Python – Creating a range of dates in Python

I want to create a list of dates, starting with today, and going back an arbitrary number of days, say, in my example 100 days. Is there a better way to do it than this?
import datetime

a = datetime.datetime.today()
numdays = 100
dateList = []
for x in range (0, numdays):
dateList.append(a – datetime.timedelta(days = x))
print dateList

….

Fix Python – Adding days to a date in Python

I have a date “10/10/11(m-d-y)” and I want to add 5 days to it using a Python script. Please consider a general solution that works on the month ends also.
I am using following code:
import re
from datetime import datetime

StartDate = “10/10/11”

Date = datetime.strptime(StartDate, “%m/%d/%y”)

print Date -> is printing ‘2011-10-10 00:00:00’
Now ….