pendulum
pendulum copied to clipboard
Initialize from existing `datetime.date` object?
pendulum.instance(dt)
works for datetime.datetime
, but how can I initialize a Pendulum object from an existing datetime.date
(specifically a Django DateField
)?
My current workaround:
pendulum.parse(str(date_object))
from datetime import datetime as dt
import pendulum
pendulum.instance(dt.fromordinal(date_object.toordinal()))
This should work.
Edit:
This is better:
pendulum.datetime(date.year,date.month,date.day)
Could we add that behavior to pendulum.instance()
?
+1
+1
I would also check for datetime.time
so we have all possible cases of a datetime covered with Pendulum.
+1 This would be a great addition to the API!
+1
+1
+1
+1
+1
+1
I would like to pump this. I find doing this over and over and would love if pendulum had this feature built in.
This is a no-brainer for me. I work with an ORM that returns datetime.date
instances, and having to pick them apart to pass them into pendulum.instance
feels like the symptom of a missing feature.
+1
I have created a first draft at https://github.com/kakulukia/pendulum .. im missing some tests before opening a puss request, but like to get some feedback first.
+1
+1
+1
+1
+1
+1 !
I've also looked into the documentation & the code in search for this behavior. In the code, I see that even the shorthand class functions to initialize a Date
like today
first construct a native datetime.date
object before they create the instance using its result. This surely would also profit from a Date.instance
class function constructor.
https://github.com/sdispater/pendulum/blob/c4e518c950bcf0a4214b36c5992ae27d3c40e47f/pendulum/date.py#L740-L755