2a867cc16456ca00bfddffab889c92182452f105
[maegirls] / tags / 0.1.0 / src / algo.py
1 #!/usr/bin/env python
2 # coding=UTF-8
3
4 # Copyright (C) 2010 Stefanos Harhalakis
5 #
6 # This file is part of maegirls.
7 #
8 # maegirls is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # maegirls is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with maegirls.  If not, see <http://www.gnu.org/licenses/>.
20 #
21 # $Id: 0.py 2265 2010-02-21 19:16:26Z v13 $
22
23 __version__ = "$Id: 0.py 2265 2010-02-21 19:16:26Z v13 $"
24
25 import time
26
27 class Algo(object):
28     def __init__(self):
29         t=today()
30         self.setReference(t, 28)
31
32     # set day "day" as the reference day of starting period cycle
33     # day is int(time.time()/86400)
34     # cycle is the cycle width in days, starting from 0
35     def setReference(self, day, cycle):
36         self.refday=day
37         self.cycle=cycle
38
39     # Convert an arbitary day to a day in cycle
40     def dayInCycle(self, day):
41         d=(day-self.refday) % self.cycle
42         if d<0:
43             d+=self.cycle
44
45         return(d)
46
47     def cycleLength(self):
48         return(self.cycle)
49
50     def currentDayInCycle(self):
51         d=today()
52         return(self.dayInCycle(d))
53
54     # Return:
55     # ret={
56     #   'status':   ['ok', 'red']
57     #   'day':      day in status
58     #   'len':      length of status
59     #   }
60     #
61     # e.g.  ret={'status':'red', 'day':0, 'len':5}
62     #       means that it is in red, in the first of 5
63     def status(self, day):
64         d=self.dayInCycle(day)
65
66         ovbefore=14
67
68         if d<5:
69             # Red
70             ret={
71                 'status':   'red',
72                 'day':      d,
73                 'len':      5,
74                 }
75         elif d>=self.cycle-6:
76             ret={
77                 'status':   'blue',
78                 'day':      6+d-self.cycle,
79                 'len':      6,
80                 }
81         elif d>self.cycle-ovbefore-4 and d<=self.cycle-ovbefore:
82             ret={
83                 'status':   'green',
84                 'day':      d-self.cycle+ovbefore+3,
85                 'len':      4,
86                 }
87         elif d>self.cycle-16 and d<=self.cycle-ovbefore+1:
88             ret={
89                 'status':   'green',
90                 'day':      2,
91                 'len':      4,
92                 }
93         else:
94             ret={
95                 'status':   'ok',
96                 'day':      d,
97                 'len':      self.cycle
98                 }
99
100         return(ret)
101
102 def today():
103     t=time.time() + time.timezone
104     day=int(t/86400)
105
106     return(day)
107
108
109 # vim: set ts=8 sts=4 sw=4 noet formatoptions=r ai nocindent:
110