ISO with timezone

Discussion in 'Python' started by nik, Jan 29, 2008.

  1. nik

    nik Guest

    Hi,

    How does one express the time in ISO format with the timezone
    designator?

    what I want is YYYY-MM-DDThh:mm:ss.sTZD

    From the documentation I see:
    >>> from datetime import tzinfo, timedelta, datetime
    >>> class TZ(tzinfo):

    .... def utcoffset(self, dt): return timedelta(minutes=-399)
    ....
    >>> datetime(2002, 12, 25, tzinfo=TZ()).isoformat(' ')

    '2002-12-25 00:00:00-06:39'

    and I've also figured out:
    >>>datetime.datetime.fromtimestamp(time.time()).isoformat()[:-3]

    '2008-01-23T11:22:54.130'

    But can't figure out how to fit them together.

    Thank you,
    Nik
     
    nik, Jan 29, 2008
    #1
    1. Advertising

  2. Hello, nik.

    On Jan 28, 2008, at 21:03, nik wrote:

    > Hi,
    >
    > How does one express the time in ISO format with the timezone
    > designator?
    >
    > what I want is YYYY-MM-DDThh:mm:ss.sTZD
    >
    >> From the documentation I see:
    >>>> from datetime import tzinfo, timedelta, datetime
    >>>> class TZ(tzinfo):

    > ... def utcoffset(self, dt): return timedelta(minutes=-399)
    > ...
    >>>> datetime(2002, 12, 25, tzinfo=TZ()).isoformat(' ')

    > '2002-12-25 00:00:00-06:39'
    >
    > and I've also figured out:
    >>>> datetime.datetime.fromtimestamp(time.time()).isoformat()[:-3]

    > '2008-01-23T11:22:54.130'
    >
    > But can't figure out how to fit them together.
    >


    There is nothing there to 'fit together' - in the first example given,
    the datetime object has no time component specified, so it fills in
    default vaules of zero. The following should make this clear:

    >>> your_time = datetime(2008, 2, 29, 15, 30, 11, tzinfo=TZ())
    >>> print your_time

    2008-02-29 15:30:11-05:00
    >>> print your_time.isoformat('T')

    2008-02-29T15:30:11-05:00

    If you wish to append the NAME of the tzinfo object instead of its
    offset, that requires a bit more playing around (along with a properly
    defined tzinfo object - check out dateutil or pytz for a concrete
    implementation of tzinfo subclasses (i.e. timezones)), but the
    following would work:

    >>> print your_time.strftime('%Y-%m-%dT%H:%M:%S %Z')

    2008-02-29T15:30:11 EST

    For details on how the .strftime method works, see Python Standard
    Library, Section 14.2.

    I hope this helps!

    Nick Fabry





    > Thank you,
    > Nik
    > --
    > http://mail.python.org/mailman/listinfo/python-list
     
    Nicholas F. Fabry, Jan 29, 2008
    #2
    1. Advertising

  3. nik

    nik Guest

    Thanks,
    that does help and now I have:

    >>> from datetime import datetime, tzinfo, timedelta
    >>> import time
    >>> class TZ(tzinfo):

    .... def utcoffset(self,dt): return timedelta(seconds=time.timezone)
    ....
    >>> print datetime(2008,2,29,15,30,11,tzinfo=TZ()).isoformat()

    2008-02-29T15:30:11+8:00


    But what I want to know now it how to get the actual time into the
    expression instead of typing the 2008,2,29,15....
    So something like: >>> print
    datetime(time.gmtime(),tzinfo=TZ()).isoformat(), but that doesn't
    work.

    I realize that I could do:
    >>> t = time.gmtime()
    >>> print datetime(t[0],t[1],t[2],t[3],t[4],t[5],tzinfo=TZ()).isoformat()


    but I imagine there might be a cleaner way of doing this.

    Thanks,
    Nik


    On Jan 28, 9:10 pm, "Nicholas F. Fabry" <>
    wrote:
    > Hello, nik.
    >
    > On Jan 28, 2008, at 21:03, nik wrote:
    >
    >
    >
    > > Hi,

    >
    > > How does one express the time in ISO format with the timezone
    > > designator?

    >
    > > what I want is YYYY-MM-DDThh:mm:ss.sTZD

    >
    > >> From the documentation I see:
    > >>>> from datetime import tzinfo, timedelta, datetime
    > >>>> class TZ(tzinfo):

    > > ... def utcoffset(self, dt): return timedelta(minutes=-399)
    > > ...
    > >>>> datetime(2002, 12, 25, tzinfo=TZ()).isoformat(' ')

    > > '2002-12-25 00:00:00-06:39'

    >
    > > and I've also figured out:
    > >>>> datetime.datetime.fromtimestamp(time.time()).isoformat()[:-3]

    > > '2008-01-23T11:22:54.130'

    >
    > > But can't figure out how to fit them together.

    >
    > There is nothing there to 'fit together' - in the first example given,
    > the datetime object has no time component specified, so it fills in
    > default vaules of zero. The following should make this clear:
    >
    > >>> your_time = datetime(2008, 2, 29, 15, 30, 11, tzinfo=TZ())
    > >>> print your_time

    > 2008-02-29 15:30:11-05:00
    > >>> print your_time.isoformat('T')

    > 2008-02-29T15:30:11-05:00
    >
    > If you wish to append the NAME of the tzinfo object instead of its
    > offset, that requires a bit more playing around (along with a properly
    > defined tzinfo object - check out dateutil or pytz for a concrete
    > implementation of tzinfo subclasses (i.e. timezones)), but the
    > following would work:
    >
    > >>> print your_time.strftime('%Y-%m-%dT%H:%M:%S %Z')

    > 2008-02-29T15:30:11 EST
    >
    > For details on how the .strftime method works, see Python Standard
    > Library, Section 14.2.
    >
    > I hope this helps!
    >
    > Nick Fabry
    >
    > > Thank you,
    > > Nik
    > > --
    > >http://mail.python.org/mailman/listinfo/python-list


    >
     
    nik, Jan 29, 2008
    #3
  4. nik

    nik Guest

    On Jan 29, 10:56 am, nik <> wrote:
    > Thanks,
    > that does help and now I have:
    >
    > >>> from datetime import datetime, tzinfo, timedelta
    > >>> import time
    > >>> class TZ(tzinfo):

    >
    > ... def utcoffset(self,dt): return timedelta(seconds=time.timezone)
    > ...>>> print datetime(2008,2,29,15,30,11,tzinfo=TZ()).isoformat()
    >
    > 2008-02-29T15:30:11+8:00
    >
    > But what I want to know now it how to get the actual time into the
    > expression instead of typing the 2008,2,29,15....
    > So something like: >>> print
    > datetime(time.gmtime(),tzinfo=TZ()).isoformat(), but that doesn't
    > work.
    >
    > I realize that I could do:
    >
    > >>> t = time.localtime()
    > >>> print datetime(t[0],t[1],t[2],t[3],t[4],t[5],tzinfo=TZ()).isoformat()

    >
    > but I imagine there might be a cleaner way of doing this.
    >
    > Thanks,
    > Nik
    >
    > On Jan 28, 9:10 pm, "Nicholas F. Fabry" <>
    > wrote:
    >
    > > Hello, nik.

    >
    > > On Jan 28, 2008, at 21:03, nik wrote:

    >
    > > > Hi,

    >
    > > > How does one express the time in ISO format with the timezone
    > > > designator?

    >
    > > > what I want is YYYY-MM-DDThh:mm:ss.sTZD

    >
    > > >> From the documentation I see:
    > > >>>> from datetime import tzinfo, timedelta, datetime
    > > >>>> class TZ(tzinfo):
    > > > ... def utcoffset(self, dt): return timedelta(minutes=-399)
    > > > ...
    > > >>>> datetime(2002, 12, 25, tzinfo=TZ()).isoformat(' ')
    > > > '2002-12-25 00:00:00-06:39'

    >
    > > > and I've also figured out:
    > > >>>> datetime.datetime.fromtimestamp(time.time()).isoformat()[:-3]
    > > > '2008-01-23T11:22:54.130'

    >
    > > > But can't figure out how to fit them together.

    >
    > > There is nothing there to 'fit together' - in the first example given,
    > > the datetime object has no time component specified, so it fills in
    > > default vaules of zero. The following should make this clear:

    >
    > > >>> your_time = datetime(2008, 2, 29, 15, 30, 11, tzinfo=TZ())
    > > >>> print your_time

    > > 2008-02-29 15:30:11-05:00
    > > >>> print your_time.isoformat('T')

    > > 2008-02-29T15:30:11-05:00

    >
    > > If you wish to append the NAME of the tzinfo object instead of its
    > > offset, that requires a bit more playing around (along with a properly
    > > defined tzinfo object - check out dateutil or pytz for a concrete
    > > implementation of tzinfo subclasses (i.e. timezones)), but the
    > > following would work:

    >
    > > >>> print your_time.strftime('%Y-%m-%dT%H:%M:%S %Z')

    > > 2008-02-29T15:30:11 EST

    >
    > > For details on how the .strftime method works, see Python Standard
    > > Library, Section 14.2.

    >
    > > I hope this helps!

    >
    > > Nick Fabry

    >
    > > > Thank you,
    > > > Nik
    > > > --
    > > >http://mail.python.org/mailman/listinfo/python-list
     
    nik, Jan 29, 2008
    #4
  5. En Tue, 29 Jan 2008 16:56:18 -0200, nik <> escribi�:

    >>>> print datetime(2008,2,29,15,30,11,tzinfo=TZ()).isoformat()

    > 2008-02-29T15:30:11+8:00
    >
    > But what I want to know now it how to get the actual time into the
    > expression instead of typing the 2008,2,29,15....
    > So something like: >>> print
    > datetime(time.gmtime(),tzinfo=TZ()).isoformat(), but that doesn't
    > work.


    Are you looking for datetime.now()?

    --
    Gabriel Genellina
     
    Gabriel Genellina, Jan 29, 2008
    #5
  6. On Jan 29, 2008, at 13:56, nik wrote:

    > Thanks,
    > that does help and now I have:
    >
    >>>> from datetime import datetime, tzinfo, timedelta
    >>>> import time
    >>>> class TZ(tzinfo):

    > ... def utcoffset(self,dt): return timedelta(seconds=time.timezone)
    > ...
    >>>> print datetime(2008,2,29,15,30,11,tzinfo=TZ()).isoformat()

    > 2008-02-29T15:30:11+8:00
    >
    >
    > But what I want to know now it how to get the actual time into the
    > expression instead of typing the 2008,2,29,15....
    > So something like: >>> print
    > datetime(time.gmtime(),tzinfo=TZ()).isoformat(), but that doesn't
    > work.
    >
    > I realize that I could do:
    >>>> t = time.gmtime()
    >>>> print
    >>>> datetime(t[0],t[1],t[2],t[3],t[4],t[5],tzinfo=TZ()).isoformat()

    >
    > but I imagine there might be a cleaner way of doing this.
    >
    > Thanks,
    > Nik
    >


    No need for the ugliness! The constructor for class datetime has a
    method, .now() that returns the current date and time, as a naive
    datetime object (i.e. no tzinfo attached). Since you want an aware
    datetime object (one with a tzinfo object attached), you can do it
    simply by feeding .now the tzinfo object you want attached, as below:

    >>> print datetime.now(TZ()).isoformat('T')

    2008-01-29T23:43:16.809049-05:00

    See PSL, Sect. 5.1.4

    Dates and Times are a bit ugly in Python. Don't be discouraged, but
    you do need to understand them quite well to get bug-free code that
    plays with them.

    Nick Fabry




    >
    > On Jan 28, 9:10 pm, "Nicholas F. Fabry" <>
    > wrote:
    >> Hello, nik.
    >>
    >> On Jan 28, 2008, at 21:03, nik wrote:
    >>
    >>
    >>
    >>> Hi,

    >>
    >>> How does one express the time in ISO format with the timezone
    >>> designator?

    >>
    >>> what I want is YYYY-MM-DDThh:mm:ss.sTZD

    >>
    >>>> From the documentation I see:
    >>>>>> from datetime import tzinfo, timedelta, datetime
    >>>>>> class TZ(tzinfo):
    >>> ... def utcoffset(self, dt): return timedelta(minutes=-399)
    >>> ...
    >>>>>> datetime(2002, 12, 25, tzinfo=TZ()).isoformat(' ')
    >>> '2002-12-25 00:00:00-06:39'

    >>
    >>> and I've also figured out:
    >>>>>> datetime.datetime.fromtimestamp(time.time()).isoformat()[:-3]
    >>> '2008-01-23T11:22:54.130'

    >>
    >>> But can't figure out how to fit them together.

    >>
    >> There is nothing there to 'fit together' - in the first example
    >> given,
    >> the datetime object has no time component specified, so it fills in
    >> default vaules of zero. The following should make this clear:
    >>
    >>>>> your_time = datetime(2008, 2, 29, 15, 30, 11, tzinfo=TZ())
    >>>>> print your_time

    >> 2008-02-29 15:30:11-05:00
    >>>>> print your_time.isoformat('T')

    >> 2008-02-29T15:30:11-05:00
    >>
    >> If you wish to append the NAME of the tzinfo object instead of its
    >> offset, that requires a bit more playing around (along with a
    >> properly
    >> defined tzinfo object - check out dateutil or pytz for a concrete
    >> implementation of tzinfo subclasses (i.e. timezones)), but the
    >> following would work:
    >>
    >>>>> print your_time.strftime('%Y-%m-%dT%H:%M:%S %Z')

    >> 2008-02-29T15:30:11 EST
    >>
    >> For details on how the .strftime method works, see Python Standard
    >> Library, Section 14.2.
    >>
    >> I hope this helps!
    >>
    >> Nick Fabry
    >>
    >>> Thank you,
    >>> Nik
    >>> --
    >>> http://mail.python.org/mailman/listinfo/python-list

    >
    >>

    > --
    > http://mail.python.org/mailman/listinfo/python-list
     
    Nicholas F. Fabry, Jan 30, 2008
    #6
  7. nik

    Ben Finney Guest

    "Nicholas F. Fabry" <> writes:

    > The constructor for class datetime has a method, .now() that returns
    > the current date and time, as a naive datetime object (i.e. no
    > tzinfo attached).


    It's not "the constructor for class 'datetime'" that has that method;
    rather, the class 'datetime' has that method.

    (If anything is "the constructor for class 'datetime'", it's the
    '__new__' method -- or, some might argue, the '__init__' method -- and
    that doesn't fit what you say above.)

    > Dates and Times are a bit ugly in Python. Don't be discouraged, but
    > you do need to understand them quite well to get bug-free code that
    > plays with them.


    This is unfortunately true. Native datetime support is improving, but
    slowly.

    --
    \ "[T]he speed of response of the internet will re-introduce us |
    `\ to that from which our political systems have separated us for |
    _o__) so long, the consequences of our own actions." -- Douglas Adams |
    Ben Finney
     
    Ben Finney, Jan 30, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Franck DARRAS
    Replies:
    12
    Views:
    659
    Jim Higson
    Aug 23, 2004
  2. Alexei Polkhanov
    Replies:
    11
    Views:
    2,480
  3. asaf
    Replies:
    3
    Views:
    6,212
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=
    Sep 11, 2006
  4. axrock
    Replies:
    8
    Views:
    986
    Roedy Green
    Mar 25, 2009
  5. Ami
    Replies:
    5
    Views:
    302
Loading...

Share This Page