[Tutor] time comparison question

Liam Clarke cyresse at gmail.com
Fri Nov 26 13:37:30 CET 2004


Hi Brian, 

class datetime( year, month, day, hour=0, minute=0, second=0,
microsecond=0, tzinfo=None)


Ease of use demo -

>>>import datetime
>>>date1 = datetime.datetime(2001,11,11,12)
>>>print str(date1)
2001-11-11 12:00:00

>>>date2 = datetime.datetime(2007,10,10,9,20,20,20)
>>>print str(date2)
2007-10-10 09:20:20.000020

>>>print date1.strftime('"%d-%b-%Y"')
11-Nov-2001

>>>date2.strftime('"%A, %d %B %Y. Time is %X"')
Wednesday, 10 October 2007. Time is 09:20:20

>>>print "difference between",date1,"&",date2
difference between 2001-11-11 12:00:00 & 2007-10-10 09:20:20.000020

>>> print date1-date2
-2159 days, 2:39:39.999980

>>> print "difference between",date2,"&",date1
difference between 2007-10-10 09:20:20.000020 & 2001-11-11 12:00:00

>>>print date2-date1
2158 days, 21:20:20.000020

>>>dateDif=date2-date1
>>>type(dateDif)
<type 'datetime.timedelta'>

So yeah, poke around, it'll do pretty much everything date related you need.

Oh, and for your particular problem - so if 10/10/2007 minus
11/11/2001 is a positive value, then you know that all future dates
will have a positive difference. Past dates will be negative
difference, and it'd be very unlikely that you'd get a zero value, but
it's possible.

HTH

Liam Clarke

On Sat, 27 Nov 2004 00:56:43 +1300, Liam Clarke <cyresse at gmail.com> wrote:
> Hi Brian,
> 
> I was in a similar position to you 3 weeks ago, and I was using
> time.time, and time.gmtime.
> 
> Each of these returns a 9 value tuple, which is documented deep within
> the time module.
> 
> The 9 value tuple has year to microsecond, and day of the week
> contained, so you can use it if you wish.
> 
>  I'm in the same boat as you, regarding OOP, although I use it when
> suitable, and all I've got to say is - what's the big deal? I can see
> it would be useful for certain applications, but it's nothing
> lifechanging, unlike Python :D
> 
> I would recommend biting the bullet and using a date/time object. You
> don't need to grok OOP to use it, and it makes this easier.
> 
>  May I post some code to demonstrate?
> 
> I wanted to write code which would find out what date last Friday was,
> whenever the code was run.
> 
> Using time.gmtime, this is what I accomplished (kept for posteritie's
> sake only..)
> def getFriday(mail_offset):
> 
>        gm_data=time.gmtime()
>        (year, month, day, weekday)=gm_data[0], gm_data[1], gm_data[2],
> gm_data[6]
>        date_offset= -(mail_offset-weekday)
>        last_friday=datetime.date(year, month, day) -
> datetime.timedelta(days=date_offset)
>        retrieve_date_string=str(last_friday)
>        split_date=retrieve_date_string.split('-')
>        email_sent_date=time.strftime("%d-%b-%Y", (int(split_date[0]),
> int(split_date[1]), int(split_date[2]), 0, 0, 0, 0, 0, 0))
> 
>        return email_sent_date
> 
> Now, i posted this up to this list, Kent Johnson recommended this instead-
> 
> def getFriday(day):
>     lastFriday = datetime.date.today()
>     oneday = datetime.timedelta(days=1)
> 
>     while lastFriday.weekday() != day:
>         lastFriday -= oneday
> 
>     return lastFriday.strftime('%d-%b-%Y')
> 
> Both returned a date in the format 15-Oct-2004
> 
> Guess which one I went with?
> My original code was brute forcish, unmaintainable & ugly, but it
> worked when I wrote it. Sounds like Perl really. But notice that I had
> to use an object anyway to invoke timedelta, so all my messing around
> was in vain.
> 
> Highly recommend this -
> 
> a) Use the datetime module
> b) Use datetime objects, so you don't have to mess around with
> functions that require 9 digit tuples
> c) Listen to Kent if he posts, he's good.
> 
> Give datetime a try, and see how it goes.
> 
> Good luck,
> 
> Liam Clarke
> 
> 
> 
> 
> On Fri, 26 Nov 2004 04:45:53 -0500, Brian van den Broek
> <bvande at po-box.mcgill.ca> wrote:
> > Hi all,
> >
> > I'm trying to build some Python 2.3.4 code which will determine if the
> > actual time when it is run is after the date and time specified in some
> > data. I want my data to be able to specify the target date/time in a
> > various levels of detail. I.e., I want 2004-11-28 13:25, 2004-11-28,
> > 11-28, 11-28 13:25 to all be accepted.
> >
> > My first thought was to get the current time via time.time() and then
> > parse my target date into a struct_time tuple to pass to time.mktime(),
> > run a comparison and be done. However, a struct_time tuple has data that
> > I won't know, given one of my target dates. I can make sensible dummies
> > for seconds (as well as year, hours and minutes when my target does not
> > specify them), and I know that the time module will try to make a
> > reasonable guess if given -1 for the DST element. But for
> > a_struct_time_tuple[-3:-1], I'd just be guessing at the weekday and day
> > of year. This leads to two questions:
> >
> > 1) As I write, time.asctime() gives 'Fri Nov 26 04:32:18 2004' for a
> > time.localtime() of (2004, 11, 26, 4, 32, 18, 4, 331, 0). If I were
> > filling out a target date of 2004-11-26 04:32, I might well end up
> > passing something like (2004, 11, 26, 4, 32, 18, 6, 328, 0) to
> > time.mktime() for the result 'Sun Nov 26 04:32:18 2004'. If I am not
> > actually using the weekday information, I suspect that I can just ignore
> > the incorrect 'Sun'. But I feel better checking -- is anything going to
> > break by giving struct_time elements that don't make sense (provided of
> > course I am not myself explicitly drawing upon them)?
> >
> > 2) Naturally, these worries lead me to think that there must be a better
> > way. I took a brief look at the datetime module, thinking it was a
> > natural place to find such tools. But it is all in terms of classes.
> > Sadly, I am still not too comfortable with doing things via OOP, so I'd
> > prefer a procedural (if that's the right term for a program with
> > functions but no classes) way. Is there a cleaner, non-class employing,
> > way of meeting my goal of finding out if a date of the form YYYY-MM-DD
> > is in the future, present, or past? (If it matters, I am on Windows.)
> >
> > Thanks for any links, etc.
> >
> > Best to all,
> >
> > Brian vdB
> >
> > _______________________________________________
> > Tutor maillist  -  Tutor at python.org
> > http://mail.python.org/mailman/listinfo/tutor
> >
> 
> 
> --
> 'There is only one basic human right, and that is to do as you damn well please.
> And with it comes the only ba sic human duty, to take the consequences.
> 


-- 
'There is only one basic human right, and that is to do as you damn well please.
And with it comes the only basic human duty, to take the consequences.


More information about the Tutor mailing list