stftime %z time conversion character

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Evan Klitzke

    #1

    stftime %z time conversion character

    Although it is not present in ANSI C, the GNU version of stftime
    supports the conversion character %z, which is a time offset from GMT.
    The four digit time offset is required in RFC 2822 dates/times, and is
    used by a number of other programs as well. I need to convert times
    that use this convention to python time representations , and because
    Python does not support the %z time conversion character I cannot
    simply use the time.strptime function.

    What solutions have people used for this? I'm currently thinking of
    creating a dict that maps four digit time offsets to the time zone
    name and then use the %Z token on that. Is there another (or better)
    way?

    --
    Evan Klitzke <evan@yelp.co m>
  • Paul Boddie

    #2
    Re: stftime %z time conversion character

    Evan Klitzke wrote:
    Although it is not present in ANSI C, the GNU version of stftime
    supports the conversion character %z, which is a time offset from GMT.
    The four digit time offset is required in RFC 2822 dates/times, and is
    used by a number of other programs as well. I need to convert times
    that use this convention to python time representations , and because
    Python does not support the %z time conversion character I cannot
    simply use the time.strptime function.
    See this patch for an idea of the scope of this problem:

    https://sourceforge.net/tracker/?fun...&group_id=5470
    What solutions have people used for this? I'm currently thinking of
    creating a dict that maps four digit time offsets to the time zone
    name and then use the %Z token on that. Is there another (or better)
    way?
    Apart from a patch which would allow the time module functions to
    produce the output you desire, an alternative might involve the
    datetime module and timezone-aware datetime objects:



    I refer indirectly to the pytz module in the above message:

    Download pytz for free. Python historical timezone library and database. pytz is a Python library that brings the IANA/Olson timezone database into the Python runtime, enabling accurate, cross-platform timezone calculations for all the world’s timezones. It addresses a major limitation of Python’s built-in datetime.tzinfo by providing consistent support for daylight-saving transitions, historical offset changes, and ambiguous times (such as the end of DST).


    Paul

    Comment

    • Evan Klitzke

      #3
      Re: stftime %z time conversion character

      On 6/19/07, Paul Boddie <paul@boddie.or g.ukwrote:
      Evan Klitzke wrote:
      Although it is not present in ANSI C, the GNU version of stftime
      supports the conversion character %z, which is a time offset from GMT.
      The four digit time offset is required in RFC 2822 dates/times, and is
      used by a number of other programs as well. I need to convert times
      that use this convention to python time representations , and because
      Python does not support the %z time conversion character I cannot
      simply use the time.strptime function.
      >
      See this patch for an idea of the scope of this problem:
      >
      https://sourceforge.net/tracker/?fun...&group_id=5470
      >
      What solutions have people used for this? I'm currently thinking of
      creating a dict that maps four digit time offsets to the time zone
      name and then use the %Z token on that. Is there another (or better)
      way?
      >
      Apart from a patch which would allow the time module functions to
      produce the output you desire, an alternative might involve the
      datetime module and timezone-aware datetime objects:
      >

      >
      I refer indirectly to the pytz module in the above message:
      >
      Download pytz for free. Python historical timezone library and database. pytz is a Python library that brings the IANA/Olson timezone database into the Python runtime, enabling accurate, cross-platform timezone calculations for all the world’s timezones. It addresses a major limitation of Python’s built-in datetime.tzinfo by providing consistent support for daylight-saving transitions, historical offset changes, and ambiguous times (such as the end of DST).

      >
      Paul
      Thanks Paul, your answer was very helpful.

      --
      Evan Klitzke <evan@yelp.co m>

      Comment

      • sergio

        #4
        Re: stftime %z time conversion character

        Evan Klitzke wrote:
        Although it is not present in ANSI C, the GNU version of stftime
        supports the conversion character %z, which is a time offset from GMT.
        The four digit time offset is required in RFC 2822 dates/times, and is
        used by a number of other programs as well. I need to convert times
        that use this convention to python time representations , and because
        Python does not support the %z time conversion character I cannot
        simply use the time.strptime function.
        >
        What solutions have people used for this? I'm currently thinking of
        creating a dict that maps four digit time offsets to the time zone
        name and then use the %Z token on that. Is there another (or better)
        way?
        >
        I had a similar problem, currently I had solved with a system call

        #The Linux Date Command: TZ=WET date -d "20070823" +%:z
        #example
        timezone = "WET"
        year = 2007
        month = 2
        day = 27
        #end of example
        cmd="TZ=%s /bin/date -d %.4d%.2d%.2d +%%z" % (timezone,year, month,day)
        print "DEBUG cmd=%s" % cmd
        handler=os.pope n(cmd, 'r', 1)
        output = "".join(handler .readlines()).s trip()
        tz=output[:3]+":"+output[3:]
        handler.close()
        print "DEBUG timezone=%sXX" % tz

        ----------------------------
        before I test timezone of my machines like this :
        import time
        tzstr=time.strf time("%Z",time. localtime())
        if tzstr == "WEST" :
        do something

        Hope that can help something

        Best regards,
        --
        Sérgio M. B.

        Comment

        Working...