how to change the time string into number?

Ben Finney ben+python at benfinney.id.au
Thu Aug 14 03:22:09 EDT 2014


Please don't top-post your response. Instead, interleave your response
and remove irrelevant quoted material. Use the Interleaved style
<URL:https://en.wikipedia.org/wiki/Posting_style#Interleaved_style>.

luofeiyu <elearn2014 at gmail.com> writes:

> in the manual  https://docs.python.org/3.4/library/time.html
>
> %z 	Time zone offset […]
> %Z 	Time zone name (no characters if no time zone exists).

> t1='Sat, 09 Aug 2014  07:36:46  '
> time.strptime(t1,"%a, %d %b %Y %H:%M:%S ")
> time.struct_time(tm_year=2014, tm_mon=8, tm_mday=9, tm_hour=7,
> tm_min=36, tm_sec
> =46, tm_wday=5, tm_yday=221, tm_isdst=-1)

Your code examples will be easier to read if you follow PEP 8 (in this
example, spaces around the operators as described in the style guide).

> >>> t2='Sat, 09 Aug 2014  07:36:46  -0700'
> >>> time.strptime(t2,"%a, %d %b %Y %H:%M:%S %z")
> time.struct_time(tm_year=2014, tm_mon=8, tm_mday=9, tm_hour=7,
> tm_min=36, tm_sec
> =46, tm_wday=5, tm_yday=221, tm_isdst=-1)
>
> t1 and t2 is different time ,the timezone in t2 is -0700 ,why we get
> the same result?

The timezone in ‘t2’ will only be understood subject to the caveat:

    Support for the %Z directive is based on the values contained in
    tzname and whether daylight is true. Because of this, it is
    platform-specific except for recognizing UTC and GMT which are
    always known (and are considered to be non-daylight savings
    timezones).

    <URL:https://docs.python.org/3/library/time.html#time.strptime>

So you'll need to see what your Python implementation supports (see
‘time.tzname’).

The support for time zones is always a pain, because they *change*
rapidly, arbitrarily, and with very little warning. Because of this, the
Python standard library does not attempt to contain a timezone database,
since it would almost immediately be out of date.

Install the ‘pytz’ package to get the latest released timezone database
supported in Python <URL:https://pypi.python.org/pypi/pytz>.

-- 
 \      “It is better to have loft and lost than to never have loft at |
  `\                                               all.” —Groucho Marx |
_o__)                                                                  |
Ben Finney




More information about the Python-list mailing list