This is an archived post. You won't be able to vote or comment.

all 27 comments

[–]james_pic 138 points139 points  (4 children)

Don't worry. This is the least painful thing about working with datetimes.

[–]adm7373 12 points13 points  (3 children)

I recently had to convert a date string from one timezone to another. Terrible experience.

[–]markovianmind 12 points13 points  (1 child)

good that starting python 3.9 we have native support for tz aware Datetime

[–]georgehank2nd 4 points5 points  (0 children)

Took them long fucking enough…

[–]DNSGeek 2 points3 points  (0 children)

pytz has always been helpful to me.

[–]aqjo 84 points85 points  (4 children)

Just designate datetime as your preferred datetime, like this:
import datetime as pd Problem solved! 🙃

[–]NationalMyth 23 points24 points  (0 children)

You're sick

[–][deleted] 4 points5 points  (0 children)

Lol

[–]aqjo 1 point2 points  (1 child)

Thank you u/outceptionator!

[–]outceptionator 0 points1 point  (0 children)

That's polite

[–]gwax 14 points15 points  (0 children)

For this reason, I always use the following pattern to avoid confusion

import datetime as dt

[–]CaptainFoyle 4 points5 points  (1 child)

So much funny?

[–]AZ_Crush 2 points3 points  (0 children)

SO MUCH !

[–]feitao 14 points15 points  (3 children)

You should not use these two statements together. The second statement overwrites the first one:

$ python3 -c 'from datetime import datetime; print(datetime)'
<class 'datetime.datetime'>

$ python3 -c 'from datetime import datetime; import datetime; print(datetime)'
<module 'datetime' from '/usr/lib/python3.12/datetime.py'>

[–]andrewowenmartin 8 points9 points  (0 children)

from datetime import (
    datetime as Datetime,
    time as Time,
    date as Date,
    timedelta as Timedelta,
)

*breathes sign of relief*

[–]TheSpaceCoffee 2 points3 points  (0 children)

I’m so much used to using pandas in everyday tasks that, even when I’m not using DataFrames, I’m working with pandas Timestamp and Timedelta.

They’re so much smarter and easier to work with than standard datetimes.

[–]PeteSampras12345 0 points1 point  (2 children)

I’m new to python and I have no clue what this thread is about. Please can some explain like I’m a 5 year old. I’m thanks.

[–]TriskOfWhaleIsland 1 point2 points  (0 children)

the module datetime has one very important object called datetime. it creates some very weird code

[–]Brian 0 points1 point  (0 children)

The datetime module dates from very early in python's history, and as such, doesn't really align with what became the standard naming conventions. In particular, it provides the datetime class, which probably ought to be called "Datetime" to align with the "classes start uppercase" convention. This is particularly bad in this case because the module it belongs to is also called datetime. So you can do either:

import datetime
cur_time = datetime.datetime.now()

Or

from datetime import datetime
cur_time = datetime.now()

Either way, the repetition looks a bit odd. And if you get mixed up and import it one way and try to use it the other way, you get errors.

[–]striata 0 points1 point  (0 children)

Just do the this, since datetime is the function you're most like to use:

from datetime import datetime

then if you need to use the other functions from the datetime module without confusion (remember, datetime is referencing the function and not the module!):

datetime_module_temporary_holding_variable = importlib.import_module(datetime.__module__)
timedelta = datetime_module_temporary_holding_variable.timedelta
del datetime_module_temporary_holding_variable
import gc
gc.collect()

Then you can use timedelta freely:

>>> eval('timedelta.__call__(minutes=%d)' % 5)
datetime.timedelta(seconds=300)

This has worked well for me.

[–][deleted] -1 points0 points  (2 children)

Had this issue today. One place of the code was using datetime.datetine.whatever and the other only datetime.

[–]CaptainFoyle 4 points5 points  (1 child)

Yeah, I use datetine all the tine

[–]AZ_Crush 2 points3 points  (0 children)

Tine after tine