Re: simpler increment of time values?

Discussion in 'Python' started by Vlastimil Brom, Jul 6, 2012.

  1. Thanks to all for further comments!
    Just for completeness and in case somebody would like to provide some
    suggestions or corrections;
    the following trivial class should be able to deal with the initial
    requirement of adding or subtracting dateless time values
    (hour:minute).

    regards,
    vbr

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

    import re

    class TrivialTime(object):
    """
    Trivial, dateless, DST-less, TZN-less time in 24-hours cycle
    only supporting hours and minutes; allows addition and subtraction.
    """

    def __init__(self, hours=0, minutes=0):
    self.total_minutes = (int(hours) * 60 + int(minutes)) % (60 * 24)
    self.hours, self.minutes = divmod(self.total_minutes, 60)

    def __add__(self, other):
    return TrivialTime(minutes=self.total_minutes + other.total_minutes)

    def __sub__(self, other):
    return TrivialTime(minutes=self.total_minutes - other.total_minutes)

    def __repr__(self):
    return "TrivialTime({}, {})".format(self.hours, self.minutes)

    def __str__(self):
    return "{}.{:0>2}".format(self.hours, self.minutes)

    @staticmethod
    def fromstring(time_string, format_re=r"^([0-2]?\d?)[.:,-]\s*([0-5]\d)$"):
    """
    Returns a TrivialTime instance according to the data from the
    given string
    with respect to the regex time format (two parethesised groups
    for minutes and seconds respectively).
    """
    time_string_match = re.match(format_re, time_string)
    if not time_string_match:
    raise ValueError("Time data cannot be obtained from the
    given string and the format regex.")
    return TrivialTime(hours=int(time_string_match.group(1)),
    minutes=int(time_string_match.group(2)))

    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    Vlastimil Brom, Jul 6, 2012
    #1
    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. Vlastimil Brom

    simpler increment of time values?

    Vlastimil Brom, Jul 5, 2012, in forum: Python
    Replies:
    5
    Views:
    227
    Steven D'Aprano
    Jul 6, 2012
  2. Chris Angelico

    Re: simpler increment of time values?

    Chris Angelico, Jul 5, 2012, in forum: Python
    Replies:
    0
    Views:
    163
    Chris Angelico
    Jul 5, 2012
  3. Mark Lawrence

    Re: simpler increment of time values?

    Mark Lawrence, Jul 5, 2012, in forum: Python
    Replies:
    0
    Views:
    169
    Mark Lawrence
    Jul 5, 2012
  4. Jason Friedman

    Re: simpler increment of time values?

    Jason Friedman, Jul 5, 2012, in forum: Python
    Replies:
    0
    Views:
    156
    Jason Friedman
    Jul 5, 2012
  5. Devin Jeanpierre

    Re: simpler increment of time values?

    Devin Jeanpierre, Jul 5, 2012, in forum: Python
    Replies:
    0
    Views:
    177
    Devin Jeanpierre
    Jul 5, 2012
Loading...

Share This Page