Expressing time.

Discussion in 'Python' started by Sean, Jul 14, 2003.

  1. Sean

    Sean Guest

    I'm playing around times and dates. I'd like to determine the age of
    particular sets of data in a user friendly matter.

    I've got everything ready but dealing with time in a user friendly manner.

    What would be a good way to express (in python):
    time.localtime()+30 days
    time.localtime()+5 minutes
    time.localtime()+2 months
    time.localtime()-2 years

    I know I could probably just calculate out each of those in seconds but that
    seems unelegant and very unfriendly (not to mention prone to error).

    Is there any python facilities to make this an easier chore (aka
    time.localtime()+months(3))?
    (I included months as it is a special case in that you can't just
    arbitrarily calculate months in seconds without being relative to a
    particular month).

    --
    Sean
    Sean, Jul 14, 2003
    #1
    1. Advertising

  2. Sean

    Eddie Corns Guest

    "Sean" <> writes:

    >I'm playing around times and dates. I'd like to determine the age of
    >particular sets of data in a user friendly matter.


    >I've got everything ready but dealing with time in a user friendly manner.


    >What would be a good way to express (in python):
    > time.localtime()+30 days
    > time.localtime()+5 minutes
    > time.localtime()+2 months
    > time.localtime()-2 years


    >Is there any python facilities to make this an easier chore (aka
    >time.localtime()+months(3))?
    >(I included months as it is a special case in that you can't just
    >arbitrarily calculate months in seconds without being relative to a
    >particular month).


    mx.DateTime makes these sort of calculations easy. I have a similarish task
    in presenting users an easy way of specifying which day's log files to search
    (hence in this case it was always a date in the past but the principle would
    be similar). So I allow things like 'monday', 'tuesday' etc. and -n for n
    days ago and several other variations. The interesting one in this instance
    is:

    dm_reg = re.compile (r'^(-?\d\d?)[/.](-?\d\d?)$')
    this_day = today()

    # dd/mm (-ve dd means count from end of month, -mm is mm months ago)
    mt = re.match (dm_reg, user_input)
    if mt:
    dy,mon = map(int, mt.groups())
    if mon < 0:
    mon = this_day.month + mon
    if mon <= 0:
    mon = 12 + mon
    dt = DateTime (this_day.year, mon, dy)
    if dt > this_day:
    dt = DateTime (this_day.year-1, mon, dy)
    return dt

    Which allows eg 1/-2 for the first of whatever month was 2 months ago or even
    -1/-1 for the last day of the previous month. mx.DateTime is doing all the
    hard work. You could cook up something similar to parse eg "-2m" "+2y" etc.

    Possibly the new time stuff in 2.3 will do this also, I haven't looked at 2.3
    yet.

    Eddie
    Eddie Corns, Jul 14, 2003
    #2
    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. Rob Heersma

    [OWL] expressing relationships

    Rob Heersma, Jun 15, 2004, in forum: XML
    Replies:
    0
    Views:
    407
    Rob Heersma
    Jun 15, 2004
  2. Mike Wahler
    Replies:
    1
    Views:
    382
    Razmig K
    Apr 1, 2004
  3. Michel Rosien
    Replies:
    0
    Views:
    356
    Michel Rosien
    Apr 22, 2004
  4. mathieu

    Expressing dynamics in XML ?

    mathieu, Aug 10, 2009, in forum: XML
    Replies:
    9
    Views:
    1,259
    Joe Kesselman
    Aug 12, 2009
  5. Richard Kilmer

    Expressing intent in method calling...

    Richard Kilmer, Jun 8, 2004, in forum: Ruby
    Replies:
    8
    Views:
    171
    Robert Klemme
    Jun 9, 2004
Loading...

Share This Page