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


Groups > comp.lang.python > #17345

Re: modifying a time.struct_time

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <mazen.harake@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'bug': 0.02; '"if': 0.04; 'attribute': 0.07; 'transform': 0.07; 'accordingly': 0.09; 'assert': 0.09; 'cheers!': 0.09; 'derived': 0.09; 'dict': 0.09; 'expected.': 0.09; 'immutable': 0.09; 'relies': 0.09; 'tuple': 0.09; 'api': 0.09; 'argument': 0.15; 'subject:skip:t 10': 0.15; 'actualy': 0.16; 'bases,': 0.16; 'class:': 0.16; 'delegating': 0.16; 'eckhardt': 0.16; 'guaranteed.': 0.16; 'metaclass': 0.16; 'seconds,': 0.16; 'sequence,': 0.16; 'ugly.': 0.16; 'cc:addr :python-list': 0.16; 'this:': 0.16; 'wrote:': 0.18; '>>>': 0.18; 'convert': 0.19; 'seems': 0.20; 'cc:no real name:2**0': 0.20; 'trying': 0.21; 'seconds': 0.21; 'header:In-Reply-To:1': 0.22; 'indices': 0.23; 'cc:2**0': 0.24; 'code': 0.25; 'suggestion': 0.26; "i'm": 0.26; 'tried': 0.27; 'received:209.85.220': 0.27; 'not.': 0.28; 'url:mailman': 0.28; 'message-id:@mail.gmail.com': 0.28; 'pass': 0.29; 'accessible': 0.29; 'looks': 0.29; "skip:' 10": 0.29; 'problem': 0.29; 'cc:addr:python.org': 0.29; 'second': 0.29; 'error': 0.29; 'least': 0.30; 'anyone': 0.31; 'does': 0.32; 'hi,': 0.32; 'wondering': 0.32; 'list': 0.32; 'url:listinfo': 0.32; 'there': 0.33; 'object': 0.33; 'match': 0.34; 'calling': 0.34; 'approach.': 0.34; 'sense,': 0.34; 'test': 0.35; 'url:python': 0.36; 'uses': 0.36; 'acceptable': 0.37; 'sequence': 0.37; 'members': 0.37; 'but': 0.37; 'received:google.com': 0.37; 'could': 0.37; 'using': 0.38; 'back.': 0.38; 'easiest': 0.38; 'allows': 0.38; 'some': 0.38; 'received:209.85': 0.38; 'e.g.': 0.39; 'fail': 0.39; 'skip:\xa0 10': 0.39; 'represent': 0.39; 'url:org': 0.39; 'should': 0.39; 'received:209': 0.40; '8bit%:8': 0.40; 'type': 0.61; '2011': 0.61; 'back': 0.62; 'order': 0.62; 'skip:1 10': 0.63; '2012': 0.67; 'dict,': 0.84; 'skip:y 30': 0.84; 'ugly,': 0.84; 'prone': 0.91
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; bh=Pnj9kBgrGY5Xk86V0bCuUSvzMMV9dHBZ5U3EwCNu0cE=; b=ZkXrfJi7MtJEmQiq4U6Sspilm2jWd3ivomWZhhdwO4nFNPtd8l80x/b9ViY3C/FhtG ZUw1O8iyi2y3WwuwLbbxUa9E/fM8GmPwfXuq7ax1Z0MWOn9vi3yP6KX7RYTtXKNT82WV 1uTJ3Fj20Cnqi0qnJwbPS0CH6l5M5SJaIMJoU=
MIME-Version 1.0
In-Reply-To <iigrr8-e96.ln1@satorlaser.homedns.org>
References <iigrr8-e96.ln1@satorlaser.homedns.org>
Date Fri, 16 Dec 2011 11:32:13 +0100
Subject Re: modifying a time.struct_time
From Mazen Harake <mazen.harake@gmail.com>
To Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com>
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
Cc python-list@python.org
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.3720.1324031536.27778.python-list@python.org> (permalink)
Lines 107
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1324031536 news.xs4all.nl 6899 [2001:888:2000:d::a6]:59940
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:17345

Show key headers only | View raw


Hi,

Easiest way is to change the time to seconds, add as many seconds as a
year/month/week/day/hour/minutes represent and then transform it back.

E.g.

>>> time.time()
1324031491.026137
>>> time.time() + 3600 # Add an hour
1324035105.082003
>>> time.gmtime(time.time() + 3600)
time.struct_time(tm_year=2011, tm_mon=12, tm_mday=16, tm_hour=11,
tm_min=31, tm_sec=57, tm_wday=4, tm_yday=350, tm_isdst=0)
>>>

On 16 December 2011 10:45, Ulrich Eckhardt
<ulrich.eckhardt@dominolaser.com> wrote:
> Hi!
>
> I'm trying to create a struct_time that is e.g. one year ahead or a month
> back in order to test some parsing/formatting code with different dates.
>
> Now, the straightforward approach is
>
>  t = time.localtime()
>  t.tm_year += 1
>
> This fails with "TypeError: readonly attribute". This kind-of makes sense,
> as an immutable object allows you to use it as key in a dict.
>
>
> The second approach is this:
>
>  l = list(t) # convert to a sequence
>  l[0] += 1 # increment year
>  t = time.struct_time(l) # convert to a struct_time
>
> This works but is ugly, because the code relies on the order inside the list
> and uses magic numbers to access them. The order is AFAICT not accessible
> programmatically but only documented, and not even in a way that makes clear
> that it is part of the API and as such actualy guaranteed. I could try to
> assert that the indices match using "if l[0] is t.tm_year", but this is
> still ugly.
>
>
> The next approach I tried was to simply create a derived class:
>
>  class my_time(time.struct_time):
>      pass
>
> This fails again with "TypeError: Error when calling the metaclass bases,
> type 'time.struct_time' is not an acceptable base type. I could try to
> encapsulate a struct_time and delegate attribute access to it in order to do
> this, but it also seems overkill. Also, using an immutable type as a
> baseclass and delegating access to members seems like hackery to me, prone
> to fail in situations where it is least expected.
>
>
> Then I tried duck typing. If it quacks like a duck, it better not be a
> crocodile! This looks like this:
>
>  struct my_time(object): pass
>  t = my_time()
>  t.tm_year = 2012
>  t.tm_month = 12
>  t.tm... # other fields accordingly
>  time.mktime(t)
>
> This fails with "TypeError: argument must be 9-item sequence, not my_time".
> I thought about using a collections.namedtuple, because a namedtuple is a
> tuple and therefore also a sequence, but that only leads me back to the
> problem that time.mktime() takes a sequence and the order of the sequence is
> not accessible programmatically.
>
>
> A last approach was to convert the thing to a dict and back. Alas, there is
> no conversion to a dict, otherwise
>
>  d = dict(t)
>  d['tm_year'] += 1
>  t = time.struct_time(d)
>
> would have been a straightforward approach.
>
>
> Does anyone have a suggestion how to solve this elegantly and pythonically?
> Also, what I'm wondering is if the lack of a clear way should be considered
> a bug or not.
>
>
> Cheers!
>
> Uli
>
> --
> http://mail.python.org/mailman/listinfo/python-list

Back to comp.lang.python | Previous | NextPrevious in thread | Next 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