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


Groups > comp.lang.python > #17834

Re: modifying a time.struct_time

References <iigrr8-e96.ln1@satorlaser.homedns.org> <CAPTjJmrc4CLVotL=4JFrdyAZ9hstKSGXyjsyv2pjye4JVggK0Q@mail.gmail.com> <CANy1k1iRrDS2SwJewv_gEs_tHrRzbdaKSJf+8SE5d+CC0PFdcg@mail.gmail.com>
Date 2011-12-24 18:13 +1100
Subject Re: modifying a time.struct_time
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.4045.1324710802.27778.python-list@python.org> (permalink)

Show all headers | View raw


On Sat, Dec 24, 2011 at 5:04 PM, Jason Friedman <jason@powerpull.net> wrote:
> Not particularly elegant, but I believe accurate and relying only on
> the stated struct_time contract:

Funny! But a binary search would be better, I think.


t = time.time()
time1 = time.localtime(t)
print("Local time is {}.".format(time1))
est1 = t + 365*86400
est2 = est1 + 86400 # Assume that the year is between 365 and 366 days
long (widen this if assumption not valid)
while True:
   est = (est1 + est2) / 2
   time2 = time.localtime(t)
   cmp = is_local_time_different_by_one_year(time1, time2):
   if cmp<0: est1 = est
   elif cmp>0: est2 = est
   else:
       print("One year later is {}".format(time2))
       break

Could do with a few more improvements. Also, it requires that the
comparison function return -1, 0, or 1, in the same way that strcmp()
does (not a difficult enhancement, but I'm off to Christmas dinner
with the family so I'll leave it as an exercise for the reader).

ChrisA

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


Thread

modifying a time.struct_time Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2011-12-16 10:45 +0100
  Re: modifying a time.struct_time Mazen Harake <mazen.harake@gmail.com> - 2011-12-16 11:32 +0100
  Re: modifying a time.struct_time Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-12-16 10:44 +0000
    Re: modifying a time.struct_time Tim Golden <mail@timgolden.me.uk> - 2011-12-16 10:50 +0000
  Re: modifying a time.struct_time Chris Angelico <rosuav@gmail.com> - 2011-12-16 21:44 +1100
  Re: modifying a time.struct_time Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2011-12-16 14:32 +0100
    Re: modifying a time.struct_time Chris Angelico <rosuav@gmail.com> - 2011-12-17 01:24 +1100
  Re: modifying a time.struct_time Jason Friedman <jason@powerpull.net> - 2011-12-24 06:04 +0000
  Re: modifying a time.struct_time Chris Angelico <rosuav@gmail.com> - 2011-12-24 18:13 +1100

csiph-web