-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathhelpers.py
276 lines (214 loc) · 7.46 KB
/
helpers.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
# -*- coding: utf-8 -*-
# This file is part of Gertrude.
#
# Gertrude is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# Gertrude is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Gertrude; if not, see <http://www.gnu.org/licenses/>.
from __future__ import unicode_literals
from __future__ import print_function
from __future__ import division
from builtins import str
import datetime
import unicodedata
from constants import *
def is_power_of_two(x):
return (x != 0) and (x & (x - 1)) == 0
def Number2String(value):
if isinstance(value, float):
return "%.2f" % value
else:
return "%d" % value
def GetDeStr(s):
if len(s) > 0 and s[0].lower() in ('a', 'e', 'i', 'o', 'u'):
return "d'" + s
else:
return "de " + s
def GetDeMoisStr(mois):
return GetDeStr(months[mois].lower())
def GetBoolStr(val):
if val:
return "OUI"
else:
return "NON"
def date2str(date):
try:
return '%.02d/%.02d/%.04d' % (date.day, date.month, date.year)
except:
return ''
def GetPeriodeString(o):
if not o.debut and not o.fin:
return "Toujours"
elif o.debut and not o.fin:
return "A partir du " + date2str(o.debut)
elif o.fin and not o.debut:
return "Jusqu'au " + date2str(o.fin)
elif o.debut == datetime.date(o.fin.year, 1, 1) and o.fin == datetime.date(o.debut.year, 12, 31):
return "Année %d" % o.debut.year
else:
return date2str(o.debut) + ' - ' + date2str(o.fin)
def GetYearStart(date):
return datetime.date(date.year, 1, 1)
def GetYearEnd(date):
return datetime.date(date.year, 12, 31)
def IncrDate(date, years=0, months=0, days=0):
day, month, year = date.day + days, date.month + months, date.year + years
while month > 12:
year += 1
month -= 12
while month <= 0:
year -= 1
month += 12
return datetime.date(year, month, 1) + datetime.timedelta(day - 1)
def GetDateAnniversaire(date, count=1):
return IncrDate(date, years=count)
def GetMonthStart(date):
return datetime.date(date.year, date.month, 1)
def GetMonthEnd(date):
if date.month == 12:
return datetime.date(date.year, 12, 31)
else:
return datetime.date(date.year, date.month + 1, 1) - datetime.timedelta(1)
def GetMonthDaysCount(date):
return (GetNextMonthStart(date) - GetMonthStart(date)).days
def GetNextMonthStart(date):
if date.month == 12:
return datetime.date(date.year+1, 1, 1)
else:
return datetime.date(date.year, date.month+1, 1)
def GetDurationMonths(start, end):
if start and end:
return end.year * 12 + end.month - start.year * 12 - start.month + 1
else:
return None
def GetTrimestreStart(date):
return datetime.date(date.year, 1 + 3 * ((date.month - 1) // 3), 1)
def GetTrimestreEnd(date):
nextTrimestre = GetTrimestreStart(date) + datetime.timedelta(80)
return GetTrimestreStart(nextTrimestre) - datetime.timedelta(1)
def str2date(string, year=None, day=None):
s = string.strip()
if s.count("-") == 2:
year, month, day = s.split("-")
if not day.isdigit() or not month.isdigit() or not year.isdigit():
return None
day, month, year = int(day), int(month), int(year)
elif s.count("/") == 2:
day, month, year = s.split("/")
if not day.isdigit() or not month.isdigit() or not year.isdigit():
return None
day, month, year = int(day), int(month), int(year)
elif year and s.count("/") == 1:
day, month = s.split("/")
if not day.isdigit() or not month.isdigit():
return None
day, month = int(day), int(month)
elif day and s.count("/") == 1:
month, year = map(lambda x: x, s.split("/"))
if not month.isdigit() or not year.isdigit():
return None
month, year = int(month), int(year)
else:
return None
if year < 1900:
return None
else:
return datetime.date(year, month, day)
def GetDureeArrondie(mode, start, end):
if start is None or end is None:
return 0
elif mode == ARRONDI_HEURE_ARRIVEE_DEPART:
return (((end + 11) // 12) - (start // 12)) * 12
elif mode == ARRONDI_HEURE:
return ((end - start + 11) // 12) * 12
elif mode == ARRONDI_HEURE_MARGE_DEMI_HEURE:
return ((end - start + 5) // 12) * 12
elif mode == ARRONDI_DEMI_HEURE:
return ((end - start + 5) // 6) * 6
else:
return end - start
def GetDateIntersection(periodes):
for one in range(0, len(periodes)-1):
i1 = periodes[one]
if i1.debut:
for two in range(one+1, len(periodes)):
i2 = periodes[two]
if i2.debut:
latest_start = max(i1.debut, i2.debut)
earliest_end = min(i1.GetFin(), i2.GetFin())
if (earliest_end - latest_start).days > 0:
return latest_start
return None
def GetHeureString(value):
# prend en paramètre une heure (float)
if value is None:
return ""
minutes = round(value * 60)
if value >= 0:
heures = minutes // 60
minutes -= heures * 60
return "%dh%02d" % (heures, minutes)
else:
minutes = -minutes
heures = (minutes // 60)
minutes -= heures * 60
return "-%dh%02d" % (heures, minutes)
def normalize_filename(filename):
return unicodedata.normalize("NFKD", filename)
def strip_accents(s):
return ''.join(c for c in unicodedata.normalize('NFD', s) if unicodedata.category(c) != 'Mn')
def get_emails(str):
if str is None:
return []
else:
return [email.strip() for email in str.split(",") if email.strip() != ""]
def truncate(string, length):
if len(string) > length:
return string[:length] + "..."
else:
return string
def Select(obj, date):
for o in obj:
if (not o.debut or date >= o.debut) and (not o.fin or date <= o.fin):
return o
return None
def GetAge(naissance, date=datetime.date.today()):
age = 0
if naissance:
age = (date.year - naissance.year) * 12 + date.month - naissance.month
if date.day < naissance.day:
age -= 1
return age
def GetAgeString(naissance, date=datetime.date.today()):
if naissance:
age = GetAge(naissance, date)
annees, mois = age / 12, age % 12
if annees < 0:
return ""
elif annees and mois:
return "%d ans et %d mois" % (annees, mois)
elif annees:
return "%d ans" % annees
else:
return "%d mois" % mois
else:
return ""
def GetDateString(date, weekday=True, annee=True):
if date.day == 1:
date_str = "1er %s" % (months[date.month - 1].lower())
else:
date_str = "%d %s" % (date.day, months[date.month - 1].lower())
if annee:
date_str += " %d" % date.year
if weekday:
return days[date.weekday()].lower() + " " + date_str
else:
return date_str