The snippets the article give are good to get an UTC datetime or timestamp now.
I'd like to add that if you have any fancy datetime manipulation to do in Python, pendulum (https://pendulum.eustace.io/) is probably your best bet.
I'd like to add that if you have any fancy datetime manipulation to do in Python, pendulum (https://pendulum.eustace.io/) is probably your best bet.
It is best until you work with Pandas DataFrame (https://stackoverflow.com/questions/47849342/making-pandas-w...)
If you use numpy and pandas, you should also not use Python datetime, generators, most stdlib mathematical functions, the itertools module, random, etc.
It's the first thing you learn if you read any good pandas book, and the first thing I teach in my numpy/pandas trainings.
It has pretty much nothing to do with pendulum.
Basically, half the Python ecosystem is "well, except with numpy/pandas of course".
from zoneinfo import ZoneInfo
import pendulum
import datetime
def pendulum_to_datetime(pendulum_dt: pendulum.DateTime) -> datetime.datetime:
return datetime.datetime.fromtimestamp(pendulum_dt.timestamp(),
ZoneInfo(pendulum_dt.timezone_name))
# test
df = pd.DataFrame([[1, 2], [1, 2]], columns=['a', 'b'])
df["time_column"] = pendulum_to_datetime(pendulum.now())
print(df)
output a b time_column
0 1 2 2023-11-19 18:14:16.027777-05:00
1 1 2 2023-11-19 18:14:16.027777-05:00
>> df.dtypes
a int64
b int64
time_column datetime64[ns, America/New_York]
dtype: object