-
Notifications
You must be signed in to change notification settings - Fork 7
/
testserver.py
95 lines (75 loc) · 3.64 KB
/
testserver.py
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#-*-coding:utf-8 -*-
import datetime
import datetime, calendar
from dateutil.relativedelta import relativedelta
dateDelim = "."
strftimeFormat = "%d" + dateDelim + "%m" + dateDelim + "%Y %H:%M:%S"
def settlement_period_info(time_start, repeat_after='', repeat_after_seconds=0, now=None, prev = False):
"""
Функция возвращает дату начала и дату конца текущегопериода
"""
#print time_start, repeat_after, repeat_after_seconds, now
if not now:
now=datetime.datetime.now()
#time_start=time_start.replace(tzinfo='UTC')
#print "repeat_after_seconds=",repeat_after_seconds
if repeat_after_seconds>0:
#print 1
if prev==False:
delta_days=now - time_start
else:
delta_days=now-datetime.timedelta(seconds=repeat_after_seconds) - time_start
length=repeat_after_seconds
if repeat_after!='DONT_REPEAT':
#Когда будет начало в текущем периоде.
nums,ost= divmod(delta_days.days*86400+delta_days.seconds, length)
tnc=now-datetime.timedelta(seconds=ost)
#Когда это закончится
tkc=tnc+datetime.timedelta(seconds=length)
return (tnc, tkc, length)
else:
return (time_start,time_start+datetime.timedelta(seconds=repeat_after_seconds), repeat_after_seconds)
elif repeat_after=='DAY':
if prev==False:
delta_days=now - time_start
else:
delta_days=now-datetime.timedelta(seconds=86400) - time_start
length=86400
#Когда будет начало в текущем периоде.
nums,ost= divmod(delta_days.days*86400+delta_days.seconds, length)
tnc=now-datetime.timedelta(seconds=ost)
#Когда это закончится
tkc=tnc+datetime.timedelta(seconds=length)
return (tnc, tkc, length)
elif repeat_after=='WEEK':
if prev==False:
delta_days=now - time_start
else:
delta_days=now-datetime.timedelta(seconds=604800) - time_start
length=604800
#Когда будет начало в текущем периоде.
nums,ost= divmod(delta_days.days*86400+delta_days.seconds, length)
tnc=start+relativedelta(weeks=nums)
tkc=tnc+relativedelta(weeks=1)
return (tnc, tkc, length)
elif repeat_after=='MONTH':
if prev==False:
months=relativedelta(now,time_start).months
else:
months=relativedelta(now-relativedelta(months=1),time_start).months
tnc=time_start+relativedelta(months=months)
tkc=tnc+relativedelta(months=1)
delta=tkc-tnc
return (tnc, tkc, delta.days*86400)
elif repeat_after=='YEAR':
#Февраль!
#To-DO: Добавить проверку на prev
tnc=start+relativedelta(years=relativedelta(now, time_start).years)
tkc=tnc+relativedelta(years=1)
delta=tkc-tnc
return (tnc, tkc, delta.seconds)
start=datetime.datetime(year=2008, month=1, day=1)
now = datetime.datetime(year=2012, month=2, day=9)
n,c,s = settlement_period_info(time_start=start, repeat_after='WEEK', now=now)
print n.strftime(strftimeFormat), c.strftime(strftimeFormat)
#print start+relativedelta(years=relativedelta(now, start).years)