Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: Ben Finney Newsgroups: comp.lang.python Subject: Re: How do I add 18 seconds to an ISO-8601 String in Python? Date: Sun, 24 Jan 2016 12:55:31 +1100 Lines: 42 Message-ID: References: <8de7de19-9746-48f3-bded-d8bfb5c7dc22@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: news.uni-berlin.de v9FxB599qGBMQmvQdHUlbQ1FGJ7wRXs3avHr3hvx2ZKA== Cancel-Lock: sha1:a1m5ZF2oz7CWSS2PV+zTtbblSXw= Return-Path: 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; '16,': 0.03; 'subject:Python': 0.05; 'subject:How': 0.09; 'format?': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'timestamp': 0.09; 'timestamps': 0.09; '\xe2\x80\x94': 0.09; 'python': 0.10; '36,': 0.16; '8bit%:26': 0.16; 'increment': 0.16; 'received:80.91.229.3': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'subject:ISO': 0.16; 'subject:String': 0.16; 'string': 0.17; 'python?': 0.18; '>>>': 0.20; 'library': 0.20; 'parsing': 0.22; 'referring': 0.22; 'import': 0.24; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'skip:" 20': 0.26; 'separate': 0.27; 'arithmetic': 0.29; 'seconds': 0.31; 'skip:d 40': 0.32; 'url:python': 0.33; '8bit%:25': 0.33; 'focusing': 0.33; 'skip:d 20': 0.34; 'add': 0.34; 'text': 0.35; 'done': 0.35; 'robert': 0.35; 'url:org': 0.36; 'url:library': 0.36; 'to:addr:python-list': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'received:org': 0.37; 'subject:-': 0.39; 'to:addr:python.org': 0.40; 'url:3': 0.60; 'your': 0.60; 'world': 0.64; 'subject': 0.70; '_o__)': 0.84; 'bliss': 0.84; 'received:125': 0.84; 'subject:add': 0.91 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: jigong.madmonks.org X-Public-Key-ID: 0xAC128405 X-Public-Key-Fingerprint: 517C F14B B2F3 98B0 CB35 4855 B8B2 4C06 AC12 8405 X-Public-Key-URL: http://www.benfinney.id.au/contact/bfinney-pubkey.asc X-Post-From: Ben Finney User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux) X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:102062 Robert James Liguori writes: (I've corrected the Subject field. The standard you're referring to is ISO 8601, I believe.) > How do I add 18 seconds to this string in Python? > > 2000-01-01T16:36:25.000Z Two separate parts: * How do I get a timestamp object from a text representation in ISO 8601 format? * How do I add 18 seconds to a timestamp object? Parsing a text representation of a timestamp to get a timestamp object is done with ‘datetime.strptime’ from the Python standard library :: >>> import datetime >>> foo_timestamp_text = "2000-01-01T16:36:25.000Z" >>> iso8601_format = "%Y-%m-%dT%H:%M:%S.%fZ" >>> foo_timestamp = datetime.datetime.strptime(foo_timestamp_text, iso8601_format) >>> foo_timestamp datetime.datetime(2000, 1, 1, 16, 36, 25) Arithmetic on timestamps is done using the ‘datetime.timedelta’ type :: >>> increment = datetime.timedelta(seconds=18) >>> increment datetime.timedelta(0, 18) >>> foo_timestamp + increment datetime.datetime(2000, 1, 1, 16, 36, 43) -- \ “You don't change the world by placidly finding your bliss — | `\ you do it by focusing your discontent in productive ways.” | _o__) —Paul Z. Myers, 2011-08-31 | Ben Finney