Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #24989

Re: simpler increment of time values?

Path csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <vlastimil.brom@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.031
X-Spam-Evidence '*H*': 0.94; '*S*': 0.00; '"""': 0.05; 'completeness': 0.07; 'def': 0.10; 'subject:values': 0.16; 'string': 0.17; 'instance': 0.17; 'skip:{ 20': 0.17; 'skip:" 30': 0.20; 'import': 0.21; 'received:209.85.214.174': 0.21; 'somebody': 0.23; 'raise': 0.24; 'allows': 0.25; 'header:In-Reply-To:1': 0.25; 'values': 0.26; 'skip:@ 10': 0.27; 'message-id:@mail.gmail.com': 0.27; 'initial': 0.28; 'skip:_ 10': 0.29; 'class': 0.29; 'seconds': 0.30; 'to:addr:python-list': 0.33; 'skip:d 20': 0.34; 'received:google.com': 0.34; 'thanks': 0.34; 'skip:f 40': 0.35; 'subject:?': 0.35; 'received:209.85': 0.35; 'skip:m 40': 0.36; 'should': 0.36; 'supporting': 0.37; 'received:209': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'skip:o 20': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'received:209.85.214': 0.39; 'skip:" 10': 0.40; 'header:Received:5': 0.40; 'further': 0.61; 'provide': 0.62; 'respect': 0.63; 'hours': 0.66; 'obtained': 0.71
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type; bh=SJ9Q4XFlGLxglK0VqNxaAjRBzMnxxkIF9Wd8NeJVgoY=; b=Vif9HZUjei8YVJ2D2TcGv8T9Bn/W29yAXFW1uiMh4R6DrWw+LbsbH7W0o5Ez9oFjQC +sbPQe+nglFC9d2VmAMTRLTf7Jho5HRmJK4tV0aXbxJvE/kuW3lB/NyDoBw9RAWk7CLj CNBSZp6s/+VXM8KSxJH9AK6iyP9LBJiQRSptybo71npFpS2m4mA4u+4JRcuhIXJVjz6E lOJCXk9tLTymxm7GFF576o7ySjhIjHYyAL9XG7NgE9K+ZF2rHcFLOp526zA9dIQg3V4y scddkjInfpRBtTheM0+6BWPeYmjBgHyd8jL9uobSrTknWAYjSjPFgZyuNXm3kw0emaKU hEEw==
MIME-Version 1.0
In-Reply-To <CAHzaPEMKGbUPhpfaB5bFkxJkMsmHrCzPZojYB7ZVO2r8P0MGAg@mail.gmail.com>
References <CAHzaPEO5D498zepGC0EVVURdUE7qxYJk6ksd5yS_Xj_hh5iiyQ@mail.gmail.com> <CAHzaPEMKGbUPhpfaB5bFkxJkMsmHrCzPZojYB7ZVO2r8P0MGAg@mail.gmail.com>
Date Fri, 6 Jul 2012 23:48:22 +0200
Subject Re: simpler increment of time values?
From Vlastimil Brom <vlastimil.brom@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1874.1341611306.4697.python-list@python.org> (permalink)
Lines 52
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1341611306 news.xs4all.nl 6915 [2001:888:2000:d::a6]:60401
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:24989

Show key headers only | View raw


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)))

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

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: simpler increment of time values? Vlastimil Brom <vlastimil.brom@gmail.com> - 2012-07-06 23:48 +0200

csiph-web