sdispater / pendulum

Python datetimes made easy
https://pendulum.eustace.io
MIT License
6.25k stars 387 forks source link

Pendulum creation and transformation with datetime.date and datetime.time #82

Closed AndreasBackx closed 5 years ago

AndreasBackx commented 7 years ago

Currently the Pendulum supports creating from given hours, minutes... using:

These all however require you to pass the hours, minutes and more manually. It would be great if these accepted the datetime.date and datetime.time classes like so:

>>> import datetime
>>> from pendulum import Pendulum
>>> today_datetime = datetime.date.today()
>>> today_datetime
datetime.date(2016, 11, 26)
>>> today_pendulum = Pendulum.create_from_date(today_datetime)
>>> today_pendulum
<Pendulum [2016-11-26T00:00:00+01:00]>

Many libraries incorporate these classes and transforming them using Pendulum would be great.

sdispater commented 7 years ago

As of the next version (likely 0.8.0), both create_from_date() and create_from_time() will be deprecated since you can use create() to get the same behavior.

Regarding with_date() and with_time() they will be replaced by on() and at() (with a deprecation warning) to avoid the confusion and the intuitive will to pass date and time objects to them.

So, in the end, I do not intend to support passing native objects to them in a near future.

AndreasBackx commented 7 years ago

Unfortunate, it's quite annoying to write:

some_pendulum.on(
    year=some_date.year,
    month=some_date.month,
    day=some_date.day
)

It would just be neat if we could write:

some_pendulum.on(some_date)

We could always just use simple *args and **kwargs manipulation to make that possible? Am I missing something behind why you don't intend on supporting it? It seems to be a lot more intuitive.

danilobellini commented 7 years ago

For dates you can:

import datetime, pendulum
today_datetime = datetime.date.today()
pendulum.create(*today_datetime.timetuple()[:3])
AndreasBackx commented 7 years ago

@danilobellini that's neat, but still a bit of a workaround and not as clean imo.

sdispater commented 7 years ago

@AndreasBackx For now, I don't want to change the API any further. I will rename and remove these methods but won't change their signatures.

It's not set in stone and I might introduce it at some point since I agree that it could help a little but I just don't want to make a lot of changes on the road to the first stable 1.0.0 version.

I will even make a 0.8.0 that I hadn't planned to give people time to adjust.

I will keep this idea in mind though but just don't expect it to make it to the 1.0.0 version :-)

AndreasBackx commented 7 years ago

@sdispater unfortunate, I hoped that it could've been added before 1.0.0 because it remains backwards compatibility. Too bad then.

klardotsh commented 7 years ago

@sdispater Isn't this right up the alley of pendulum.instance()? Right now this throws an AttributeError because of course datetime.date doesn't have a tzinfo attribute, but patching up instance() to support date and time instances rather than just datetime would be trivial and seemingly backwards-compatible. Actually, it appears the only reasons one can't already use this is because pendulum.py#198 is unsafe and uses dt.tzinfo without a hasattr safety check, and the fact that lines 211-215 also blindly assume a full datetime instance (getattr's 3rd argument could be handy in a refactor here).

sdispater commented 7 years ago

I don't think I will change instance() to accept date and time objects. This is what Date.instance() and Time.instance() are for.

The initial and only purpose of instance() is to transform a datetime object to a Pendulum object.

And as I said, it's fairly easy to get a Pendulum instance from a date or even a time.

Regarding on() and at() methods, I haven't made my mind yet. That's why I am leaving this issue open for now.