Need Help comparing dates

C

colincolehour

I am new to Python and am working on my first program. I am trying to
compare a date I found on a website to todays date. The problem I have
is the website only shows 3 letter month name and the date.
Example: Jun 15

How would I go about comparing that to a different date? The purpose of
my program is to load a webpage and see if the content on the front
page is fresh or stale as in older than a few days. Any help in the
right direction would be great!
 
B

Ben Finney

I am new to Python and am working on my first program. I am trying
to compare a date I found on a website to todays date. The problem I
have is the website only shows 3 letter month name and the date.
Example: Jun 15

The 'datetime' module in the standard library will do the job of
creating date objects that can be compared.

<URL:http://docs.python.org/lib/module-datetime>

Construct a date from arbitrary values with datetime.date(), get the
current date with datetime.date.today(). The objects returned by those
functions can be compared directly.

As for how to get from a string representation to a date object,
you're now talking about parsing strings to extract date/time
information. This isn't provided in the standard library, largely
because there's no "one obvious way to do it". An existing PEP
proposing adding such functionality has since been withdrawn:

<URL:http://www.python.org/dev/peps/pep-0321/>

Parsing datetime values from strings is fuzzy and prone to lots of
assumptions and errors, so the programmer must choose their own set of
assumptions. One popular implementation of a specific set of
assumptions is the egenix 'mxDateTime' module.

<URL:http://www.egenix.com/files/python/mxDateTime.html>

That module predates (and was largely an inspiration for) the standard
library 'datetime' module; however, I don't know if the egenix module
uses objects that can be used with those from the standard library.
 
T

Tim Chase

I am new to Python and am working on my first program. I am trying to
compare a date I found on a website to todays date. The problem I have
is the website only shows 3 letter month name and the date.
Example: Jun 15

No year, right? Are you making the assumption that the year is
the current year?
How would I go about comparing that to a different date?

Once you've got them as dates,

you can just compare them as you would any other comparable items.

If you need to map the month-strings back into actual dates, you
can use this dictionary:
>>> month_numbers = dict([(date(2006, m, 1).strftime("%b"), m)
for m in range(1,13)])

It happens to be locale specific, so you might have to tinker a
bit if you're mapping comes out differently from what the website
uses. I also made the assumption the case was the same (rather
than trying to normalize to upper/lower case)

Then, you can use
>>> webpageDateString = "Mar 21"
>>> webMonth, webDay = webpageDateString.split()
>>> month = m[webMonth]
>>> day = int(webDay)
>>> webpageDate = date(date.today().year, month, day)
>>> compareDate = date.today()
>>> compareDate < webpageDate False
>>> compareDate > webpageDate
True

You can wrap the load in a function, something like
.... monthString, dayString = dateString.split()
.... month = month_numbers[monthString]
.... day = int(dayString)
.... return date.today() < date(year, month, day)

which will allow you to do
False

and the like.

There's plenty of good stuff in the datetime module.

-tkc
 
B

Ben Finney

Ben Finney said:
The 'datetime' module in the standard library will do the job of
creating date objects that can be compared.

<URL:http://docs.python.org/lib/module-datetime>

Construct a date from arbitrary values with datetime.date(), get the
current date with datetime.date.today(). The objects returned by those
functions can be compared directly.

As for how to get from a string representation to a date object,
you're now talking about parsing strings to extract date/time
information. This isn't provided in the standard library

As soon as I sent this, I remembered that the standard library *does*
provide datetime parsing:

<URL:http://docs.python.org/lib/module-time#l2h-1956>

So your task now consists of:

- get a date object of today's date using datetime.date.today()
- define a format for parsing a string date
- get a struct_time object from time.strptime() feeding it the
string and the format
- make any assumptions about missing pieces of the date (e.g. the
year)
- get a date object by feeding values to datetime.date()
- compare the two date objects
 
B

Ben Finney

Tim Chase said:
If you need to map the month-strings back into actual dates, you
can use this dictionary:
month_numbers = dict([(date(2006, m, 1).strftime("%b"), m)
for m in range(1,13)])

Or you can just use the same format codes to specify that
time.strptime() should do the parsing for you. (Which I remembered too
late for my initial reply.)
 
J

John Machin

The 'datetime' module in the standard library will do the job of
creating date objects that can be compared.

<URL:http://docs.python.org/lib/module-datetime>

Construct a date from arbitrary values with datetime.date(), get the
current date with datetime.date.today(). The objects returned by those
functions can be compared directly.

As for how to get from a string representation to a date object,
you're now talking about parsing strings to extract date/time
information. This isn't provided in the standard library,

time.strptime() doesn't do "parsing strings to extract date/time
information"?

It appears to me to do a reasonably tolerant job on the OP's spec i.e.
month abbreviation and a day number:

|>> import time
|>> time.strptime('Jun 15', '%b %d')
(1900, 6, 15, 0, 0, 0, 4, 166, -1)
|>> time.strptime('dec 9', '%b %d')
(1900, 12, 9, 0, 0, 0, 4, 152, -1)

The OP should be able to use that to get the month and the day. The year
of the web page date will need some guessing (e.g. is within the last
year) and not even Python has a crystal ball :)

BTW, datetime has grown a strptime in version 2.5.


[snip]
That module predates (and was largely an inspiration for) the standard
library 'datetime' module; however, I don't know if the egenix module
uses objects that can be used with those from the standard library.

It doesn't.
 
C

colincolehour

So when I grab the date of the website, that date is actually a string?

How would I got about converting that to a date?
 
J

John Machin

So when I grab the date of the website, that date is actually a string?

Yes. Anything you grab off a website (or read from a file) will be held
in a string. Typically you would then need to convert it (or parts of
it) to some other type(s) e.g. int, float, date, ...
How would I got about converting that to a date?

By following the instructions you have already been given. The response
by Tim Chase gives you a stir-by-stir recipe.
===
You said "I am new to Python and am working on my first program". Here
are some diagnostic questions the answers to which should enable us to
prescribe some more help for you: In what other computer language(s)
have you written programs? What book or tutorial are you using to learn
Python? Have you worked through the book/tutorial's examples/exercises
before starting "my first program"?

Cheers,
John
 
C

colincolehour

Thanks for the reply, the book I'm actually using is Python Programming
for the absolute beginner. The book has been good to pick up basic
things but it doesn't cover time or dates as far as I can tell. As for
previous programming experience, I have had some lite introductions to
C & C++ about 6 years ago but I never went past the introductions.So
far "my first program" has been written from snippets from many
different websites.

Also i'm using ActiveState Active Python 2.4 on a Windows XP machine.

I will try to work through Tim's response. I tried using it yesterday
but I was really confused on what I was doing.

Colin
 
T

Tim Chase

I will try to work through Tim's response. I tried using it
yesterday but I was really confused on what I was doing.

I'll put my plug in for entering the code directly at the shell
prompt while you're trying to grok new code or toy with an idea.
It makes it much easier to see what is going on as you enter
it. You can ask for any variables' value at any time. It's a
small pain when you're entering some nested code, and biff up on
the interior of it (throwing an exception, and having to restart
the function-definition/loop/class-definition statement all
over), but it certainly has its advantages for learning the language.

On top of that, you can usually ask for help on any object, so if
you wanted to know about the datetime module or the date object
in the datetime module, you can just use

>>> import datetime
>>> help(datetime)
>>> help(datetime.date)

which will give you all sorts of documentation on a date object.
I'm also partial to learning about what methods the object
exposes via the dir() function:

>>> dir(datetime.date)
or
>>> print "\n".join(dir(datetime.date))

IMHO, help() and dir() at the command-line combine to make one of
Python's best selling points...ease of learning. I understand
Perl and Ruby might have something similar, but I've never liked
their idioms. Java/C/C++, you have the edit/compile/run cycle,
so if you just want to explore some simple code ideas, you've got
a 3-step process, not just a simple "try it out right here and
now" method.

I even stopped using "bc" as my command-line calculator,
preferring the power of command-line python. :)

Just a few thoughts on what made learning python easier for me.

Again, if you have questions, and can't figure out the answers by
stepping through the code in the command shell (or some
strategically placed print statements), this is certainly the
forum for asking those questions. It's full of smart and helpful
folks.

-tkc
 
C

colincolehour

I kept getting a Python error for the following line:

month = m[webMonth]


I changed it to month = month_numbers[webMonth]

and that did the trick.


Tim said:
I am new to Python and am working on my first program. I am trying to
compare a date I found on a website to todays date. The problem I have
is the website only shows 3 letter month name and the date.
Example: Jun 15

No year, right? Are you making the assumption that the year is
the current year?
How would I go about comparing that to a different date?

Once you've got them as dates,

you can just compare them as you would any other comparable items.

If you need to map the month-strings back into actual dates, you
can use this dictionary:
month_numbers = dict([(date(2006, m, 1).strftime("%b"), m)
for m in range(1,13)])

It happens to be locale specific, so you might have to tinker a
bit if you're mapping comes out differently from what the website
uses. I also made the assumption the case was the same (rather
than trying to normalize to upper/lower case)

Then, you can use
webpageDateString = "Mar 21"
webMonth, webDay = webpageDateString.split()
month = m[webMonth]
day = int(webDay)
webpageDate = date(date.today().year, month, day)
compareDate = date.today()
compareDate < webpageDate False
compareDate > webpageDate
True

You can wrap the load in a function, something like
... monthString, dayString = dateString.split()
... month = month_numbers[monthString]
... day = int(dayString)
... return date.today() < date(year, month, day)

which will allow you to do
False

and the like.

There's plenty of good stuff in the datetime module.

-tkc
 
T

Tim Chase

I kept getting a Python error for the following line:
month = m[webMonth]

I changed it to month = month_numbers[webMonth]

and that did the trick.

Sorry for the confusion. Often when I'm testing these things,
I'll be lazy and create an alias to save me the typing. In this
case, I had aliased the month_numbers mapping to "m":

>>> m = month_numbers

and it slipped into my pasted answer email. You correctly fixed
the problem. The purpose of the mapping is simply to resolve the
month name to the month number so that the month number can be
used. Depending on how predictable the website's date string
will be, you can use

month_numbers = dict([(date(2006, m, 1).strftime("%b").upper(),
m) for m in range(1,13)])

and then use

month = month_numbers[webMonth.upper()]

or

month = month_numbers[webMonth[0:3].upper()]

depending on whether the website might return full month names or
not.

-tkc
 
C

colincolehour

The program works great! It does everything I wanted it to do and now
I'm already thinking about ways of making it more useful like emailing
me the results of my program.

Thanks everyone for the help and advice.

Colin

Tim said:
I kept getting a Python error for the following line:

month = m[webMonth]

I changed it to month = month_numbers[webMonth]

and that did the trick.

Sorry for the confusion. Often when I'm testing these things,
I'll be lazy and create an alias to save me the typing. In this
case, I had aliased the month_numbers mapping to "m":

>>> m = month_numbers

and it slipped into my pasted answer email. You correctly fixed
the problem. The purpose of the mapping is simply to resolve the
month name to the month number so that the month number can be
used. Depending on how predictable the website's date string
will be, you can use

month_numbers = dict([(date(2006, m, 1).strftime("%b").upper(),
m) for m in range(1,13)])

and then use

month = month_numbers[webMonth.upper()]

or

month = month_numbers[webMonth[0:3].upper()]

depending on whether the website might return full month names or
not.

-tkc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,111
Latest member
VetaMcRae
Top