Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Initialize from existing datetime.date object? #250

Closed
jacebrowning opened this issue Jul 5, 2018 · 22 comments · Fixed by #732
Closed

Initialize from existing datetime.date object? #250

jacebrowning opened this issue Jul 5, 2018 · 22 comments · Fixed by #732

Comments

@jacebrowning
Copy link
Contributor

jacebrowning commented Jul 5, 2018

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))
@ghost
Copy link

ghost commented Jul 21, 2018

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)

@jacebrowning
Copy link
Contributor Author

Could we add that behavior to pendulum.instance()?

@kakulukia
Copy link

+1

@trancee
Copy link

trancee commented Sep 4, 2018

+1

I would also check for datetime.time so we have all possible cases of a datetime covered with Pendulum.

@joaodlf
Copy link

joaodlf commented Dec 12, 2018

+1 This would be a great addition to the API!

@SemMulder
Copy link

+1

5 similar comments
@joaomcarlos
Copy link

+1

@racerthunder
Copy link

+1

@lonsun
Copy link

lonsun commented Jun 26, 2020

+1

@Alsanis
Copy link

Alsanis commented Jul 6, 2020

+1

@rodrigondec
Copy link

+1

@madisvain
Copy link

I would like to pump this. I find doing this over and over and would love if pendulum had this feature built in.

@AaronBeaudoin
Copy link

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.

@richieadler
Copy link

+1

@kakulukia
Copy link

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.

@blondelg
Copy link

+1

4 similar comments
@yardensachs
Copy link

+1

@lena
Copy link

lena commented May 3, 2023

+1

@mcguigon0033
Copy link

+1

@jackb2113
Copy link

+1

@codytowstik
Copy link

+1 !

@FichteFoll
Copy link

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.

pendulum/pendulum/date.py

Lines 740 to 755 in c4e518c

def today(cls) -> Self:
dt = date.today()
return cls(dt.year, dt.month, dt.day)
@classmethod
def fromtimestamp(cls, t: float) -> Self:
dt = super().fromtimestamp(t)
return cls(dt.year, dt.month, dt.day)
@classmethod
def fromordinal(cls, n: int) -> Self:
dt = super().fromordinal(n)
return cls(dt.year, dt.month, dt.day)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.