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


Groups > comp.lang.python > #24912

Re: simpler increment of time values?

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!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.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'output': 0.04; 'context': 0.05; 'ok.': 0.07; 'seemed': 0.07; 'missed': 0.09; 'ignoring': 0.09; 'minus': 0.09; 'output,': 0.09; 'robust': 0.09; 'times,': 0.13; 'count.': 0.16; 'epoch': 0.16; 'it),': 0.16; 'mark,': 0.16; 'naive': 0.16; 'reminding': 0.16; 'simplest': 0.16; 'subject:values': 0.16; 'thoughts?': 0.16; '>>>': 0.18; 'trying': 0.21; 'received:209.85.214.174': 0.21; 'converted': 0.22; '15,': 0.23; 'seems': 0.23; 'header:In-Reply-To:1': 0.25; 'message- id:@mail.gmail.com': 0.27; "doesn't": 0.28; '(possibly': 0.29; '(unless': 0.29; 'case,': 0.29; 'probably': 0.29; 'maybe': 0.29; 'seconds': 0.30; 'asked': 0.33; 'stands': 0.33; 'turns': 0.33; 'to:addr:python-list': 0.33; 'likely': 0.33; 'skip:d 20': 0.34; 'received:google.com': 0.34; 'thanks': 0.34; 'jason': 0.35; 'expected': 0.35; 'subject:?': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'but': 0.36; "didn't": 0.36; 'possible': 0.37; 'beyond': 0.37; 'correctly': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'received:209.85.214': 0.39; 'header:Received:5': 0.40; 'think': 0.40; 'your': 0.60; 'most': 0.61; 'real': 0.61; 'more': 0.63; 'hours': 0.66; 'stated': 0.69; 'calculations': 0.84; 'doable': 0.84
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=cUDOI2eqeNuhPePrtN2FLkMsszhcSrk/S611CKsLHG8=; b=kl2P8rwQpwX27Cz8uE/QgTwDr3X6Qc8zi1WxCq1/O7ALuCIujWWgec8WZGNGKBi3m/ NZBBK3JI8VN8ZFt83n8QwOEg5tw9L2IpknkRet5DACQmjU6NF7RIVwIj5O0eyUqZ2V/d KgcZ6jDbG8Wei4tUukIvI8Y7oXV+cRlCsy9lEV6qcgx2fvfKYB3JOmZhNATM3tFud0tD ex6pO2aDgken3rZN3czngtLiFNyNMnalEeCJZBW8KKAWko6YswYukXEpLEyyjUXOh2AG X/ILWdfTDRznCg8FONqv5kxn30u8nnt47SI5m6yyjztLOWSpCcdBJh1k6mnmpkRRAun7 ZFxw==
MIME-Version 1.0
In-Reply-To <CAHzaPEO5D498zepGC0EVVURdUE7qxYJk6ksd5yS_Xj_hh5iiyQ@mail.gmail.com>
References <CAHzaPEO5D498zepGC0EVVURdUE7qxYJk6ksd5yS_Xj_hh5iiyQ@mail.gmail.com>
Date Thu, 5 Jul 2012 15:18:09 +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.1822.1341494292.4697.python-list@python.org> (permalink)
Lines 56
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1341494292 news.xs4all.nl 6846 [2001:888:2000:d::a6]:35111
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:24912

Show key headers only | View raw


Many thanks to all for your suggestions!

@ChrisA
Yes, the calculations with seconds since the Unix epoch is very
convenient for real times, but trying to make it dateless seemed to
make it more complicated for me.

The expected output for the increments asked by Jason was already
correctly stated by Devin; i.e.: 12:45 plus 12 hours is 0:45 and 12:45
minus 13 hours is 23:45.

Thanks for reminding me of dateutil.relativedelta, Mark, I didn't
think of it in this context (I always thought, the "relative" stands
for time and date calculations with regard to the current time and
date). There doesn't seem to be a way to use dateless time either
(unless I missed it),
however, it turns out, that one can probably work with this naive
"times" like with deltas (possibly ignoring other units than hours and
minutes in the result):

>>> td = dateutil.relativedelta.relativedelta(hours=9, minutes=45) + dateutil.relativedelta.relativedelta(minutes=30)
>>> "%s.%s" % (td.hours, td.minutes)
'10.15'
>>>
Which is probably the simplest and the most robust way, I found sofar.
It likely isn't the expected use case for relativedelta, but it seems
to work ok. (Are there maybe some drawbacks I am missing?)
(Well I just found one possible pitfall , if floats are passed:
>>> td = dateutil.relativedelta.relativedelta(hours=9, minutes=45) + dateutil.relativedelta.relativedelta(minutes=30.5)
>>> "%s.%s" % (td.hours, td.minutes)
'10.0.15.5'
, but its beyond my current use case, and the validation can always be added.)


The same would be doable using the built in timedelta too, but there
are no hours and minutes in its output, hence these are to be
converted from the seconds count.

>>> dttd=datetime.timedelta(hours=9, minutes=45) + datetime.timedelta(minutes=30)
>>> dttd
datetime.timedelta(0, 36900)
>>> dttd.seconds
36900
>>> s = dttd.seconds
>>> h,s = divmod(s,3600)
>>> m,s = divmod(s,60)
>>> h,m,s
(10, 15, 0)
>>> "%s.%s" % (h, m)
'10.15'
>>>

Any thoughts?
 thanks again,

   vbr

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-05 15:18 +0200

csiph-web