def timestr2secs_utc(timestr): """ Convert a timestring to UTC (=GMT) seconds. The format is either one of these two: '20020702100000 CDT' '200209080000 +0100' """ # This is either something like 'EDT', or '+1' try: tval, tz = timestr.split() except ValueError: tval = timestr tz = str(-time.timezone / 3600) if tz == 'CET': tz = '+1' # Is it the '+1' format? if tz[0] == '+' or tz[0] == '-': tmTuple = (int(tval[0:4]), int(tval[4:6]), int(tval[6:8]), int(tval[8:10]), int(tval[10:12]), 0, -1, -1, -1) secs = calendar.timegm(tmTuple) adj_neg = int(tz) >= 0 try: min = int(tz[3:5]) except ValueError: # sometimes the mins are missing :-( min = 0 adj_secs = int(tz[1:3]) * 3600 + min * 60 if adj_neg: secs -= adj_secs else: secs += adj_secs else: # No, use the regular conversion ## WARNING! BUG HERE! # The line below is incorrect; the strptime.strptime function doesn't # handle time zones. There is no obvious function that does. Therefore # this bug is left in for someone else to solve. try: secs = time.mktime(strptime.strptime(timestr, xmltv.date_format)) except ValueError: timestr = timestr.replace('EST', '') secs = time.mktime(strptime.strptime(timestr, xmltv.date_format)) return secs
def getRunTimeFILE(fname): Load(Filename=fname,OutputWorkspace='Dummy') wksp=mtd['Dummy'] #[date_ini,time_ini]= ini=wksp.getRun(). get('run_start').value #[date_fin,time_fin]= fin=wksp.getRun(). get('run_end').value try: strptime = time.strptime except AttributeError: from strptime import strptime #print strptime("31 Nov 00", "%d %b %y") #print strptime("1 Jan 70 1:30pm", "%d %b %y %I:%M%p") return (time.mktime(strptime(fin, "%Y-%m-%dT%H:%M:%S"))-time.mktime(strptime(ini, "%Y-%m-%dT%H:%M:%S")))/3600
def get_runtime_file(fname): load(Filename=fname, OutputWorkspace='Dummy') wksp = mtd['Dummy'] # [date_ini,time_ini]= ini = wksp.getRun().get('run_start').value # [date_fin,time_fin]= fin = wksp.getRun().get('run_end').value try: strptime = time.strptime except AttributeError: from strptime import strptime # print strptime("31 Nov 00", "%d %b %y") # print strptime("1 Jan 70 1:30pm", "%d %b %y %I:%M%p") return (time.mktime(strptime(fin, "%Y-%m-%dT%H:%M:%S")) - time.mktime(strptime(ini, "%Y-%m-%dT%H:%M:%S"))) / 3600
def getRunTime(instrument='HRPD', runno=0): inst=instrument[0:3] cycle=rawpath(str(runno), inst=inst) fname=cycle+'/'+inst+str(runno)+'.raw' Load(Filename=fname,OutputWorkspace='Dummy') wksp=mtd['Dummy'] #[date_ini,time_ini]= ini=wksp.getRun(). get('run_start').value #[date_fin,time_fin]= fin=wksp.getRun(). get('run_end').value try: strptime = time.strptime except AttributeError: from strptime import strptime #print strptime("31 Nov 00", "%d %b %y") #print strptime("1 Jan 70 1:30pm", "%d %b %y %I:%M%p") return (time.mktime(strptime(fin, "%Y-%m-%dT%H:%M:%S"))-time.mktime(strptime(ini, "%Y-%m-%dT%H:%M:%S")))/3600
def get_runtime(instrument='HRPD', runno=0): inst = instrument[0:3] cycle = rawpath(str(runno), inst=inst) fname = cycle + '/' + inst + str(runno) + '.raw' load(Filename=fname, OutputWorkspace='Dummy') wksp = mtd['Dummy'] # [date_ini,time_ini]= ini = wksp.getRun().get('run_start').value # [date_fin,time_fin]= fin = wksp.getRun().get('run_end').value try: strptime = time.strptime except AttributeError: from strptime import strptime # print strptime("31 Nov 00", "%d %b %y") # print strptime("1 Jan 70 1:30am", "%d %b %y %I:%M%p") return (time.mktime(strptime(fin, "%Y-%m-%dT%H:%M:%S")) - time.mktime(strptime(ini, "%Y-%m-%dT%H:%M:%S"))) / 3600
def loopLogs(): totalBytes = 0 totalSeconds = 0 totalFiles = 0 apppath = os.path.dirname(os.path.abspath(sys.argv[0])) indexMatch = re.compile("^threadlog_[0-9]+\.log$") fileMatch = re.compile("uploaded totally ([0-9]+) files") byteMatch = re.compile("uploaded totally ([0-9]+) bytes") timeFormat = "%Y-%m-%d %H:%M:%S" theStartTime = datetime.datetime.max theEndTime = datetime.datetime.min for f in os.listdir(apppath): if indexMatch.match(f): fobj = open(os.path.join(apppath, f)) lines = fobj.readlines() fobj.close() m = fileMatch.match(lines[-1]) if m: totalFiles = totalFiles + int(m.group(1)) m = byteMatch.match(lines[-2]) if m: totalBytes = totalBytes + int(m.group(1)) startTimeTup = strptime.strptime(lines[-4].strip(), timeFormat) endTimeTup = strptime.strptime(lines[-3].strip(), timeFormat) startTime = datetime.datetime(startTimeTup[0], startTimeTup[1], startTimeTup[2], startTimeTup[3], startTimeTup[4], startTimeTup[5]) if startTime < theStartTime: theStartTime = startTime endTime = datetime.datetime(endTimeTup[0], endTimeTup[1], endTimeTup[2], endTimeTup[3], endTimeTup[4], endTimeTup[5]) if endTime > theEndTime: theEndTime = endTime deltaTime = theEndTime - theStartTime totalSeconds = totalSeconds + deltaTime.days * 24 * 3600 + deltaTime.seconds print "Upload started from: \t%s" % theStartTime.strftime(timeFormat) print "Upload stopped at: \t%s" % theEndTime.strftime(timeFormat) print "Upload last for:\t", deltaTime print "Total files: \t%s" % number_format(totalFiles) print "Total bytes: \t%s" % number_format(totalBytes) print "Total seconds: \t%s" % number_format(totalSeconds) print "Speed: %d bytes/sec" % (totalBytes / totalSeconds) print "Speed: %d files/sec" % (totalFiles / totalSeconds)
''' 将字符串转换为时间对象 ''' # 使用 time.strptime 函数解析时间 # time 模块包含了 strptime 函数, 它的作用与 strftime 相反. # 给定一个字符串和模式, 它返回相应的时间对象 import time try: strptime = time.strptime except AttributeError: from strptime import strptime print(strptime('30 Nov 00','%d %b %y')) print(strptime('1 Jan 70 1:30pm','%d %b %y %I:%M%p')) ''' time.struct_time(tm_year=2000, tm_mon=11, tm_mday=30, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=335, tm_isdst=-1) time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=13, tm_min=30, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=-1) ''' print('-----------------') # strptime 实现 # 对于没有提供标准实现的平台, 下面提供了一个不完全的实现. import re MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] SPEC = { # map formatting code to a regular expression fragment "%a": "(?P<weekday>[a-z]+)", "%A": "(?P<weekday>[a-z]+)",
import time # make sure we have a strptime function! try: strptime = time.strptime except AttributeError: from strptime import strptime print(strptime("30 Nov 00", "%d %b %y")) print(strptime("1 Jan 70 1:30pm", "%d %b %y %I:%M%p"))
#"d:\\data\\fileter_processedChekins.txt" #J:\\checkin\\filter_allCheckinNewYork.txt fileName = "d:\\data\\fileter_processedChekins.txt" f=open(fileName,'r') #trainf=open("d:\\trainfilter2.txt",'w+') #testf=open("d:\\testfilter2.txt",'w+') #逐行处理数据 #第一行初始化,为位置关系等比较 lastline=f.readline() arr=lastline.split('\t') lasttime=time.mktime(strptime(arr[1],"20%y-%m-%dT%H:%M:%SZ")) lastuserid=arr[0] lastlocid=arr[4] timeDic={0:0} i=0 while True: newline=f.readline() if newline=='': break; arr=newline.split('\t') tm=strptime(arr[1],"20%y-%m-%dT%H:%M:%SZ") newtime=time.mktime(tm) #统计每个签到的时间分布 yearmon = int(str(tm.tm_year)+str(tm.tm_mon))
def initFromDirectoryListing(self, dirname, line): mode = 0 isSymlink = False self.dirname = dirname link_target = None if not line: log.debug("initFromDirectoryListing: No line to process!") return False # Check the first character of the listing is a unix style if line[0] in self._possible_first_chars_for_unix_listing: # UNIX-style listing, without inum and without blocks */ # "-rw-r--r-- 1 root other 531 Jan 29 03:26 README" */ # "dr-xr-xr-x 2 root other 512 Apr 8 1994 etc" */ # "dr-xr-xr-x 2 root 512 Apr 8 1994 etc" */ # "lrwxrwxrwx 1 root other 7 Jan 25 00:17 bin -> usr/bin" */ # Note that UNIX-style listings can use names that contain a space: # "-rw------- 1 incognito.guy Domain Users 11420 2011-12-29 18:51 .bash_history" # Also produced by Microsoft's FTP servers for Windows: */ # "---------- 1 owner group 1803128 Jul 10 10:18 ls-lR.Z" */ # "d--------- 1 owner group 0 May 9 19:45 Softlib" */ # Also WFTPD for MSDOS: */ # "-rwxrwxrwx 1 noone nogroup 322 Aug 19 1996 message.ftp" */ # Also NetWare: */ # "d [R----F--] supervisor 512 Jan 16 18:53 login" */ # "- [R----F--] rhesus 214059 Oct 20 15:27 cx.exe" */ # Also NetPresenz and Rumpus on the Mac: */ # "-------r-- 326 1391972 1392298 Nov 22 1995 MegaPhone.sit" */ # "drwxrwxr-x folder 2 May 10 1996 network" */ # We used to regex on the line, but that is too slow, now we just # use split() and do our best with that. fi = line.split(None, 7) if len(fi) == 7 and fi[1] == "folder": # NetPresenz and Rumpus folder format fi = fi[:1] + ["", ""] + fi[2:] # First char dictates the type of file if line[0] == "d": mode |= stat.S_IFDIR # directory elif line[0] == "-": mode |= stat.S_IFREG # regular file elif line[0] == "l": mode |= stat.S_IFLNK # symlink isSymlink = True elif line[0] == "s": mode |= stat.S_IFSOCK # socket elif line[0] == "b": mode |= stat.S_IFBLK # block type (special file) elif line[0] == "c": mode |= stat.S_IFCHR # character type (special file) else: mode |= stat.S_IFREG # unknown #self.log.debug("THIS IS A SYMLINK!!!") # we unfortunately dont know if we are owner or group member, so guess again mode_str = fi[0] # Determine the file permissions, i.e. from the line "drwxr-xr-x" if mode_str[1:2] == "r": mode |= stat.S_IRUSR if mode_str[2:3] == "w": mode |= stat.S_IWUSR if mode_str[3:4] == "x": mode |= stat.S_IXUSR elif mode_str[3:4] == "s": mode |= (stat.S_IXUSR | stat.S_ISUID) elif mode_str[3:4] == "S": mode |= stat.S_ISUID if mode_str[4:5] == "r": mode |= stat.S_IRGRP if mode_str[5:6] == "w": mode |= stat.S_IWGRP if mode_str[6:7] == "x": mode |= stat.S_IXGRP elif mode_str[6:7] == "s": mode |= (stat.S_IXGRP | stat.S_ISGID) elif mode_str[6:7] == "S": mode |= stat.S_ISGID if mode_str[7:8] == "r": mode |= stat.S_IROTH if mode_str[8:9] == "w": mode |= stat.S_IWOTH if mode_str[9:10] == "x": mode |= stat.S_IXOTH elif mode_str[9:10] == "t": mode |= (stat.S_IXOTH | stat.S_ISVTX) elif mode_str[9:10] == "T": mode |= stat.S_ISVTX # Deal with spaces in the user or group names - bug . if fi[4] and fi[4][0].lower() in ascii_lowercase and \ fi[5] and fi[5][0] in string_digits and \ fi[7] and fi[7][0] in string_digits and \ " " in fi[7] and \ fi[3] and fi[3][0].lower() in ascii_lowercase and \ fi[2] and fi[2][0].lower() in ascii_lowercase: while len(fi) >= 8: fi[3] += fi.pop(5) fi = fi[:6] + fi[6].split(None, 1) if fi[5] and fi[5][0] in string_digits: break if fi[4].lower() in self._3char_month_names: # Not enough fields, pad it out. fi.insert(1, "") if fi[4]: self.st_size = fi[4] # File size # Work out the date try: # Date is in fi[5], check it's format. I.e # "Nov 30" # "2005-11-30" # "26 fev" # to see if we have a time, or a year? guessedYear = False if fi[5] and (fi[5][0] not in string_digits or (len(fi[6]) == 3 and fi[6][0] not in string_digits)): if " " in fi[7] and fi[7][0] in string_digits: # Requires the filename field to be split up: fi = fi[:1] + fi[2:7] + fi[7].split(None, 1) if len(fi) == 9: fi.pop(1) month,day,year = fi[4:7] # Some locales swap the day and the month, i.e. french: # "26 fev 22:00", bug 88866. if month and month[0] in string_digits and \ day and day not in string_digits: day, month = month, day if year.find(":") > -1: hour = year # fix time to be 5 digit always year = "%d"%(time.gmtime(time.time())[0]) guessedYear = True else: hour = "00:00" if len(day) < 2: day = "0"+day if len(hour) < 5: hour = "0"+hour date = "%s %s %s %s" % (month, day, year, hour) try: # Note: This can fail due to locale differences between # the client and server, as time.strptime uses the # locale for converting the fields. If this does # fail, we give it one other chance using default # English as the locale setting. See bug: # http://bugs.activestate.com/show_bug.cgi?id=62638 t = time.strptime(date, '%b %d %Y %H:%M') except Exception, e: # Error parsing the date field # Try using internal strptime, with ENGLISH setting t = strptime.strptime(date, '%b %d %Y %H:%M') else: sp = fi[5].split('-', 2) if len(sp) == 3: # 2005-11-30 format year, month, day = sp hour = fi[6] date = "%s %s %s %s" % (year, month, day, hour) t = time.strptime(date, '%Y %m %d %H:%M') else: raise Exception("Uknown date") self.st_mtime = time.mktime(t) if guessedYear: # Bug 81475. # If the time is more than a day ahead, set it back one # year. For example, we recieved: "Dec 31 23:29", but # if today's date is "March 18 2009", then the year should # actually be 2008, otherwise the date is in the future... # A date too far ahead is defined as two days greater than # the current local machine time (> 99% correct). if self.st_mtime >= time.time() + (2 * 60 * 60 * 24): t = list(t) t[0] -= 1 self.st_mtime = time.mktime(t)
locr={0:{0:0}} user_loc={0:{0:0}} checkintime=[0]*21 x=0 locr_max=0#位置边的最大权值 trainc=0 testc=0 locckins={0:0} userckins={0:0} #逐行处理数据 #第一行初始化,为位置关系等比较 lastline=f.readline() arr=lastline.split('\t') lasttime=time.mktime(strptime(arr[1],"20%y-%m-%dT%H:%M:%SZ")) lastuserid=arr[0] lastlocid=arr[4] i=0 while True: #i=i+1 newline=f.readline() if newline=='': break; arr=newline.split('\t') tm=strptime(arr[1],"20%y-%m-%dT%H:%M:%SZ") newtime=time.mktime(tm) userid=int(arr[0]) locidstr=arr[4] locid=int(locidstr[:-2])
#第一行初始化,为位置关系等比较 lastline=f.readline() arr=lastline.split(',') #lasttime=time.mktime(strptime(arr[2],"%d/%m/20%y %H:%M:%S")) lastuserid=arr[1] lastlocid=arr[3] i=0 time={0:0} while True: newline=f.readline() if newline=='': break; arr=newline.split(',') tm=strptime(arr[2],"%d/%m/20%y %H:%M:%S") newtime=time.mktime(tm) userid=int(arr[1]) locidstr=arr[3] '''#统计每个签到的时间分布 if tm.tm_year==2010 and tm.tm_mon>=10: testf.write(newline) testc=testc+1 else: trainf.write(newline) trainc=trainc+1 ''' #统计每个用户的签到次数
#!/usr/bin/python import time try: strptime = time.strptime except AttributeError: print("load from strptime") from strptime import strptime print strptime("2013-09-14 00:00:00","%Y-%m-%d %H:%M:%S") print strptime("30 Nov 00","%d %b %y") print strptime("1 Jan 70 1:30pm","%d %b %y %I:%M%p")
''' #import strptime try: strptime = time.strptime except AttributeError: from strptime import strptime f=open("d:\\1totalCheckins.txt",'r') out=file("d:\\1out.txt","w") #第一行,把时间值存储在lastt中,初始化 lastline=f.readline() arr=lastline.split('\t') print arr lastt=time.mktime(strptime(arr[1],"20%y-%m-%dT%H:%M:%SZ")) lastflag=True i=0 #后面的每行与前一行判断,如果user相同且时间在五分钟之内,则放弃,否则写入到out。txt中 #(默认为不同用户之间最早的和最晚的签到时间不可能在五分钟之内) while True: line=f.readline() arr=line.split('\t') i=i+1 if i%1000==0: print i,":",arr #获得时间值 intt=time.mktime(strptime(arr[1],"20%y-%m-%dT%H:%M:%SZ")) if lastt-intt>300: if lastflag:
littleTime=201311 largeTime=0 #逐行处理数据 timeCountStatc=[0,0,0,0,0,0,0,0,0,0,0] #将string类型的locId改为int型的 locStrIdDic={} locIdNum=0 while True: newline=f.readline() if newline=='': break; arr=newline.split(',')#arr=newline.split('\t') tm=strptime(arr[2],"%d/%m/20%y %H:%M:%S")#tm=strptime(arr[1],"20%y-%m-%dT%H:%M:%SZ") newtime=time.mktime(tm) userid=int(arr[1]) locidstr=arr[3] locid=locIdNum if locidstr in locStrIdDic.keys(): locid=locStrIdDic[locidstr] else: locStrIdDic[locidstr]=locIdNum locIdNum+=1 timenum=timeNum(tm.tm_year,tm.tm_mon) ''' if timenum>largeTime: largeTime=timenum if timenum<littleTime :littleTime=timenum if timenum<=201301:timeCountStatc[0]+=1
import time # make sure we have a strptime function! try: strptime = time.strptime except AttributeError: from strptime import strptime print strptime("31 Nov 00", "%d %b %y") print strptime("1 Jan 70 1:30pm", "%d %b %y %I:%M%p")
fout=open("E:\\checkin\\foursquare_allCheckin.txt",'w') #将string类型的locId改为int型的 locStrIdDic={} locIdNum=0 i=0 while True: i=i+1 if i%10000 ==0: print i newline=f.readline() if newline=='': break; arr=newline.split(',')#arr=newline.split('\t') tm=strptime(arr[2],"%d/%m/20%y %H:%M:%S")#tm=strptime(arr[1],"20%y-%m-%dT%H:%M:%SZ") newtime=time.mktime(tm) userid=int(arr[1]) locidstr=arr[3] locid=locIdNum if locidstr in locStrIdDic: locid=locStrIdDic[locidstr] else: locStrIdDic[locidstr]=locIdNum locIdNum+=1 timenum=timeNum(tm.tm_year,tm.tm_mon) nline=str(userid)+'\t'+str(tm.tm_year)+'-'+str(tm.tm_mon)+'-'+str(tm.tm_mday)+'T'+str(tm.tm_hour)+':'\ +str(tm.tm_min)+":"+str(tm.tm_sec)+'Z\t'+arr[5]+'\t'+arr[6]+'\t'+ str(locid)+'\n'
else: self.st_size = fi[1] try: #t = time.strptime(fi[0],'%m-%d-%y %I:%M%p') try: # Note: This can fail due to locale differences between # the client and server, as time.strptime uses the # locale for converting the fields. If this does # fail, we give it one other chance using default # English as the locale setting. See bug: # http://bugs.activestate.com/show_bug.cgi?id=62638 t = time.strptime(fi[0],'%m-%d-%y %I:%M%p') except Exception, e: # Error parsing the date field # Try using internal strptime, with ENGLISH setting t = strptime.strptime(fi[0],'%m-%d-%y %I:%M%p') #print "date trans: ",repr(t) self.st_time = time.mktime(t) except Exception, e: # Error parsing the date field #print "\n%s" % e self.log.warn("Unknown date in line: '%s'" % (line)) self.st_mtime = 0 # Unknown date, 1970 it is! filename = fi[2] #print "filename:", filename self.st_mode = mode # Set the full path # XXX - Windows/DOS filenames break ?? self._setpathinfo(dirname, filename, link_target) #self.log.debug("initFromDirectoryListing: %s", self) return True
f.write(str(cur)) dbcon.commit() cur.execute("SELECT * FROM trends") f.write("".join(["\n----Rows after deletion---\n",str(cur.rowcount),"\n"])) twohoursbefore = (datetime.now() - timedelta(0,2*60*60)) onehourbefore = (datetime.now() - timedelta(0,1*60*60)) for row in cursor: # Iteration is equivalent to lots of fetchone() calls entrytime = strptime(row[0], "2013-%m-%d %H:%M") entrydatetime = datetime.fromtimestamp(mktime(entrytime)) #print entrydatetime.hour; #if entrydatetime.hour == twohoursbefore.hour and entrydatetime.hour < onehourbefore.hour: f.write ("".join([row[1],str(row[2])])) cur.execute("SELECT * FROM trends WHERE word = %s", (row[1],)) print(cur) if cur.rowcount == 0: #means the row has to be inserted f.write('\nWord does not exist. Will now be inserted.\n') cur.execute("INSERT INTO trends (word, count) VALUES (%s, %s)", (row[1], row[2],)) f.write(str(cur)) dbcon.commit() else:
else: self.st_size = fi[1] try: #t = time.strptime(fi[0],'%m-%d-%y %I:%M%p') try: # Note: This can fail due to locale differences between # the client and server, as time.strptime uses the # locale for converting the fields. If this does # fail, we give it one other chance using default # English as the locale setting. See bug: # http://bugs.activestate.com/show_bug.cgi?id=62638 t = time.strptime(fi[0], '%m-%d-%y %I:%M%p') except Exception, e: # Error parsing the date field # Try using internal strptime, with ENGLISH setting t = strptime.strptime(fi[0], '%m-%d-%y %I:%M%p') #print "date trans: ",repr(t) self.st_time = time.mktime(t) except Exception, e: # Error parsing the date field #print "\n%s" % e self.log.warn("Unknown date in line: '%s'" % (line)) self.st_mtime = 0 # Unknown date, 1970 it is! filename = fi[2] #print "filename:", filename self.st_mode = mode # Set the full path # XXX - Windows/DOS filenames break ?? self._setpathinfo(dirname, filename, link_target) #self.log.debug("initFromDirectoryListing: %s", self) return True
def initFromDirectoryListing(self, dirname, line): mode = 0 isSymlink = False self.dirname = dirname link_target = None if not line: log.debug("initFromDirectoryListing: No line to process!") return False # Check the first character of the listing is a unix style if line[0] in self._possible_first_chars_for_unix_listing: # UNIX-style listing, without inum and without blocks */ # "-rw-r--r-- 1 root other 531 Jan 29 03:26 README" */ # "dr-xr-xr-x 2 root other 512 Apr 8 1994 etc" */ # "dr-xr-xr-x 2 root 512 Apr 8 1994 etc" */ # "lrwxrwxrwx 1 root other 7 Jan 25 00:17 bin -> usr/bin" */ # Note that UNIX-style listings can use names that contain a space: # "-rw------- 1 incognito.guy Domain Users 11420 2011-12-29 18:51 .bash_history" # Also produced by Microsoft's FTP servers for Windows: */ # "---------- 1 owner group 1803128 Jul 10 10:18 ls-lR.Z" */ # "d--------- 1 owner group 0 May 9 19:45 Softlib" */ # Also WFTPD for MSDOS: */ # "-rwxrwxrwx 1 noone nogroup 322 Aug 19 1996 message.ftp" */ # Also NetWare: */ # "d [R----F--] supervisor 512 Jan 16 18:53 login" */ # "- [R----F--] rhesus 214059 Oct 20 15:27 cx.exe" */ # Also NetPresenz and Rumpus on the Mac: */ # "-------r-- 326 1391972 1392298 Nov 22 1995 MegaPhone.sit" */ # "drwxrwxr-x folder 2 May 10 1996 network" */ # We used to regex on the line, but that is too slow, now we just # use split() and do our best with that. fi = line.split(None, 7) if len(fi) == 7 and fi[1] == "folder": # NetPresenz and Rumpus folder format fi = fi[:1] + ["", ""] + fi[2:] # First char dictates the type of file if line[0] == "d": mode |= stat.S_IFDIR # directory elif line[0] == "-": mode |= stat.S_IFREG # regular file elif line[0] == "l": mode |= stat.S_IFLNK # symlink isSymlink = True elif line[0] == "s": mode |= stat.S_IFSOCK # socket elif line[0] == "b": mode |= stat.S_IFBLK # block type (special file) elif line[0] == "c": mode |= stat.S_IFCHR # character type (special file) else: mode |= stat.S_IFREG # unknown #self.log.debug("THIS IS A SYMLINK!!!") # we unfortunately dont know if we are owner or group member, so guess again mode_str = fi[0] # Determine the file permissions, i.e. from the line "drwxr-xr-x" if mode_str[1:2] == "r": mode |= stat.S_IRUSR if mode_str[2:3] == "w": mode |= stat.S_IWUSR if mode_str[3:4] == "x": mode |= stat.S_IXUSR elif mode_str[3:4] == "s": mode |= (stat.S_IXUSR | stat.S_ISUID) elif mode_str[3:4] == "S": mode |= stat.S_ISUID if mode_str[4:5] == "r": mode |= stat.S_IRGRP if mode_str[5:6] == "w": mode |= stat.S_IWGRP if mode_str[6:7] == "x": mode |= stat.S_IXGRP elif mode_str[6:7] == "s": mode |= (stat.S_IXGRP | stat.S_ISGID) elif mode_str[6:7] == "S": mode |= stat.S_ISGID if mode_str[7:8] == "r": mode |= stat.S_IROTH if mode_str[8:9] == "w": mode |= stat.S_IWOTH if mode_str[9:10] == "x": mode |= stat.S_IXOTH elif mode_str[9:10] == "t": mode |= (stat.S_IXOTH | stat.S_ISVTX) elif mode_str[9:10] == "T": mode |= stat.S_ISVTX # Deal with spaces in the user or group names - bug . if fi[4] and fi[4][0].lower() in ascii_lowercase and \ fi[5] and fi[5][0] in string_digits and \ fi[7] and fi[7][0] in string_digits and \ " " in fi[7] and \ fi[3] and fi[3][0].lower() in ascii_lowercase and \ fi[2] and fi[2][0].lower() in ascii_lowercase: while len(fi) >= 8: fi[3] += fi.pop(5) fi = fi[:6] + fi[6].split(None, 1) if fi[5] and fi[5][0] in string_digits: break if fi[4].lower() in self._3char_month_names: # Not enough fields, pad it out. fi.insert(1, "") if fi[4]: self.st_size = fi[4] # File size # Work out the date try: # Date is in fi[5], check it's format. I.e # "Nov 30" # "2005-11-30" # "26 fev" # to see if we have a time, or a year? guessedYear = False if fi[5] and (fi[5][0] not in string_digits or (len(fi[6]) == 3 and fi[6][0] not in string_digits)): if " " in fi[7] and fi[7][0] in string_digits: # Requires the filename field to be split up: fi = fi[:1] + fi[2:7] + fi[7].split(None, 1) if len(fi) == 9: fi.pop(1) month, day, year = fi[4:7] # Some locales swap the day and the month, i.e. french: # "26 fev 22:00", bug 88866. if month and month[0] in string_digits and \ day and day not in string_digits: day, month = month, day if year.find(":") > -1: hour = year # fix time to be 5 digit always year = "%d" % (time.gmtime(time.time())[0]) guessedYear = True else: hour = "00:00" if len(day) < 2: day = "0" + day if len(hour) < 5: hour = "0" + hour date = "%s %s %s %s" % (month, day, year, hour) try: # Note: This can fail due to locale differences between # the client and server, as time.strptime uses the # locale for converting the fields. If this does # fail, we give it one other chance using default # English as the locale setting. See bug: # http://bugs.activestate.com/show_bug.cgi?id=62638 t = time.strptime(date, '%b %d %Y %H:%M') except Exception, e: # Error parsing the date field # Try using internal strptime, with ENGLISH setting t = strptime.strptime(date, '%b %d %Y %H:%M') else: sp = fi[5].split('-', 2) if len(sp) == 3: # 2005-11-30 format year, month, day = sp hour = fi[6] date = "%s %s %s %s" % (year, month, day, hour) t = time.strptime(date, '%Y %m %d %H:%M') else: raise Exception("Uknown date") self.st_mtime = time.mktime(t) if guessedYear: # Bug 81475. # If the time is more than a day ahead, set it back one # year. For example, we recieved: "Dec 31 23:29", but # if today's date is "March 18 2009", then the year should # actually be 2008, otherwise the date is in the future... # A date too far ahead is defined as two days greater than # the current local machine time (> 99% correct). if self.st_mtime >= time.time() + (2 * 60 * 60 * 24): t = list(t) t[0] -= 1 self.st_mtime = time.mktime(t)
if tm.tm_sec<10: dayStr = "0"+ str(tm.tm_sec) return str(tm.tm_year)+'-'+monStr+'-'+dayStr+'T'+hourStr+':'+minStr+":"+secStr ''' path="E:\\checkin\\" outpath="E:\\checkin\\" f=open(path+"filter_foursquare_CheckinNewYork.txt",'r') outf=open(outpath+"foursquare_NewYork_allcheckin.txt",'w') i=0 while True: i=i+1 if i%10000 ==0: print i newline=f.readline() if newline=='': break; arr=newline.split('\t') tm=strptime(arr[1],"20%y-%m-%dT%H:%M:%SZ") newtime=time.mktime(tm) timenum=timeNum(tm.tm_year,tm.tm_mon) newline=arr[0]+'\t'+timeStr(tm)+'Z\t'+arr[2]+'\t'+arr[3]+'\t'+ arr[4] outf.write(newline) f.close() outf.close()
# -*- coding:utf-8 -*- # Example 1-81. 使用 time.strptime 函数解析时间 # File: time-example-6.py import time # make sure we have a strptime function! # 确认有函数 strptime try: strptime = time.strptime except AttributeError: from strptime import strptime print strptime("31 Nov 00", "%d %b %y") print strptime("1 Jan 70 1:30pm", "%d %b %y %I:%M%p")