You are not logged in.

#1 2006-08-23 19:42:03

hypermegachi
Member
Registered: 2004-07-25
Posts: 311

parsing the date in python

i'm learning python right now, and writing a simple subversion mailing script.

how do i parse the date that's spit out from "svnlook date"?  i can't seem to find any parsing functions sad

thanks.

Offline

#2 2006-08-23 19:50:58

Mr Green
Forum Fellow
From: U.K.
Registered: 2003-12-21
Posts: 5,896
Website

Re: parsing the date in python

EDIT ooops yeah like what Dusty says .....


Mr Green

Offline

#3 2006-08-23 19:55:05

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: parsing the date in python

I'm not sure how the output looks, but you might want to look at these string functions:

find
endswith
startswith
replace
split

The last (split) is probably your best bet.

You might also want to look at the re module (for regular expressions). I suspect There is also a tokenize module, but I don't know anything about it.

HTH,
Dusty

Offline

#4 2006-08-23 20:01:58

hypermegachi
Member
Registered: 2004-07-25
Posts: 311

Re: parsing the date in python

:'(

i got babied by .net's datetime.parse function...

i guess i could regex it.  thanks.

Offline

#5 2006-08-23 20:52:47

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Offline

#6 2006-08-24 07:53:11

postlogic
Member
Registered: 2005-02-24
Posts: 410
Website

Re: parsing the date in python

import re ?

Offline

#7 2006-08-24 15:15:39

hypermegachi
Member
Registered: 2004-07-25
Posts: 311

Re: parsing the date in python

it's a very simple app.  i wanna generate a text file for the current date, containing subversion log messages, the dates, authors, and files modified.

i just make use of svnlook, and parse the output.  the format of the date that svnlook spits out is:

2006-08-24 10:21:29 -0400 (Thu, 24 Aug 2006)

really, i only need the first part.  i just figured python would have a function somewhere that would parse "2006-08-24" into a datetime object or something.

i've written the script in c# already, and seemed like a good opportunity to learn some python.

the easiest way so far is just to use substring, and create the datetime object manually.

Offline

#8 2006-08-24 15:27:42

Mr Green
Forum Fellow
From: U.K.
Registered: 2003-12-21
Posts: 5,896
Website

Re: parsing the date in python

errr split maybe

 a = "2006-08-24 10:21:29 -0400 (Thu, 24 Aug 2006)"
b = a.split()
print b[0]

or

 print a[0:10]

hth


Mr Green

Offline

#9 2006-08-24 16:55:43

hypermegachi
Member
Registered: 2004-07-25
Posts: 311

Re: parsing the date in python

this is what i've done,

sdate = os.popen(svnlook + " date " + svnrepo + " -r " + revision).read()
date = datetime.datetime(int(sdate[0:4]), int(sdate[5:7]), int(sdate[8:10]))

seems to work tongue
i'm still not used to this type inference...it's scary looking!

Offline

#10 2006-08-24 17:00:53

T-Dawg
Forum Fellow
From: Charlotte, NC
Registered: 2005-01-29
Posts: 2,736

Re: parsing the date in python

it wouldn't look so scary if you didn't put it all in one line tongue. I would go with split(' ') instead of slicing too..

Offline

#11 2006-08-24 17:04:21

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: parsing the date in python

I don't have time to test it, but I think the time.strptime() function does what you want. Here's the help:

time.strptime() wrote:

strptime(      string[, format])
    Parse a string representing a time according to a format. The return value is a struct_time as returned by gmtime() or localtime(). The format parameter uses the same directives as those used by strftime(); it defaults to "%a %b %d %H:%M:%S %Y" which matches the formatting returned by ctime(). If string cannot be parsed according to format, ValueError is raised. If the string to be parsed has excess data after parsing, ValueError is raised. The default values used to fill in any missing data are (1900, 1, 1, 0, 0, 0, 0, 1, -1) .

    Support for the %Z directive is based on the values contained in tzname and whether daylight is true. Because of this, it is platform-specific except for recognizing UTC and GMT which are always known (and are considered to be non-daylight savings timezones).

I think it'll return a rather ugly tuple that can be used to construct a datetime object or something, which will allow you really simple access to each of the fields.

Dusty

Offline

#12 2006-08-24 18:20:17

hypermegachi
Member
Registered: 2004-07-25
Posts: 311

Re: parsing the date in python

Penguin wrote:

it wouldn't look so scary if you didn't put it all in one line tongue.

you should look at my c# code if that looks scary  :twisted:


Dusty wrote:

...

you are indeed correct big_smile
the following will work

time.strptime(sdate[0:10], "%Y-%m-%d")

Offline

#13 2006-08-24 18:36:49

battra
Member
From: Earth.US.Illinois.Chicago
Registered: 2006-05-12
Posts: 71

Re: parsing the date in python

You might look at installing/using these modules:

http://labix.org/python-dateutil
http://www.nanoo.org/~daniel/parsetime/


"I know nothing except the fact of my ignorance."
- Socrates

Offline

Board footer

Powered by FluxBB