Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to convert a zope DateTime object into Python datetime object?

Tags:

python

zope

I need to convert a zope 2 DateTime object into a Python datetime object. What is the best way to do that? Thanks, Erika

like image 447
Erika Schuller Avatar asked Apr 05 '10 14:04

Erika Schuller


People also ask

How do you convert a date object to a datetime object in Python?

Using the datetime() Pass the year, month and day values of the desired date object (as my_date. year, my_date. month, my_date. day) to this constructor to convert a date object to datetime object.

How do I convert a datetime to a specific date in Python?

For this, we will use the strptime() method and Pandas module. This method is used to create a DateTime object from a string. Then we will extract the date from the DateTime object using the date() function and dt. date from Pandas in Python.

How do you convert a date to month and year in Python?

Method 1: Use DatetimeIndex. month attribute to find the month and use DatetimeIndex. year attribute to find the year present in the Date.


3 Answers

Newer DateTime implementations (2.11 and up) have a asdatetime method that returns a python datetime.datetime instance:

modernthingy = zopethingy.asdatetime()
like image 56
Martijn Pieters Avatar answered Sep 21 '22 02:09

Martijn Pieters


modernthingy = datetime.datetime.fromtimestamp(zopethingy.timeTime())

The datetime instance is timezone-naive; if you need to support timezones (as Zope2's DateTime does), I recommend third-party extension package pytz.

like image 32
Alex Martelli Avatar answered Sep 20 '22 02:09

Alex Martelli


If you mean this one

.strftime('%m/%d/%Y %H:%M') =  04/25/2005 10:19

then reverse is

>>> time.strptime('04/25/2005 10:19','%m/%d/%Y %H:%M')
time.struct_time(tm_year=2005, tm_mon=4, tm_mday=25, tm_hour=10, tm_min=19, tm_sec=0, tm_wday=0, tm_yday=115, tm_isdst=-1)
like image 37
YOU Avatar answered Sep 23 '22 02:09

YOU