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


Groups > comp.lang.python > #102062

Re: How do I add 18 seconds to an ISO-8601 String in Python?

From Ben Finney <ben+python@benfinney.id.au>
Newsgroups comp.lang.python
Subject Re: How do I add 18 seconds to an ISO-8601 String in Python?
Date 2016-01-24 12:55 +1100
Message-ID <mailman.191.1453600551.15297.python-list@python.org> (permalink)
References <8de7de19-9746-48f3-bded-d8bfb5c7dc22@googlegroups.com>

Show all headers | View raw


Robert James Liguori <gliesian66@gmail.com> 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
<URL:https://docs.python.org/3/library/datetime.html#datetime.datetime.strptime>::

    >>> 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
<URL:https://docs.python.org/3/library/datetime.html#timedelta-objects>::

    >>> 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

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


Thread

How do I add 18 seconds ISO-8301 String in Python? Robert James Liguori <gliesian66@gmail.com> - 2016-01-23 16:54 -0800
  Re: How do I add 18 seconds to an ISO-8601 String in Python? Ben Finney <ben+python@benfinney.id.au> - 2016-01-24 12:55 +1100
    Re: How do I add 18 seconds to an ISO-8601 String in Python? Robert James Liguori <gliesian66@gmail.com> - 2016-01-23 18:01 -0800
    Re: How do I add 18 seconds to an ISO-8601 String in Python? Robert James Liguori <gliesian66@gmail.com> - 2016-01-23 18:03 -0800
    Re: How do I add 18 seconds to an ISO-8601 String in Python? Robert James Liguori <gliesian66@gmail.com> - 2016-01-23 18:22 -0800
      Re: How do I add 18 seconds to an ISO-8601 String in Python? Michael Torrie <torriem@gmail.com> - 2016-01-23 19:44 -0700
      Re: How do I add 18 seconds to an ISO-8601 String in Python? Ben Finney <ben+python@benfinney.id.au> - 2016-01-24 16:26 +1100

csiph-web