Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.009 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'modifying': 0.07; 'string': 0.09; 'falls': 0.09; 'function,': 0.09; 'mess': 0.09; 'parsing': 0.09; 'present,': 0.09; 'skip:t 60': 0.09; 'def': 0.12; 'anyway': 0.14; 'losing': 0.16; 'pythonic': 0.16; 'range.': 0.16; 'skip:[ 40': 0.16; 'subtraction': 0.16; 'utc.': 0.16; 'wrote:': 0.18; 'trying': 0.19; 'finished': 0.19; 'thu,': 0.19; 'import': 0.22; 'aug': 0.22; 'switched': 0.24; 'looks': 0.24; 'pass': 0.26; 'header:In-Reply-To:1': 0.27; 'skip:p 30': 0.29; 'am,': 0.29; 'converting': 0.30; 'subject:list': 0.30; 'message- id:@mail.gmail.com': 0.30; "i'm": 0.30; 'code': 0.31; '(although': 0.31; 'strip': 0.31; 'skip:t 40': 0.33; 'skip:d 20': 0.34; 'subject:the': 0.34; 'subject:from': 0.34; 'could': 0.34; 'something': 0.35; 'convert': 0.35; 'but': 0.35; 'received:google.com': 0.35; '14,': 0.36; 'list.': 0.37; 'to:addr :python-list': 0.38; 'rather': 0.38; 'that,': 0.38; 'sure': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'skip:t 30': 0.61; "you're": 0.61; "you'll": 0.62; 'such': 0.63; 'more': 0.64; 'minutes': 0.67; 'hour': 0.70; 'subject:get': 0.81; 'min': 0.84; 'rework': 0.84; 'single,': 0.84; 'doubling': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=+BOzflfl9fUEK2Cu7lHpNvnDYzAU3bKTyRWv1GGykwo=; b=DH5fk4IBbo/v+KB4SMy+KQa3MgU92v+z85OJP8c7+RLCiNJBsuuY8yd4SfOyeOoHW6 n9ZbNcBgDQP4YhMwGC3ildpGd6UKy0kyX/rQS8UA5h+sx7eIi9jFxwyyjTEYHc9VUzDW S7gILl0Ku9x9q0hHJGWiQD8z2oySE4ILjdh1Trb8MX1UotHebPIJt9V5YcPFbZr2RLh6 1Da3gbyw2MJPuzO4yoTIJQRnhb4tvyyaxluAg2Ct25+/6FfMIy/7MohitXd3mTaOn6vy Akx3gfisjqRlsndEq2QM81Fh1U1SlBi2QiLonHRkCZxcBWny1XA48kuDdFH2bOhCiMpz ZkZw== X-Received: by 10.70.35.207 with SMTP id k15mr11575525pdj.5.1408037139785; Thu, 14 Aug 2014 10:25:39 -0700 (PDT) MIME-Version: 1.0 In-Reply-To: <53ECC35C.80608@gmail.com> References: <53ECC35C.80608@gmail.com> From: Ian Kelly Date: Thu, 14 Aug 2014 11:24:59 -0600 Subject: Re: get the min date from a list To: Python Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 44 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1408037143 news.xs4all.nl 2932 [2001:888:2000:d::a6]:39902 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:76323 On Thu, Aug 14, 2014 at 8:10 AM, luofeiyu 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.