user392409
user392409

Reputation: 143

Using datetime and manipulating date strings using python

I have a file of the following format

Summary:meeting Description:None DateStart:20100629T110000 DateEnd:20100629T120000 Time:20100805T084547Z
Summary:meeting Description:None DateStart:20100630T090000 DateEnd:20100630T100000 Time:20100805T084547Z 

I need to create a function that would retrieve "Summary" at a given "date" and "time". For example the function would have two arguments, the date and time, which wont be in date time formats. It needs to check if the date and time specified in the function argument is between the date and times in the DateStart and DateEnd in the file.

I am not sure how to retrieve time and date from the format specified above [i.e., 20100629T110000]. I was trying to use the following line_time = datetime.strptime(time, "%Y%D%MT%H%M%S"), where time is "20100629T110000", but i am getting a lot of errors, like " datetime.datetime has not attribute strptime".

Whats the right way to do this function, thanks in advance.

....................EDIT................

Here is my error

Python 2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.

    ****************************************************************
    Personal firewall software may warn about the connection IDLE
    makes to its subprocess using this computer's internal loopback
    interface.  This connection is not visible on any external
    interface and no data is sent to or received from the Internet.
    ****************************************************************

>>>
Traceback (most recent call last):
  File "C:\Python24\returnCalendarstatus", line 24, in -toplevel-
    status = calendarstatus()
  File "C:\Python24\returnCalendarstatus", line 16, in calendarstatus
    line_time = datetime.strptime(time, "%Y%m%dT%H%M%S")
AttributeError: type object 'datetime.datetime' has no attribute 'strptime'
>>> 

And here is my code

import os
import datetime
import time
from datetime import datetime

def calendarstatus():

    g = open('calendaroutput.txt','r')
    lines = g.readlines()
    for line in lines:        
        line=line.strip()
        info=line.split(";")
        summary=info[1]
        description=info[2]
        time=info[5];
        line_time = datetime.strptime(time, "%Y%m%dT%H%M%S") 
        return line_time.year 

status = calendarstatus()

Upvotes: 3

Views: 8335

Answers (2)

Daniel Roseman
Daniel Roseman

Reputation: 599610

You need to actually read the documentation appropriate for your version of Python. See the note on strptime in the docs for datetime:

New in version 2.5.

and you are using version 2.4. You will need to use the workaround mentioned in that documentation:

import time
import datetime
[...]
time_string = info[5]
line_time = datetime(*(time.strptime(time_string, "%Y%m%dT%H%M%S")[0:6]))

Upvotes: 6

David Webb
David Webb

Reputation: 193706

Don't confuse the datetime module with the datetime Objects in the module.

The module has no strptime function, but the Object does have a strptime class method:

>>> time = "20100629T110000"
>>> import datetime
>>> line_time = datetime.strptime(time, "%Y%m%dT%H%M%S")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'strptime'
>>> line_time = datetime.datetime.strptime(time, "%Y%m%dT%H%M%S")
>>> line_time
datetime.datetime(2010, 6, 29, 11, 0)

Note the second time we have to reference the class as datetime.datetime.

Alternatively you can import just the class:

>>> from datetime import datetime
>>> line_time = datetime.strptime(time, "%Y%m%dT%H%M%S")
>>> line_time
datetime.datetime(2010, 6, 29, 11, 0)

Also, I changed your format string from %Y%D%MT%H%M%S to %Y%m%dT%H%M%S which I think is what you want.

Upvotes: 6

Related Questions