how to change the time string into number?

Cameron Simpson cs at zip.com.au
Thu Aug 14 03:25:49 EDT 2014


On 14Aug2014 14:52, luofeiyu <elearn2014 at gmail.com> wrote:
>in the manual  https://docs.python.org/3.4/library/time.html
>
>┌──┬──────────────────────────────────────────────────────────────────────┬─┐
>│  │Time zone offset indicating a positive or negative time difference    │ │
>│%z│from UTC/GMT of the form +HHMM or -HHMM, where H represents decimal   │ │
>│  │hour digits and M represents decimal minute digits [-23:59, +23:59].  │ │
>├──┼──────────────────────────────────────────────────────────────────────┼─┤
>│%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)
>
>>>> 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?

What you get back a struct_time, which is little more than the numeric values 
extracted from a time string. And as far as the text you have supplied in your 
example, those values are the same.

Regarding the difference, string in t2 has a time zone offset.

My Python 3.4 doco says (about struct_time):

   Changed in version 3.3: tm_gmtoff and tm_zone attributes are available on 
   platforms with C library supporting the corresponding fields in struct tm.

Judging by your output, your C library does not support the tm_gmtoff and 
tm_zone fields in its C library "struct tm".

Please:

   tell us what specific version of Python you are using

   tell us what OS you're running on

Then look up the localtime() or gmtime() functions for you C library and see 
what that documentation says about "struct tm", which is what they and the C 
library strptime() return.

>>>> t3='Sat, 09 Aug 2014  07:36:46  +0400'
>>>> time.strptime(t3,"%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)
>
>The Directive   %z  has no any effect here,what is the matter?

The directive allows the strptime parser to keep recognising text. Imagine, for 
example, that the timezone were embedded in the middle of the string for some 
reason.

It looks like you platform does not support storing the time zone information 
in the C library "struct tm", and therefore it does not get exposed to the 
Python interpreter.

Cheers,
Cameron Simpson <cs at zip.com.au>

What I want is Facts. Teach these boys and girls nothing but Facts.  Facts
alone are wanted in life. Plant nothing else, and root out everything else.
         - Charles Dickens    John Huffam   1812-1870  Hard Times [1854]



More information about the Python-list mailing list