Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #76335

Re: get the min date from a list

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <elearn2014@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.011
X-Spam-Evidence '*H*': 0.98; '*S*': 0.00; 'string.': 0.05; 'modifying': 0.07; 'sys': 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; 'bit': 0.19; 'trying': 0.19; 'finished': 0.19; 'thu,': 0.19; '>>>': 0.22; 'import': 0.22; 'aug': 0.22; 'header:User-Agent:1': 0.23; 'switched': 0.24; 'looks': 0.24; 'pass': 0.26; 'header:In-Reply- To:1': 0.27; 'to:2**1': 0.27; 'function': 0.29; 'skip:p 30': 0.29; 'am,': 0.29; 'converting': 0.30; 'subject:list': 0.30; 'skip:( 20': 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; 'adjust': 0.36; 'list.': 0.37; 'received:10': 0.37; 'message- id:@gmail.com': 0.38; 'to:addr:python-list': 0.38; 'rather': 0.38; 'that,': 0.38; 'does': 0.39; 'sure': 0.39; 'to:addr:python.org': 0.39; 'how': 0.40; 'ian': 0.60; 'skip:t 30': 0.61; "you're": 0.61; "you'll": 0.62; 'hear': 0.63; 'such': 0.63; 'more': 0.64; 'to:addr:gmail.com': 0.65; 'charset:windows-1252': 0.65; 'minutes': 0.67; 'mar': 0.68; 'hour': 0.70; 'subject:get': 0.81; 'glad': 0.83; '2014,': 0.84; '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=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=F1MkPWmQlgm+1p+6x4WqZV7N89XBDZkLRWTIJ8FZFG8=; b=Fe/Q61NCIrvX90XhSfxjEAPU/lE6Ukx4pLgFXQj5NZpVPyCiEUEOnwJk6vBjB9x05B duQD1orRa1CM5uHo+uhEwTZG/vTeFPFSo9kQZwzawCSAqe3qhMLX0BWgmNAIXq1UahCH wwbGi9rlWgVlICvRyluVvQTIFwBpSmOzxYteFTxOh0XcvFYMC7L/N5m9ts1MO4OfOUC9 ZGdIDi7Zn0mQje2plg87yNm0YUswkt87OLkfss3m/xTQ2qSCpPAUn8f7Rmr3BbQauVGa cgqPBrC+AyFTDnUUev0nL+8Lf+R9f3CH8ELrW4mH1iUp4ysBNSJp/OL3sOSoGhJkZ4rS /X/g==
X-Received by 10.70.22.163 with SMTP id e3mr5362760pdf.157.1408058640035; Thu, 14 Aug 2014 16:24:00 -0700 (PDT)
Date Fri, 15 Aug 2014 07:22:21 +0800
From luofeiyu <elearn2014@gmail.com>
User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.0
MIME-Version 1.0
To Ian Kelly <ian.g.kelly@gmail.com>, python-list@python.org
Subject Re: get the min date from a list
References <53ECC35C.80608@gmail.com> <CALwzidmMPCdzwgfyBWst_qYyqsicBUM1_m6YB3FvT_PxniXo5g@mail.gmail.com>
In-Reply-To <CALwzidmMPCdzwgfyBWst_qYyqsicBUM1_m6YB3FvT_PxniXo5g@mail.gmail.com>
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.13012.1408058650.18130.python-list@python.org> (permalink)
Lines 70
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1408058650 news.xs4all.nl 2912 [2001:888:2000:d::a6]:34540
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:76335

Show key headers only | View raw


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


Thread

Re: get the min date from a list luofeiyu <elearn2014@gmail.com> - 2014-08-15 07:22 +0800

csiph-web