Re: how to change the time string into number?
luofeiyu writes: > s="Aug" > > how can i change it into 8 with some python time module? months = (None, # dummy, to start month indices from 1 "Jan","Feb","Mar","Apr","May","Jun", "Jul","Aug","Sep","Oct","Nov","Dec" ) month_number = months.index(month_abbr) # month_abbr == "Aug" Note: - time.strptime(month_abbr, "%b").tm_mon may fail in non-English locale - list(calendar.month_abbr).index(month_abbr) is also locale-specific -- Akira -- https://mail.python.org/mailman/listinfo/python-list
Re: how to change the time string into number?
On Thu, 14 Aug 2014 14:52:17 +0800, luofeiyu wrote: > in the manual https://docs.python.org/3.4/library/time.html > > %zTime zone offset indicating a positive or negative time difference > 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]. > %ZTime 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? > > >>> 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? Please learn to use usenet properly. Comments go below the text they refer to. What version of python are you using? I know what version of the documentation you are looking at, but as I explained inj an earlier post, the implementation varies between different python versions, and for example python 2.7 strptime seems to completely ignore the %z in the format string, so again, what version of python are you using? To check your python version: $ python >>> import sys >>> sys.version will output something like: '2.7.3 (default, Feb 27 2014, 19:58:35) \n[GCC 4.6.3]' for Python 2.7 or: '3.2.3 (default, Feb 27 2014, 21:31:18) \n[GCC 4.6.3]' for Python 3.2. Again, I stress, we need to know what version of python you are using to help you! Did you run the code I posted? Did you get the same output as me? If you didn't, what was different. If you did get the same output, what do you think is wrong with it? -- Denis McMahon, denismfmcma...@gmail.com -- https://mail.python.org/mailman/listinfo/python-list
Re: how to change the time string into number?
On Thu, 14 Aug 2014 09:46:20 +0800, luofeiyu wrote: > s="Aug" > > how can i change it into 8 with some python time module? You don't need a time module for this, just use a dictionary: months = { "Jan" : 1, . , "Dec": 12 } num = months[s] print num Fill in the rest of the months dictionary yourself, it shouldn't be too hard. -- Denis McMahon, denismfmcma...@gmail.com -- https://mail.python.org/mailman/listinfo/python-list
Re: how to change the time string into number?
Dennis Lee Bieber writes: > "Android" /is/ the flavor > > Though Google has probably done some things to it that make it > "not-Linux". Android is definitely Linux, since that is the kernel Android runs. Remember that Linux is not an operating system; it is one part, the kernel. This is where it's very useful to have a distinct name to refer to the operating system. GNU is an operating system, Android is a completely different operating system. Both happen to have Linux as their kernel. -- \ “I went to San Francisco. I found someone's heart.” —Steven | `\Wright | _o__) | Ben Finney -- https://mail.python.org/mailman/listinfo/python-list
Re: how to change the time string into number?
In article , Dennis Lee Bieber wrote: > On Thu, 14 Aug 2014 17:47:00 +1000, Cameron Simpson > declaimed the following: > > > > >Your Android phone will be running some flavour of Linux I believe. Someone > >who > >has used one may correct me here. > > > "Android" /is/ the flavor > > Though Google has probably done some things to it that make it > "not-Linux". > > A closer description might be that the Android phone is running some > flavor of Android -- since the OEMs tend to put customized skins on the > user interface level. The OEM marketing folks call that "a product differentiator". Most everybody else calls it "crapware". -- https://mail.python.org/mailman/listinfo/python-list
Re: how to change the time string into number?
On 14/08/2014 02:46, luofeiyu wrote: s="Aug" how can i change it into 8 with some python time module? If all else fails, read the instructions, so start here https://docs.python.org/3/library/datetime.html#module-datetime -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence -- https://mail.python.org/mailman/listinfo/python-list
Re: how to change the time string into number?
On 14Aug2014 14:52, luofeiyu 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 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 DickensJohn Huffam 1812-1870 Hard Times [1854] -- https://mail.python.org/mailman/listinfo/python-list
Re: how to change the time string into number?
On 14Aug2014 15:30, luofeiyu wrote: import sys sys.version '3.4.0 (v3.4.0:04f714765c13, Mar 16 2014, 19:25:23) [MSC v.1600 64 bit (AMD64)]' First, please post in an interleaved style so that we can see your responses underneath the text to which they relate. Thanks. Ok, you have Python 3.4.0. And (I am guessing from the "MSC"), some 64 bit Windows install? You will need to look up the Microsoft documentation to see whather your C library "struct tm" supports timezone information. Your Android phone will be running some flavour of Linux I believe. Someone who has used one may correct me here. import time time.tzname ('China Standard Time', 'China Daylight Time') Ok. Have a look at time.timezone. That may help you. Cheers, Cameron Simpson On 8/14/2014 3:25 PM, Cameron Simpson wrote: On 14Aug2014 14:52, luofeiyu 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 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 DickensJohn Huffam 1812-1870 Hard Times [1854] -- https://mail.python.org/mailman/listinfo/python-list
Re: how to change the time string into number?
>>> import sys >>> sys.version '3.4.0 (v3.4.0:04f714765c13, Mar 16 2014, 19:25:23) [MSC v.1600 64 bit (AMD64)]' >>> import time >>> time.tzname ('China Standard Time', 'China Daylight Time') On 8/14/2014 3:25 PM, Cameron Simpson wrote: On 14Aug2014 14:52, luofeiyu 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 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 DickensJohn Huffam 1812-1870 Hard Times [1854] -- https://mail.python.org/mailman/listinfo/python-list
Re: how to change the time string into number?
Please don't top-post your response. Instead, interleave your response and remove irrelevant quoted material. Use the Interleaved style https://en.wikipedia.org/wiki/Posting_style#Interleaved_style>. luofeiyu writes: > in the manual https://docs.python.org/3.4/library/time.html > > %zTime zone offset […] > %ZTime 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). 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 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 -- https://mail.python.org/mailman/listinfo/python-list
Re: how to change the time string into number?
in the manual https://docs.python.org/3.4/library/time.html %z Time zone offset indicating a positive or negative time difference 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? >>> 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? On 8/14/2014 10:01 AM, Ben Finney wrote: luofeiyu writes: s="Aug" how can i change it into 8 with some python time module? What is your purpose here? If you want to parse a text value into a structured time object, don't do it piece by piece. Use the ‘time.strptime’ function. >>> import time >>> input_time_text = "14 Aug 2014" >>> input_time = time.strptime(input_text, "%d %b %Y") >>> input_time.tm_mon 8 -- https://mail.python.org/mailman/listinfo/python-list
Re: how to change the time string into number?
Tim Chase : > Or, if you want a more convoluted way: > > >>> import calendar as c > >>> [i for i, m in enumerate(c.month_abbr) if m == "Aug"].pop() > 8 Let's not forget the much simpler solutions: >>> def eight(x): return 8 ... >>> eight("Aug") 8 and: >>> 8 8 BTW, is this a bug: >>> import locale >>> locale.getlocale() ('de_DE', 'UTF-8') >>> import time >>> time.strptime("Dez", "%b").tm_mon Traceback (most recent call last): File "", line 1, in File "/usr/lib/python3.2/_strptime.py", line 482, in _strptime_time tt = _strptime(data_string, format)[0] File "/usr/lib/python3.2/_strptime.py", line 337, in _strptime (data_string, format)) ValueError: time data 'Dez' does not match format '%b' >>> time.strftime("%b", time.localtime(time.time() + 120 * 86400)) 'Dec' >>> time.strftime("%x") '08/14/14' After all, "%b" is documented as "Locale’s abbreviated month name." Anyway, "%b" *should* depend on the locale, so str[pf]time may not be suitable to deal with email dates, for example. Marko -- https://mail.python.org/mailman/listinfo/python-list
Re: how to change the time string into number?
In article <53ec2453$0$2299$426a7...@news.free.fr>, YBM wrote: > Le 14/08/2014 04:16, Tim Chase a écrit : > > On 2014-08-13 21:01, Tim Chase wrote: > >> On 2014-08-14 09:46, luofeiyu wrote: > >>> s="Aug" > >>> > >>> how can i change it into 8 with some python time module? > >> > >> >>> import time > >> >>> s = "Aug" > >> >>> time.strptime(s, "%b").tm_mon > >> 8 > >> > >> works for me. > > > > Or, if you want a more convoluted way: > > > > >>> import calendar as c > > >>> [i for i, m in enumerate(c.month_abbr) if m == "Aug"].pop() > > 8 > > it's a joke isn't it ? No, it's a song. If I could save time in a bottle The first thing that I'd like to do Is to make every month be an integer number And then I could count them with you. -- https://mail.python.org/mailman/listinfo/python-list
Re: how to change the time string into number?
On Wed, Aug 13, 2014 at 8:51 PM, YBM wrote: > BTW, why iterators does not have such an index method ? Because iterators don't support indexing. In order to support such a thing, it would have to exhaust the iterator. >>> iter(range(5))[3] Traceback (most recent call last): File "", line 1, in TypeError: 'range_iterator' object is not subscriptable The only methods you can rely upon an arbitrary iterator to have are __iter__ and __next__. -- https://mail.python.org/mailman/listinfo/python-list
Re: how to change the time string into number?
Le 14/08/2014 04:16, Tim Chase a écrit : On 2014-08-13 21:01, Tim Chase wrote: On 2014-08-14 09:46, luofeiyu wrote: s="Aug" how can i change it into 8 with some python time module? >>> import time >>> s = "Aug" >>> time.strptime(s, "%b").tm_mon 8 works for me. Or, if you want a more convoluted way: >>> import calendar as c >>> [i for i, m in enumerate(c.month_abbr) if m == "Aug"].pop() 8 it's a joke isn't it ? >>> import calendar as c >>> list(c.month_abbr).index('Aug') 8 BTW, why iterators does not have such an index method ? >>> iter(c.month_abbr).index('Aug') Traceback (most recent call last): File "", line 1, in AttributeError: 'iterator' object has no attribute 'index' -- https://mail.python.org/mailman/listinfo/python-list
Re: how to change the time string into number?
On 2014-08-13 21:01, Tim Chase wrote: > On 2014-08-14 09:46, luofeiyu wrote: > > s="Aug" > > > > how can i change it into 8 with some python time module? > > >>> import time > >>> s = "Aug" > >>> time.strptime(s, "%b").tm_mon > 8 > > works for me. Or, if you want a more convoluted way: >>> import calendar as c >>> [i for i, m in enumerate(c.month_abbr) if m == "Aug"].pop() 8 -tkc -- https://mail.python.org/mailman/listinfo/python-list
Re: how to change the time string into number?
On 2014-08-14 09:46, luofeiyu wrote: > s="Aug" > > how can i change it into 8 with some python time module? >>> import time >>> s = "Aug" >>> time.strptime(s, "%b").tm_mon 8 works for me. -tkc -- https://mail.python.org/mailman/listinfo/python-list
Re: how to change the time string into number?
luofeiyu writes: > s="Aug" > > how can i change it into 8 with some python time module? What is your purpose here? If you want to parse a text value into a structured time object, don't do it piece by piece. Use the ‘time.strptime’ function. >>> import time >>> input_time_text = "14 Aug 2014" >>> input_time = time.strptime(input_text, "%d %b %Y") >>> input_time.tm_mon 8 -- \“I knew it was a shocking thing to say, but … no-one has the | `\right to spend their life without being offended.” —Philip | _o__) Pullman, 2010-03-28 | Ben Finney -- https://mail.python.org/mailman/listinfo/python-list
Re: how to change the time string into number?
On Thu, Aug 14, 2014 at 11:46 AM, luofeiyu wrote: > s="Aug" > > how can i change it into 8 with some python time module? Is this homework? If not, let me set you some homework. Step 1: Read the docs for some Python time module. Step 2: See if it lets you do what you want. Step 3: Return to step 1 with a different module, until your problem is solved. ChrisA -- https://mail.python.org/mailman/listinfo/python-list