Comparing dates problem

Discussion in 'Python' started by kyosohma@gmail.com, May 9, 2007.

  1. Guest

    Hi,

    I am writing a reminder program for our Zimbra email client. One of
    the requirements I was given was to automatically increment or
    decrement the display to show something like the following:

    5 minutes until appointment

    or

    10 minutes past your appointment


    Either way, as each minute goes by, I need to increment it or
    decrement it. I am having trouble finding a coherent way to take the
    same date and compare just the number of minutes between them to find
    the difference. Like if I have an appointment at 9:30 a.m. and the app
    is loaded at 8 a.m., I need to know the number of minutes or hours and
    minutes until the appointment.

    I have looked at the dateutils module and it has many comparison
    methods, but they seem to only return the number of days or seconds.

    Any ideas would be great!

    Mike
     
    , May 9, 2007
    #1
    1. Advertising

  2. John Machin Guest

    On May 10, 7:34 am, wrote:
    > Hi,
    >
    > I am writing a reminder program for our Zimbra email client. One of
    > the requirements I was given was to automatically increment or
    > decrement the display to show something like the following:
    >
    > 5 minutes until appointment
    >
    > or
    >
    > 10 minutes past your appointment
    >
    > Either way, as each minute goes by, I need to increment it or
    > decrement it. I am having trouble finding a coherent way to take the
    > same date and compare just the number of minutes between them to find
    > the difference. Like if I have an appointment at 9:30 a.m. and the app
    > is loaded at 8 a.m., I need to know the number of minutes or hours and
    > minutes until the appointment.
    >
    > I have looked at the dateutils module and it has many comparison
    > methods, but they seem to only return the number of days or seconds.
    >


    Ermmm ... what's wrong with

    minutes = seconds / 60.0
    hours = minutes / 60.0

    ?
     
    John Machin, May 9, 2007
    #2
    1. Advertising

  3. Tim Golden Guest

    wrote:
    > I am writing a reminder program for our Zimbra email client. One of
    > the requirements I was given was to automatically increment or
    > decrement the display to show something like the following:
    >
    > 5 minutes until appointment
    >
    > or
    >
    > 10 minutes past your appointment
    >
    >
    > Either way, as each minute goes by, I need to increment it or
    > decrement it. I am having trouble finding a coherent way to take the
    > same date and compare just the number of minutes between them to find
    > the difference. Like if I have an appointment at 9:30 a.m. and the app
    > is loaded at 8 a.m., I need to know the number of minutes or hours and
    > minutes until the appointment.


    Not the most elegant piece of code on earth,
    but this piece of code works for me (cut-and-pasted
    directly from a working project, so doesn't
    *exactly* match your requirement).

    <code>
    def deltastamp (now, then):

    def pluralise (base, n):
    if n > 1:
    return "%d %ss" % (n, base)
    else:
    return "%d %s" % (n, base)

    if now > then:
    output_format = "%s ago"
    delta = now - then
    else:
    output_format = "in %s"
    delta = then - now

    days = delta.days
    if days <> 0:
    wks, days = divmod (days, 7)
    if wks > 0:
    output = pluralise ("wk", wks)
    else:
    output = pluralise ("day", days)
    else:
    mins, secs = divmod (delta.seconds, 60)
    hrs, mins = divmod (mins, 60)
    if hrs > 0:
    output = pluralise ("hr", hrs)
    elif mins > 0:
    output = pluralise ("min", mins)
    else:
    output = pluralise ("sec", secs)

    return output_format % output

    </code>

    TJG
     
    Tim Golden, May 10, 2007
    #3
  4. Guest

    On May 9, 5:12 pm, John Machin <> wrote:
    > On May 10, 7:34 am, wrote:
    >
    >
    >
    > > Hi,

    >
    > > I am writing a reminder program for our Zimbra email client. One of
    > > the requirements I was given was to automatically increment or
    > > decrement the display to show something like the following:

    >
    > > 5 minutes until appointment

    >
    > > or

    >
    > > 10 minutes past your appointment

    >
    > > Either way, as each minute goes by, I need to increment it or
    > > decrement it. I am having trouble finding a coherent way to take the
    > > same date and compare just the number of minutes between them to find
    > > the difference. Like if I have an appointment at 9:30 a.m. and the app
    > > is loaded at 8 a.m., I need to know the number of minutes or hours and
    > > minutes until the appointment.

    >
    > > I have looked at the dateutils module and it has many comparison
    > > methods, but they seem to only return the number of days or seconds.

    >
    > Ermmm ... what's wrong with
    >
    > minutes = seconds / 60.0
    > hours = minutes / 60.0
    >
    > ?


    I'm sure there is a hack for doing something like what you suggest,
    but it would be messy. The problem is that I get a datetime object
    returned and division really isn't something you can do to one of
    those objects. Besides, if I had seconds returned, I would want to
    multiply by 60, not divide.

    Maybe I misunderstand you.

    Mike
     
    , May 10, 2007
    #4
  5. Guest

    On May 10, 2:37 am, Tim Golden <> wrote:
    > wrote:
    > > I am writing a reminder program for our Zimbra email client. One of
    > > the requirements I was given was to automatically increment or
    > > decrement the display to show something like the following:

    >
    > > 5 minutes until appointment

    >
    > > or

    >
    > > 10 minutes past your appointment

    >
    > > Either way, as each minute goes by, I need to increment it or
    > > decrement it. I am having trouble finding a coherent way to take the
    > > same date and compare just the number of minutes between them to find
    > > the difference. Like if I have an appointment at 9:30 a.m. and the app
    > > is loaded at 8 a.m., I need to know the number of minutes or hours and
    > > minutes until the appointment.

    >
    > Not the most elegant piece of code on earth,
    > but this piece of code works for me (cut-and-pasted
    > directly from a working project, so doesn't
    > *exactly* match your requirement).
    >
    > <code>
    > def deltastamp (now, then):
    >
    > def pluralise (base, n):
    > if n > 1:
    > return "%d %ss" % (n, base)
    > else:
    > return "%d %s" % (n, base)
    >
    > if now > then:
    > output_format = "%s ago"
    > delta = now - then
    > else:
    > output_format = "in %s"
    > delta = then - now
    >
    > days = delta.days
    > if days <> 0:
    > wks, days = divmod (days, 7)
    > if wks > 0:
    > output = pluralise ("wk", wks)
    > else:
    > output = pluralise ("day", days)
    > else:
    > mins, secs = divmod (delta.seconds, 60)
    > hrs, mins = divmod (mins, 60)
    > if hrs > 0:
    > output = pluralise ("hr", hrs)
    > elif mins > 0:
    > output = pluralise ("min", mins)
    > else:
    > output = pluralise ("sec", secs)
    >
    > return output_format % output
    >
    > </code>
    >
    > TJG


    Thanks for the advice. I think this will work for me with some minor
    tweaking. If not, I will post again.

    Mike
     
    , May 10, 2007
    #5
  6. On Thu, 2007-05-10 at 11:52 -0700, wrote:
    > I'm sure there is a hack for doing something like what you suggest,
    > but it would be messy. The problem is that I get a datetime object
    > returned and division really isn't something you can do to one of
    > those objects. Besides, if I had seconds returned, I would want to
    > multiply by 60, not divide.


    If you subtract that datetime object from the current datetime, you'll
    get a timedelta object that gives you the number of days and seconds
    (and microseconds, if you care) between the two datetimes:

    >>> import datetime
    >>> dt1 = datetime.datetime(2007,5,1,12,0,0)
    >>> dt2 = datetime.datetime.now()
    >>> delta = dt2 - dt1
    >>> delta.days

    9
    >>> delta.seconds

    11219

    Now, if 60 seconds are one minute, 11219 seconds are how many minutes?
    (Answer left as an exercise for the reader.)

    Hope this helps,

    --
    Carsten Haese
    http://informixdb.sourceforge.net
     
    Carsten Haese, May 10, 2007
    #6
  7. Guest

    On May 10, 1:52 pm, wrote:
    > On May 9, 5:12 pm, John Machin <> wrote:
    >
    >
    >
    > > On May 10, 7:34 am, wrote:

    >
    > > > Hi,

    >
    > > > I am writing a reminder program for our Zimbra email client. One of
    > > > the requirements I was given was to automatically increment or
    > > > decrement the display to show something like the following:

    >
    > > > 5 minutes until appointment

    >
    > > > or

    >
    > > > 10 minutes past your appointment

    >
    > > > Either way, as each minute goes by, I need to increment it or
    > > > decrement it. I am having trouble finding a coherent way to take the
    > > > same date and compare just the number of minutes between them to find
    > > > the difference. Like if I have an appointment at 9:30 a.m. and the app
    > > > is loaded at 8 a.m., I need to know the number of minutes or hours and
    > > > minutes until the appointment.

    >
    > > > I have looked at the dateutils module and it has many comparison
    > > > methods, but they seem to only return the number of days or seconds.

    >
    > > Ermmm ... what's wrong with

    >
    > > minutes = seconds / 60.0
    > > hours = minutes / 60.0

    >
    > > ?

    >
    > I'm sure there is a hack for doing something like what you suggest,
    > but it would be messy. The problem is that I get a datetime object
    > returned and division really isn't something you can do to one of
    > those objects. Besides, if I had seconds returned, I would want to
    > multiply by 60, not divide.
    >
    > Maybe I misunderstand you.
    >
    > Mike


    Of course, after posting this, I felt very stupid...
     
    , May 10, 2007
    #7
  8. John Machin Guest

    On May 11, 4:52 am, wrote:
    > On May 9, 5:12 pm, John Machin <> wrote:
    >
    >
    >
    >
    >
    >
    >
    > > On May 10, 7:34 am, wrote:

    >
    > > > Hi,

    >
    > > > I am writing a reminder program for our Zimbra email client. One of
    > > > the requirements I was given was to automatically increment or
    > > > decrement the display to show something like the following:

    >
    > > > 5 minutes until appointment

    >
    > > > or

    >
    > > > 10 minutes past your appointment

    >
    > > > Either way, as each minute goes by, I need to increment it or
    > > > decrement it. I am having trouble finding a coherent way to take the
    > > > same date and compare just the number of minutes between them to find
    > > > the difference. Like if I have an appointment at 9:30 a.m. and the app
    > > > is loaded at 8 a.m., I need to know the number of minutes or hours and
    > > > minutes until the appointment.

    >
    > > > I have looked at the dateutils module and it has many comparison
    > > > methods, but they seem to only return the number of days or seconds.

    >
    > > Ermmm ... what's wrong with

    >
    > > minutes = seconds / 60.0
    > > hours = minutes / 60.0

    >
    > > ?

    >
    > I'm sure there is a hack for doing something like what you suggest,
    > but it would be messy. The problem is that I get a datetime object
    > returned and division really isn't something you can do to one of
    > those objects. Besides, if I had seconds returned, I would want to
    > multiply by 60, not divide.
    >
    > Maybe I misunderstand you.
    >


    Maybe it's mutual -- hack? messy? multiply? Where I come from, 180
    seconds is (180 / 60 = 3) minutes. 180 seconds * 60 is 10800 sixtieths-
    of-a-second, which appears to be travelling away from a solution to
    your problem.

    You have *TWO* datetime objects, (say) appt_dt and now_dt.

    delta =appt_dt - now_dt # delta is a timedelta object.
    # calculate difference in minutes
    mins = delta.days * 24 * 60 + delta.seconds // 60

    Have you not read Tim Golden's response?
     
    John Machin, May 10, 2007
    #8
  9. Guest

    On May 10, 2:45 pm, John Machin <> wrote:
    > On May 11, 4:52 am, wrote:
    >
    >
    >
    > > On May 9, 5:12 pm, John Machin <> wrote:

    >
    > > > On May 10, 7:34 am, wrote:

    >
    > > > > Hi,

    >
    > > > > I am writing a reminder program for our Zimbra email client. One of
    > > > > the requirements I was given was to automatically increment or
    > > > > decrement the display to show something like the following:

    >
    > > > > 5 minutes until appointment

    >
    > > > > or

    >
    > > > > 10 minutes past your appointment

    >
    > > > > Either way, as each minute goes by, I need to increment it or
    > > > > decrement it. I am having trouble finding a coherent way to take the
    > > > > same date and compare just the number of minutes between them to find
    > > > > the difference. Like if I have an appointment at 9:30 a.m. and the app
    > > > > is loaded at 8 a.m., I need to know the number of minutes or hours and
    > > > > minutes until the appointment.

    >
    > > > > I have looked at the dateutils module and it has many comparison
    > > > > methods, but they seem to only return the number of days or seconds.

    >
    > > > Ermmm ... what's wrong with

    >
    > > > minutes = seconds / 60.0
    > > > hours = minutes / 60.0

    >
    > > > ?

    >
    > > I'm sure there is a hack for doing something like what you suggest,
    > > but it would be messy. The problem is that I get a datetime object
    > > returned and division really isn't something you can do to one of
    > > those objects. Besides, if I had seconds returned, I would want to
    > > multiply by 60, not divide.

    >
    > > Maybe I misunderstand you.

    >
    > Maybe it's mutual -- hack? messy? multiply? Where I come from, 180
    > seconds is (180 / 60 = 3) minutes. 180 seconds * 60 is 10800 sixtieths-
    > of-a-second, which appears to be travelling away from a solution to
    > your problem.
    >
    > You have *TWO* datetime objects, (say) appt_dt and now_dt.
    >
    > delta =appt_dt - now_dt # delta is a timedelta object.
    > # calculate difference in minutes
    > mins = delta.days * 24 * 60 + delta.seconds // 60
    >
    > Have you not read Tim Golden's response?


    Yeah. I said I felt stupid. I'm sorry. I was looking at the problem
    from the wrong direction.

    Mike
     
    , May 10, 2007
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Lozzi

    Dates dates dates dates... SQL and ASP.NET

    David Lozzi, Sep 29, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    875
    Rob Schieber
    Sep 30, 2005
  2. PW

    Dates! Dates! Dates!

    PW, Aug 7, 2004, in forum: ASP General
    Replies:
    4
    Views:
    201
    Mark Schupp
    Aug 9, 2004
  3. Replies:
    1
    Views:
    207
    Jano Svitok
    Jul 17, 2007
  4. kellygreer1

    RFC-822 dates into Ruby dates

    kellygreer1, Jun 8, 2008, in forum: Ruby
    Replies:
    1
    Views:
    205
    Eric I.
    Jun 8, 2008
  5. Dr J R Stockton

    FAQ Dates; Opera Dates.

    Dr J R Stockton, Jun 18, 2007, in forum: Javascript
    Replies:
    0
    Views:
    163
    Dr J R Stockton
    Jun 18, 2007
Loading...

Share This Page