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


Groups > comp.lang.python > #24887 > unrolled thread

simpler increment of time values?

Started byVlastimil Brom <vlastimil.brom@gmail.com>
First post2012-07-05 02:29 +0200
Last post2012-07-06 02:34 +0000
Articles 6 — 4 participants

Back to article view | Back to comp.lang.python


Contents

  simpler increment of time values? Vlastimil Brom <vlastimil.brom@gmail.com> - 2012-07-05 02:29 +0200
    Re: simpler increment of time values? rurpy@yahoo.com - 2012-07-05 07:11 -0700
    Re: simpler increment of time values? rurpy@yahoo.com - 2012-07-05 07:11 -0700
    Re: simpler increment of time values? John Nagle <nagle@animats.com> - 2012-07-05 10:34 -0700
      Re: simpler increment of time values? rurpy@yahoo.com - 2012-07-05 11:15 -0700
        Re: simpler increment of time values? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-07-06 02:34 +0000

#24887 — simpler increment of time values?

FromVlastimil Brom <vlastimil.brom@gmail.com>
Date2012-07-05 02:29 +0200
Subjectsimpler increment of time values?
Message-ID<mailman.1803.1341448153.4697.python-list@python.org>
Hi all,
I'd like to ask about the possibilities to do some basic manipulation
on timestamps - such as incrementing a given time (hour.minute -
string) by some minutes.
Very basic notion of "time" is assumed, i.e. dateless,
timezone-unaware, DST-less etc.
I first thought, it would be possible to just add a timedelta to a
time object, but, it doesn't seem to be the case.

The code I came up with (using time and datetime modules) seems rather
convoluted and I would like to ask about some possible more
straightforward alternatives I missed.
The equivalent function (lacking validation) without the (date)time
libraries seems simple enough (for this limited and individual task).
Although it is probably mostly throw-away code, which seems to do what
I need, I'd be interested in better/more elegant... solutions.

# # #
import time
import datetime
import re

print re.sub(r"^0","", (datetime.datetime(*list(time.strptime("8.45",
"%H.%M"))[:6]) + datetime.timedelta(minutes=30)).strftime("%H.%M"))
# 9.15

# # # # # # # # #

def add_minutes(hour_min_str, separator=".", minutes_to_add=0):
    h, m = [int(s) for s in hour_min_str.split(separator)]
    sum_minutes = h * 60 + m + minutes_to_add
    h, m = divmod(sum_minutes, 60)
    h = h % 24
    return "%s%s%s" % (h, separator, m)

print add_minutes(hour_min_str="8.45", separator='.', minutes_to_add=30)
# 9.15

# # # # # # # # #

Is it true, that timedelta cannot be used with dateless time values?
(Is there some other possibility than the current one, where strptime
actually infers 1. 1. 1900?)
Is there some simpler way to adapt the incompatible output of strptime
as the input of datetime?
Is it possible to get one-digit hours formatted without the leading zero?

Thanks in advance for any suggestions or remarks;
           regards,
              Vlastimil Brom

[toc] | [next] | [standalone]


#24915

Fromrurpy@yahoo.com
Date2012-07-05 07:11 -0700
Message-ID<ba932a8b-35dc-4bc7-aaa9-419bef8f3dcd@googlegroups.com>
In reply to#24887
On Wednesday, July 4, 2012 6:29:10 PM UTC-6, Vlastimil Brom wrote:
> Hi all,
> I'd like to ask about the possibilities to do some basic manipulation
> on timestamps - such as incrementing a given time (hour.minute -
> string) by some minutes.
> Very basic notion of "time" is assumed, i.e. dateless,
> timezone-unaware, DST-less etc.
> I first thought, it would be possible to just add a timedelta to a
> time object, but, it doesn't seem to be the case.
> 
> The code I came up with (using time and datetime modules) seems rather
> convoluted and I would like to ask about some possible more
> straightforward alternatives I missed.
> The equivalent function (lacking validation) without the (date)time
> libraries seems simple enough (for this limited and individual task).
> Although it is probably mostly throw-away code, which seems to do what
> I need, I'd be interested in better/more elegant... solutions.
> 
> # # #
> import time
> import datetime
> import re
> 
> print re.sub(r"^0","", (datetime.datetime(*list(time.strptime("8.45",
> "%H.%M"))[:6]) + datetime.timedelta(minutes=30)).strftime("%H.%M"))
> # 9.15
> 
> # # # # # # # # #
> 
> def add_minutes(hour_min_str, separator=".", minutes_to_add=0):
>     h, m = [int(s) for s in hour_min_str.split(separator)]
>     sum_minutes = h * 60 + m + minutes_to_add
>     h, m = divmod(sum_minutes, 60)
>     h = h % 24
>     return "%s%s%s" % (h, separator, m)
> 
> print add_minutes(hour_min_str="8.45", separator='.', minutes_to_add=30)
> # 9.15
> 
> # # # # # # # # #
> 
> Is it true, that timedelta cannot be used with dateless time values?
> (Is there some other possibility than the current one, where strptime
> actually infers 1. 1. 1900?)
> Is there some simpler way to adapt the incompatible output of strptime
> as the input of datetime?
> Is it possible to get one-digit hours formatted without the leading zero?
> 
> Thanks in advance for any suggestions or remarks;
>            regards,
>               Vlastimil Brom

If it's any consolation, I had to add a small constant time 
delta to all the times in a video subtitles file and my code
ended up looking very much like yours.  What should have take
five minutes to write took several hours,

I remain surprised and disappointed that doing something so
simple (read time text into time object, add timedelta, print 
result) was so awkward in Python.

[toc] | [prev] | [next] | [standalone]


#24916

Fromrurpy@yahoo.com
Date2012-07-05 07:11 -0700
Message-ID<mailman.1824.1341497507.4697.python-list@python.org>
In reply to#24887
On Wednesday, July 4, 2012 6:29:10 PM UTC-6, Vlastimil Brom wrote:
> Hi all,
> I'd like to ask about the possibilities to do some basic manipulation
> on timestamps - such as incrementing a given time (hour.minute -
> string) by some minutes.
> Very basic notion of "time" is assumed, i.e. dateless,
> timezone-unaware, DST-less etc.
> I first thought, it would be possible to just add a timedelta to a
> time object, but, it doesn't seem to be the case.
> 
> The code I came up with (using time and datetime modules) seems rather
> convoluted and I would like to ask about some possible more
> straightforward alternatives I missed.
> The equivalent function (lacking validation) without the (date)time
> libraries seems simple enough (for this limited and individual task).
> Although it is probably mostly throw-away code, which seems to do what
> I need, I'd be interested in better/more elegant... solutions.
> 
> # # #
> import time
> import datetime
> import re
> 
> print re.sub(r"^0","", (datetime.datetime(*list(time.strptime("8.45",
> "%H.%M"))[:6]) + datetime.timedelta(minutes=30)).strftime("%H.%M"))
> # 9.15
> 
> # # # # # # # # #
> 
> def add_minutes(hour_min_str, separator=".", minutes_to_add=0):
>     h, m = [int(s) for s in hour_min_str.split(separator)]
>     sum_minutes = h * 60 + m + minutes_to_add
>     h, m = divmod(sum_minutes, 60)
>     h = h % 24
>     return "%s%s%s" % (h, separator, m)
> 
> print add_minutes(hour_min_str="8.45", separator='.', minutes_to_add=30)
> # 9.15
> 
> # # # # # # # # #
> 
> Is it true, that timedelta cannot be used with dateless time values?
> (Is there some other possibility than the current one, where strptime
> actually infers 1. 1. 1900?)
> Is there some simpler way to adapt the incompatible output of strptime
> as the input of datetime?
> Is it possible to get one-digit hours formatted without the leading zero?
> 
> Thanks in advance for any suggestions or remarks;
>            regards,
>               Vlastimil Brom

If it's any consolation, I had to add a small constant time 
delta to all the times in a video subtitles file and my code
ended up looking very much like yours.  What should have take
five minutes to write took several hours,

I remain surprised and disappointed that doing something so
simple (read time text into time object, add timedelta, print 
result) was so awkward in Python.

[toc] | [prev] | [next] | [standalone]


#24938

FromJohn Nagle <nagle@animats.com>
Date2012-07-05 10:34 -0700
Message-ID<jt4j6m$rhp$1@dont-email.me>
In reply to#24887
On 7/4/2012 5:29 PM, Vlastimil Brom wrote:
> Hi all,
> I'd like to ask about the possibilities to do some basic manipulation
> on timestamps - such as incrementing a given time (hour.minute -
> string) by some minutes.
> Very basic notion of "time" is assumed, i.e. dateless,
> timezone-unaware, DST-less etc.
> I first thought, it would be possible to just add a timedelta to a
> time object, but, it doesn't seem to be the case.

    That's correct.  A datetime.time object is a time within a day.
A datetime.date object is a date without a time.  A datetime.datetime
object contains both.

   You can add a datetime.timedelta object to a datetime.datetime
object, which will yield a datetime.datetime object.

   You can also call time.time(), and get the number of seconds
since the epoch (usually 1970-01-01 00:00:00 UTC). That's just
a number, and you can do arithmetic on that.

   Adding a datetime.time to a datetime.timedelta isn't that
useful.  It would have to return a value error if the result
crossed a day boundary.

				John Nagle

[toc] | [prev] | [next] | [standalone]


#24942

Fromrurpy@yahoo.com
Date2012-07-05 11:15 -0700
Message-ID<10c08425-e66d-4abc-958e-d36ccd216847@googlegroups.com>
In reply to#24938
On Thursday, July 5, 2012 11:34:16 AM UTC-6, John Nagle wrote:
>[...]
>    You can also call time.time(), and get the number of seconds
> since the epoch (usually 1970-01-01 00:00:00 UTC). That's just
> a number, and you can do arithmetic on that.
> 
>    Adding a datetime.time to a datetime.timedelta isn't that
> useful. 

It certainly is useful and I gave an obvious and real-
world example in my previous post.

> It would have to return a value error if the result
> crossed a day boundary.

Why?  When I turn the adjustment knob on my analog
clock it crosses the day boundary from 23:59 to 0:00
with no problem whatsoever.  Why is Python unable
to do what billions of clocks do? 

Instead I have to convert everything to seconds and
do the same math I would have done in fortran in 1980.
Phew.

Another example of Pythonic "purity beats practicality"

[toc] | [prev] | [next] | [standalone]


#24953

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2012-07-06 02:34 +0000
Message-ID<4ff64eb1$0$29988$c3e8da3$5496439d@news.astraweb.com>
In reply to#24942
On Thu, 05 Jul 2012 11:15:04 -0700, rurpy wrote:

> On Thursday, July 5, 2012 11:34:16 AM UTC-6, John Nagle wrote:
>>[...]
>>    You can also call time.time(), and get the number of seconds
>> since the epoch (usually 1970-01-01 00:00:00 UTC). That's just a
>> number, and you can do arithmetic on that.
>> 
>>    Adding a datetime.time to a datetime.timedelta isn't that
>> useful.
> 
> It certainly is useful and I gave an obvious and real- world example in
> my previous post.

Agreed.

A timedelta of less than one day magnitude should be usable with time 
objects, and wrap around at midnight. That's a clear and useful extension 
to the current functionality.

I can't see a feature request (rejected or otherwise) on the bug tracker. 
Perhaps you should raise one for Python 3.4. It will have a better chance 
of being accepted if you include a patch, or at least tests.

http://bugs.python.org/



-- 
Steven

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web