Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #76335
| Date | 2014-08-15 07:22 +0800 |
|---|---|
| From | luofeiyu <elearn2014@gmail.com> |
| Subject | Re: get the min date from a list |
| References | <53ECC35C.80608@gmail.com> <CALwzidmMPCdzwgfyBWst_qYyqsicBUM1_m6YB3FvT_PxniXo5g@mail.gmail.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.13012.1408058650.18130.python-list@python.org> (permalink) |
I am glad to hear that it is no necessary to create a complicated my
function to change the time string.
But in my computer the timezone offset do not work for me.
I am in win7+python34.
>>> 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')
>>> time.strptime(t1,"%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)
>>> 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)
>>>
The %z does not work for me, how to adjust it my computer?
On 8/15/2014 1:24 AM, Ian Kelly wrote:
> On Thu, Aug 14, 2014 at 8:10 AM, luofeiyu <elearn2014@gmail.com> wrote:
>> I finished it ,but how to make it into more pythonic way such as
>>
>> min (dates, key = converter)
> The converter will be your changeToUnix function, but you'll need to
> rework it to convert a single, rather than the whole list.
>
>> def changeToUnix(times):
>> import time,calendar,re
>> time_list=[]
>> for time1 in times:
>> pat='(.+?)([-|+]\d{4})(\(?.*\)?)'
>> x=re.search(pat,time1)
>> time_part=x.groups()[0].strip()
>> tz_part=x.groups()[1]
>> tz_acc=x.groups()[2].strip().replace('(','').replace(')','')
>> num=int(tz_part[1:3])
>> if tz_acc in ["","UTC","CST","GMT","EST","CST","PST"]: num=num
>> if tz_acc in ["EDT"]: num=num+2
>> if tz_acc in ["CDT"]: num=num+1
>> if tz_acc in ["PDT"]: num=num-1
>> op=tz_part[0]
>> y=time.strptime(time_part,"%a, %d %b %Y %H:%M:%S")
>> if op=="-": hour=int(y.tm_hour)-num
>> if op=="+": hour=int(y.tm_hour)+num
>> time2=(y.tm_year,y.tm_mon,y.tm_mday,hour,y.tm_min,y.tm_sec)
>> time_list.append(calendar.timegm(time2))
>> return(time_list)
> This looks way overly complicated. Why are you trying to mess with the
> time zone offset? strptime has a %z format code for parsing that
> (although I would recommend using datetime.datetime.strptime since I'm
> not sure how well time.strptime supports it). By adding the offset to
> the hour like that, you could end up with an hour that falls outside
> the accepted range. And I think you have your addition and subtraction
> switched around anyway -- in effect you're doubling the time zone
> offset, not converting to UTC. Also you would be losing the minutes
> part of the time zone offset if you were to get something like +0545.
> I also don't understand why you're special-casing and modifying three
> of the time zones.
>
> All you need to do is strip the parenthesized timezone off the string
> if it's present, and pass the result to datetime.datetime.strptime.
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: get the min date from a list luofeiyu <elearn2014@gmail.com> - 2014-08-15 07:22 +0800
csiph-web