myusuf3 / delorean

Delorean: Time Travel Made Easy
http://delorean.rtfd.org/
MIT License
1.84k stars 128 forks source link

Delorean object from aware datetimes #22

Closed mwaterfall closed 11 years ago

mwaterfall commented 11 years ago

There seems to be no way to create a Delorean object from an aware datetime object.

Is there a reason for this?

I tend to work with fully aware datetime objects in UTC, but with them being passed around and used everywhere, it's not particularly easy to create a Delorean object from them, without having to do:

dt = Delorean(datetime=dt_utc.replace(tzinfo=None), timezone='UTC')

Which is a bit tedious for an awesome library like Delorean!

I might be a little naive though (excuse the pun!) so I wanted to ask!

myusuf3 commented 11 years ago

I initially made this the default behaviour in order to help people from making mistakes with passing in localized datetime objects forcing them to take into account what they were actually doing.

I can see how this can be annoying for currently existing systems. I will definitely try to get this in an upcoming release.

Are there any other examples of usage? That are inconvenient to use it with that I may have missed?

fawce commented 11 years ago

I have a lot of code that also uses pytz for setting the tzinfo, so another nice feature would be to have the constructor accept as a single argument a datetime that has a pytz timezone:

e.g.

dt = datetime(2010, 3, 12, tzinfo=pytz.utc)
dd = Delorean(dt)
dt_eastern = dd.shift("US/Eastern").datetime
mwaterfall commented 11 years ago

@myusuf3 No I've not come across anything else that's inconvenient, I'm really enjoying using Delorean. If it had a kwarg that took an aware datetime (ensuring anyone that used it is doing it intentionally) then that would be perfect!

myusuf3 commented 11 years ago

@mwaterfall @fawce I have added support for dealing with already localized datetime objects. https://github.com/myusuf3/delorean/commit/c6f4185ae4a464859321ca924962bd47e6d2d35d

Hope this alleviates some pain with the library.

I would reread this section for the new functionality. http://delorean.readthedocs.org/en/latest/quickstart.html#making-some-time

fawce commented 11 years ago

Since my most common use is shift, I would prefer the time zone parameter to shift an aware date time rather than be ignored.

myusuf3 commented 11 years ago

@fawce you can still shift after the object is created, but I guess I can update that caveat in an upcoming release.

mwaterfall commented 11 years ago

@myusuf3 Awesome, thanks for incorporating this feature. Much appreciated :-)