1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
import time import MySQLdb import math,datetime
def _add_month_interval(dt,inter): m=dt.month+inter-1 y=dt.year y=dt.year+int(m/12) m=m % 12 +1 return (y,m)
def add_month_interval(dt,inter): y,m=_add_month_interval(dt,inter) y2,m2=_add_month_interval(dt,inter+1) maxD=( datetime.date(y2,m2,1)-datetime.timedelta(days=1) ).day d= dt.day<=maxD and dt.day or maxD return(y,m,d)
def add_year_interval(dt,inter): return add_month_interval(dt,inter*12)
def update_metric(inter): conn=MySQLdb.connect(host='localhost',user='root',passwd='root',db='gavin',port=3306) cur=conn.cursor() cur.execute('select date from metric_impressions order by date desc') results = cur.fetchall() result=list(results) for now_date in result: timeArray = time.strptime(str(now_date[0]), "%Y-%m-%d %H:%M:%S") timeArray = list(timeArray) dt = datetime.date(timeArray[0],timeArray[1],timeArray[2]) timeArray[0],timeArray[1],timeArray[2] = add_month_interval(dt,inter) new_date = time.strftime("%Y-%m-%d %H:%M:%S", timeArray) print "new_date:{} now_date:{}".format(new_date, now_date[0]) sql = 'update metric_impressions_hour set date="{}" where date="{}"'.format(new_date, now_date[0]) cur.execute(sql) conn.commit() conn.close()
def update_compaign(inter): conn=MySQLdb.connect(host='localhost',user='root',passwd='root',db='gavin',port=3306) cur=conn.cursor() cur.execute('select data_date from campaign order by data_date desc') results = cur.fetchall() result=list(results) for now_date in result: timeArray = time.strptime(str(now_date[0]), "%Y%m%d") timeArray = list(timeArray) dt = datetime.date(timeArray[0],timeArray[1],timeArray[2]) timeArray[0],timeArray[1],timeArray[2] = add_month_interval(dt,inter) new_date = time.strftime("%Y%m%d", timeArray) print "new_date:{} now_date:{}".format(new_date, now_date[0]) sql = 'update campaign_stats set data_date="{}" where data_date="{}"'.format(new_date, now_date[0]) print sql cur.execute(sql) conn.commit() conn.close()
update_compaign(1)
|