-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathorgduration.py
317 lines (284 loc) · 9.99 KB
/
orgduration.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
from ctypes import ArgumentError
import datetime
import re
import math
import sublime_plugin
# This library provides tools to manipulate durations. A duration
# can have multiple formats:
#
# - 3:12
# - 1:23:45
# - 1y 3d 3h 4min
# - 1d3h5min
# - 3d 13:35
# - 2.35h
#
# More accurately, it consists of numbers and units, as defined in
# variable `org-duration-units', possibly separated with white
# spaces, and an optional "H:MM" or "H:MM:SS" part, which always
# comes last. White spaces are tolerated between the number and its
# relative unit. Variable `org-duration-format' controls durations
# default representation.
#
# The library provides functions allowing to convert a duration to,
# and from, a number of minutes: `org-duration-to-minutes' and
# `org-duration-from-minutes'. It also provides two lesser tools:
# `org-duration-p', and `org-duration-h:mm-only-p'.
#
# Users can set the number of minutes per unit, or define new units,
# in `org-duration-units'. The library also supports canonical
# duration, i.e., a duration that doesn't depend on user's settings,
# through optional arguments.
RE_DURATION_PARSER = re.compile(r'\s*((?P<years>[0-9.]+)y)?\s*((?P<days>[0-9.]+)d)?\s*((?P<hours>[0-9.]+)h)?\s*((?P<mins>[0-9.]+)min)?\s*((?P<thours>[0-9]+)[:](?P<tmins>[0-9]+)([:](?P<tsecs>[0-9]+))?)?')
class OrgDuration:
def __init__(self, minutes):
self.mins = minutes
def __str__(self):
r = ""
y = int(self.mins / 525600.0)
if (y > 0):
r += str(y) + "y "
days = math.fmod(self.mins, 525600.0)
d = int(days / 1440.0)
if (d > 0):
r += str(d) + "d "
hours = math.fmod(days, 1440.0)
h = int(hours / 60.0)
if (h > 0):
r += str(h) + "h "
mins = int(math.fmod(hours, 60))
if (mins > 0):
r += str(mins) + "mins"
return r.strip()
def days(self):
return self.mins / 1440.0
def months(self):
return self.mins / 43800.0
def weeks(self):
return self.mins / 10080.0
def hours(self):
return self.mins / 60.0
def minutes(self):
return self.mins
def seconds(self):
return self.mins * 60.0
def doubled(self):
return OrgDuration(self.mins * 2.0)
def __sub__(self, o):
if (isinstance(o, int)):
d = OrgDuration.ParseInt(o)
mins = self.mins - d.mins
d.mins = abs(mins)
return d
if (isinstance(o, float)):
d = OrgDuration.ParseFloat(o)
mins = self.mins - d.mins
d.mins = abs(mins)
return d
if (isinstance(o, OrgDuration)):
d = OrgDuration.ParseInt(1)
d.mins = abs(self.mins - o.mins)
return d
return self
def __add__(self, o):
if (isinstance(o, int)):
d = OrgDuration.ParseInt(o)
mins = self.mins + d.mins
d.mins = mins
return d
if (isinstance(o, float)):
d = OrgDuration.ParseFloat(o)
mins = self.mins + d.mins
d.mins = mins
return d
if (isinstance(o, OrgDuration)):
d = OrgDuration.ParseInt(1)
d.mins = self.mins + o.mins
return d
return self
def __div__(self, other):
if (isinstance(other, int) or isinstance(other, float)):
return OrgDuration(self.mins / other)
return OrgDuration(self.mins)
def __mul__(self, other):
if isinstance(other, int) or isinstance(other, float):
return OrgDuration(self.mins * other)
return OrgDuration(self.mins)
def __rmul__(self, other):
return self.__mul__(other)
def __radd__(self, other):
return self.__add__(other)
def __ne__(self, other):
return not self.__eq__(other)
def __gt__(self, other):
if isinstance(other, int) or isinstance(other, float):
return self.mins > other
if isinstance(other, str):
v = OrgDuration.Parse(other)
if v is not None:
return self.mins > v.mins
else:
raise ArgumentError(other + " is not a valid duration")
if isinstance(other, OrgDuration):
return self.mins > other.mins
return False
def __ge__(self, other):
if isinstance(other, int) or isinstance(other, float):
return self.mins >= other
if isinstance(other, str):
v = OrgDuration.Parse(other)
if v is not None:
return self.mins >= v.mins
else:
raise ArgumentError(other + " is not a valid duration")
if isinstance(other, OrgDuration):
return self.mins >= other.mins
return False
def __lt__(self, other):
if isinstance(other, int) or isinstance(other, float):
return self.mins < other
if isinstance(other, str):
v = OrgDuration.Parse(other)
if v is not None:
return self.mins < v.mins
else:
raise ArgumentError(other + " is not a valid duration")
if isinstance(other, OrgDuration):
return self.mins < other.mins
return False
def __le__(self, other):
if isinstance(other, int) or isinstance(other, float):
return self.mins <= other
if isinstance(other, str):
v = OrgDuration.Parse(other)
if v is not None:
return self.mins <= v.mins
else:
raise ArgumentError(other + " is not a valid duration")
if isinstance(other, OrgDuration):
return self.mins <= other.mins
return False
def timedelta(self):
y = int(self.mins / 525600.0)
days = math.fmod(self.mins, 525600.0)
d = int(days / 1440.0)
hours = math.fmod(days, 1440.0)
h = int(hours / 60.0)
mins = int(math.fmod(hours, 60))
td = datetime.timedelta(days=d + (y * 365), hours=h, minutes=mins)
return td
@staticmethod
def FromTimedelta(td: datetime.timedelta):
return OrgDuration(td.days * 1440 + (td.seconds / 60.0))
@staticmethod
def Parse(txt: str, need: bool = False):
m = RE_DURATION_PARSER.search(txt)
if (m):
mtot = 0.0
got = False
y = m.group('years')
if (y):
mtot += float(y) * 525600.0
got = True
d = m.group('days')
if (d):
mtot += float(d) * 1440
got = True
h = m.group('hours')
if (h):
mtot += float(h) * 60
got = True
mins = m.group('mins')
if (mins):
mtot += float(mins)
got = True
h = m.group('thours')
if (h):
mtot += float(h) * 60
got = True
mins = m.group('tmins')
if (mins):
mtot += float(mins)
got = True
secs = m.group('tsecs')
if (secs):
mtot += float(secs) * 0.01666667
got = True
if (got or not need):
return OrgDuration(mtot)
return None
@staticmethod
def ParseWeekDayOffset(txt: str):
if (len(txt) < 3):
return None
change = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]
nextDay = txt.lower()
nextOf = [idx for idx, element in enumerate(change) if element.startswith(nextDay)]
if (len(nextOf) > 0):
nextOf = nextOf[0]
else:
return None
wd = datetime.datetime.now().weekday()
if (wd >= nextOf):
offset = 7 - wd + nextOf
else:
offset = nextOf - wd
mtot = 0.0
if (offset != 0):
mtot += float(offset) * 1440
return OrgDuration(mtot)
return None
@staticmethod
def ParseMonthOffset(txt: str):
if (len(txt) < 3):
return None
change = ["january", "febuary", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"]
tokens = txt.split(' ')
monthOffset = 0
dayOffset = 0
day = datetime.datetime.now().day
for token in tokens:
tk = token.lower()
monthCheck = [idx for idx, element in enumerate(change) if element.startswith(tk)]
if (len(monthCheck) > 0):
monthOffset = (monthCheck[0] + 1)
if tk.isnumeric():
dayOffset = int(tk)
if (monthOffset == 0 and dayOffset == 0):
return None
if (dayOffset == 0):
dayOffset = day
dt = datetime.datetime.now().replace(month=monthOffset, day=dayOffset)
offset = dt - datetime.datetime.now()
mtot = 0.0
if ((offset.total_seconds() / 60.0) != 0):
mtot += offset.total_seconds() / 60.0
return OrgDuration(mtot)
return None
@staticmethod
def ParseInt(d: int):
# Let the user determine default meaning d is the default
mtot = float(d) * 1440
return OrgDuration(mtot)
@staticmethod
def ParseFloat(d: float):
# Let the user determine default meaning d is the default
mtot = float(d) * 1440
return OrgDuration(mtot)
# ================================================================================
class OrgTestDurationCommand(sublime_plugin.TextCommand):
def run(self, edit, onDone=None):
d = OrgDuration.Parse("2y3d5h6min")
print(str(d))
d = OrgDuration.Parse("1y")
print(str(d))
d = OrgDuration.Parse("2d")
print(str(d))
d = OrgDuration.Parse("3h")
print(str(d))
d = OrgDuration.Parse("4min")
print(str(d))
d = OrgDuration.Parse("1d 3:44")
print(str(d))
d = OrgDuration.Parse("1d 4:55:55")
print(str(d))