#!/usr/local/bin/python
# coding=utf-8
''' a new time factory for processing stat time
'''import sys
import time
import datetime
(stat_hourly, stat_daily, stat_weekly, stat_monthly, stat_minute) = range(5)
stat_periods =
#時間初始工廠
class timefactory(object):
def __init__(self,start_time,end_time,diff=none):
self.start_time = start_time
self.end_time = end_time
self.diff = diff
def process_time(self):
pass
#公用方法,用於返回時間區間的列表,在工廠控制2函式處呼叫
def time_range(self):
time_list =
date_list = self.process_time()
print(date_list)
for i,time in enumerate(date_list):
if i < len(date_list)-1:
time = [date_list[i],date_list[i+1]]
return time_list
#處理日統計工廠
class dayfactory(timefactory):
def process_time(self):
date_list =
dt = self.start_time
delta = datetime.timedelta(days=1)
while dt <= self.end_time:
dt += delta
date_list = [datetime.datetime.strptime(d, '%y%m%d').strftime('%y-%m-%d') for d in date_list]
return date_list
#處理周統計工廠
class weekfactory(timefactory):
def process_time(self):
date_list =
dt = self.start_time
dt -= datetime.timedelta(days=6)
delta = datetime.timedelta(days=1)
while dt <= self.end_time:
if dt.weekday() == 0:
dt += delta
date_list = [datetime.datetime.strptime(d, '%y%m%d').strftime('%y-%m-%d') for d in date_list]
return date_list
#處理月統計工廠
class monthfactory(timefactory):
def process_time(self):
dt = self.start_time
date_list = set()
delta = datetime.timedelta(days=1)
while dt <= self.end_time:
date_list.add('%s%s%s' % (dt.year, dt.month, 1))
dt += delta
date_list = [datetime.datetime.strptime(d, '%y%m%d').strftime('%y-%m-%d') for d in date_list]
return date_list
#處理小時統計工廠
class hou***ctory(timefactory):
def process_time(self):
hour_list =
self.diff = 3600
# begin = int(time.mktime(datetime.datetime.strptime(self.start_time,'%y-%m-%s').timetuple()))
begin = int(time.mktime(datetime.datetime.timetuple(self.start_time)))
end = int(time.mktime(datetime.datetime.timetuple(self.end_time)))
while begin <= end:
begin += 3600
return hour_list
#此類支援分鐘和秒統計,傳入diff為秒的差值,例按每分鐘統計則傳入diff=60
class othe***ctory(timefactory):
def process_time(self):
hour_list =
begin = int(time.mktime(datetime.datetime.timetuple(self.start_time)))
end = int(time.mktime(datetime.datetime.timetuple(self.end_time)))
while begin <= end:
begin += int(self.diff)
return hour_list
#控制時間工廠函式1和2的區別:返回時間格式不同,1返回統計時間列表,2返回統計時間區間列表
def control_timefactory(period,date_range,diff=none):
start_time = date_range.split('-')[0]
end_time = date_range.split('-')[1]
start_time = datetime.datetime.strptime(start_time, '%y%m%d')
end_time = datetime.datetime.strptime(end_time, '%y%m%d')
print(start_time, end_time)
if period == 1:
stat_timelist = dayfactory(start_time, end_time).process_time()
elif period == 2:
stat_timelist = weekfactory(start_time, end_time).process_time()
elif period == 3:
stat_timelist = monthfactory(start_time, end_time).process_time()
elif period == 4:
stat_timelist = hou***ctory(start_time, end_time).process_time()
else:
stat_timelist = othe***ctory(start_time, end_time, diff).process_time()
return stat_timelist
def control_timefactory2(period,date_range,diff=none):
start_time = date_range.split('-')[0]
end_time = date_range.split('-')[1]
start_time = datetime.datetime.strptime(start_time, '%y%m%d')
end_time = datetime.datetime.strptime(end_time, '%y%m%d')
print(start_time, end_time)
if period == 1:
stat_timelist = dayfactory(start_time, end_time).time_range()
elif period == 2:
stat_timelist = weekfactory(start_time, end_time).time_range()
elif period == 3:
stat_timelist = monthfactory(start_time, end_time).time_range()
elif period == 0:
stat_timelist = hou***ctory(start_time, end_time).time_range()
else:
stat_timelist = othe***ctory(start_time, end_time, diff).time_range()
return stat_timelist
統計時間周,月的計算
計算周 getdateinweek 和dateadd 兩個方法 返回乙個日期字串在星期中的順序 public static int getdateinweek string strdate catch parseexception e 乙個日期上加天數 public static string da...
使用Spring AOP新增統計時間的功能
最近有個需求,需要統計各個介面 類的方法的執行時間,但是要盡量不影響原來業務,不改變原有 spring提供了4種實現aop的方式 1.經典的基於 的aop 2.aspectj註解驅動的切面 3.純pojo切面 4.注入式aspectj切面 我這裡講到的是使用 aspectj註解驅動的方式,其他的可以...
設計模式系列之工廠模式 抽象工廠模式 八
推薦閱讀 設計模式系列之工廠模式 工廠方法模式 七 工廠方法模式通過引入工廠等級結構,解決了簡單工廠模式中工廠類職責太重的問題,但由於工廠方法模式中的每個工廠只生產一類動物,可能會導致系統中存在大量的工廠類。此時,可以考慮將一些相關的動物組成乙個動物族,由同乙個工廠來統一生產,這就是抽象工廠模式的基...