-
Notifications
You must be signed in to change notification settings - Fork 268
/
Copy pathorgdate.py
323 lines (270 loc) · 9.25 KB
/
orgdate.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
318
319
320
321
322
323
# -*- coding: utf-8 -*-
u"""
OrgDate
~~~~~~~~~~~~~~~~~~
This module contains all date/time/timerange representations that exist in
orgmode.
There exist three different kinds:
* OrgDate: is similar to a date object in python and it looks like
'2011-09-07 Wed'.
* OrgDateTime: is similar to a datetime object in python and looks like
'2011-09-07 Wed 10:30'
* OrgTimeRange: indicates a range of time. It has a start and and end date:
* <2011-09-07 Wed>--<2011-09-08 Fri>
* <2011-09-07 Wed 10:00-13:00>
All OrgTime oblects can be active or inactive.
"""
import datetime
import re
from orgmode.py3compat.encode_compatibility import *
# <2011-09-12 Mon>
_DATE_REGEX = re.compile(r"<(\d\d\d\d)-(\d\d)-(\d\d) [A-Z]\w\w>", re.UNICODE)
# [2011-09-12 Mon]
_DATE_PASSIVE_REGEX = re.compile(r"\[(\d\d\d\d)-(\d\d)-(\d\d) [A-Z]\w\w\]", re.UNICODE)
# <2011-09-12 Mon 10:20>
_DATETIME_REGEX = re.compile(
r"<(\d\d\d\d)-(\d\d)-(\d\d) [A-Z]\w\w (\d{1,2}):(\d\d)>", re.UNICODE)
# [2011-09-12 Mon 10:20]
_DATETIME_PASSIVE_REGEX = re.compile(
r"\[(\d\d\d\d)-(\d\d)-(\d\d) [A-Z]\w\w (\d{1,2}):(\d\d)\]", re.UNICODE)
_DATETIMERANGE_PASSIVE_REGEX = re.compile(
# <2011-09-12 Mon 10:00>--
r"\[(\d\d\d\d)-(\d\d)-(\d\d) [A-Z]\w\w (\d\d):(\d\d)\]--"
# <2011-09-12 Mon 11:00>
"\[(\d\d\d\d)-(\d\d)-(\d\d) [A-Z]\w\w (\d\d):(\d\d)\]", re.UNICODE)
# <2011-09-12 Mon>--<2011-09-13 Tue>
_DATERANGE_REGEX = re.compile(
# <2011-09-12 Mon>--
r"<(\d\d\d\d)-(\d\d)-(\d\d) [A-Z]\w\w>--"
# <2011-09-13 Tue>
"<(\d\d\d\d)-(\d\d)-(\d\d) [A-Z]\w\w>", re.UNICODE)
# <2011-09-12 Mon 10:00>--<2011-09-12 Mon 11:00>
_DATETIMERANGE_REGEX = re.compile(
# <2011-09-12 Mon 10:00>--
r"<(\d\d\d\d)-(\d\d)-(\d\d) [A-Z]\w\w (\d\d):(\d\d)>--"
# <2011-09-12 Mon 11:00>
"<(\d\d\d\d)-(\d\d)-(\d\d) [A-Z]\w\w (\d\d):(\d\d)>", re.UNICODE)
# <2011-09-12 Mon 10:00--12:00>
_DATETIMERANGE_SAME_DAY_REGEX = re.compile(
r"<(\d\d\d\d)-(\d\d)-(\d\d) [A-Z]\w\w (\d\d):(\d\d)-(\d\d):(\d\d)>", re.UNICODE)
def get_orgdate(data):
u"""
Parse the given data (can be a string or list). Return an OrgDate if data
contains a string representation of an OrgDate; otherwise return None.
data can be a string or a list containing strings.
"""
# TODO maybe it should be checked just for iterable? Does it affect here if
# in base __getitem__(slice(i,j)) doesn't return a list but userlist...
if isinstance(data, list):
return _findfirst(_text2orgdate, data)
else:
return _text2orgdate(data)
# if no dates found
return None
def _findfirst(f, seq):
u"""
Return first item in sequence seq where f(item) == True.
TODO: this is a general help function and it should be moved somewhere
else; preferably into the standard lib :)
"""
for found in (f(item) for item in seq if f(item)):
return found
def _text2orgdate(string):
u"""
Transform the given string into an OrgDate.
Return an OrgDate if data contains a string representation of an OrgDate;
otherwise return None.
"""
# handle active datetime with same day
result = _DATETIMERANGE_SAME_DAY_REGEX.search(string)
if result:
try:
(syear, smonth, sday, shour, smin, ehour, emin) = \
[int(m) for m in result.groups()]
start = datetime.datetime(syear, smonth, sday, shour, smin)
end = datetime.datetime(syear, smonth, sday, ehour, emin)
return OrgTimeRange(True, start, end)
except BaseException:
return None
# handle active datetime
result = _DATETIMERANGE_REGEX.search(string)
if result:
try:
tmp = [int(m) for m in result.groups()]
(syear, smonth, sday, shour, smin, eyear, emonth, eday, ehour, emin) = tmp
start = datetime.datetime(syear, smonth, sday, shour, smin)
end = datetime.datetime(eyear, emonth, eday, ehour, emin)
return OrgTimeRange(True, start, end)
except BaseException:
return None
# handle active datetime
result = _DATERANGE_REGEX.search(string)
if result:
try:
tmp = [int(m) for m in result.groups()]
syear, smonth, sday, eyear, emonth, ehour = tmp
start = datetime.date(syear, smonth, sday)
end = datetime.date(eyear, emonth, ehour)
return OrgTimeRange(True, start, end)
except BaseException:
return None
# handle active datetime
result = _DATETIME_REGEX.search(string)
if result:
try:
year, month, day, hour, minutes = [int(m) for m in result.groups()]
return OrgDateTime(True, year, month, day, hour, minutes)
except BaseException:
return None
# handle passive datetime
result = _DATETIMERANGE_PASSIVE_REGEX.search(string)
if result:
try:
tmp = [int(m) for m in result.groups()]
(syear, smonth, sday, shour, smin, eyear, emonth, eday, ehour, emin) = tmp
start = datetime.datetime(syear, smonth, sday, shour, smin)
end = datetime.datetime(eyear, emonth, eday, ehour, emin)
return OrgTimeRange(False, start, end)
except BaseException:
return None
# handle passive datetime
result = _DATETIME_PASSIVE_REGEX.search(string)
if result:
try:
year, month, day, hour, minutes = [int(m) for m in result.groups()]
return OrgDateTime(False, year, month, day, hour, minutes)
except BaseException:
return None
# handle passive dates
result = _DATE_PASSIVE_REGEX.search(string)
if result:
try:
year, month, day = [int(m) for m in result.groups()]
return OrgDate(False, year, month, day)
except BaseException:
return None
# handle active dates
result = _DATE_REGEX.search(string)
if result:
try:
year, month, day = [int(m) for m in result.groups()]
return OrgDate(True, year, month, day)
except BaseException:
return None
class OrgDate(datetime.date):
u"""
OrgDate represents a normal date like '2011-08-29 Mon'.
OrgDates can be active or inactive.
NOTE: date is immutable. Thats why there needs to be __new__().
See: http://docs.python.org/reference/datamodel.html#object.__new__
"""
def __init__(self, active, year, month, day):
self.active = active
pass
def __new__(cls, active, year, month, day):
return datetime.date.__new__(cls, year, month, day)
def __unicode__(self):
u"""
Return a string representation.
"""
if self.active:
return self.strftime(u'<%Y-%m-%d %a>')
else:
return self.strftime(u'[%Y-%m-%d %a]')
def __str__(self):
return u_encode(self.__unicode__())
def strftime(self, fmt):
return u_decode(datetime.date.strftime(self, u_encode(fmt)))
class OrgDateTime(datetime.datetime):
u"""
OrgDateTime represents a normal date like '2011-08-29 Mon'.
OrgDateTime can be active or inactive.
NOTE: date is immutable. Thats why there needs to be __new__().
See: http://docs.python.org/reference/datamodel.html#object.__new__
"""
def __init__(self, active, year, month, day, hour, mins):
self.active = active
def __new__(cls, active, year, month, day, hour, minute):
return datetime.datetime.__new__(cls, year, month, day, hour, minute)
def __unicode__(self):
u"""
Return a string representation.
"""
if self.active:
return self.strftime(u'<%Y-%m-%d %a %H:%M>')
else:
return self.strftime(u'[%Y-%m-%d %a %H:%M]')
def __str__(self):
return u_encode(self.__unicode__())
def strftime(self, fmt):
return u_decode(datetime.datetime.strftime(self, u_encode(fmt)))
class OrgTimeRange(object):
u"""
OrgTimeRange objects have a start and an end. Start and ent can be date
or datetime. Start and end have to be the same type.
OrgTimeRange objects look like this:
* <2011-09-07 Wed>--<2011-09-08 Fri>
* <2011-09-07 Wed 20:00>--<2011-09-08 Fri 10:00>
* <2011-09-07 Wed 10:00-13:00>
"""
def __init__(self, active, start, end):
u"""
stat and end must be datetime.date or datetime.datetime (both of the
same type).
"""
super(OrgTimeRange, self).__init__()
self.start = start
self.end = end
self.active = active
self.verbose = False
def __unicode__(self):
u"""
Return a string representation.
"""
# active
if self.active:
# datetime
if isinstance(self.start, datetime.datetime):
# if start and end are on same the day
if self.start.year == self.end.year and\
self.start.month == self.end.month and\
self.start.day == self.end.day:
return u"<%s-%s>" % (
self.start.strftime(u'%Y-%m-%d %a %H:%M'),
self.end.strftime(u'%H:%M'))
else:
return u"<%s>--<%s>" % (
self.start.strftime(u'%Y-%m-%d %a %H:%M'),
self.end.strftime(u'%Y-%m-%d %a %H:%M'))
# date
if isinstance(self.start, datetime.date):
return u"<%s>--<%s>" % (
self.start.strftime(u'%Y-%m-%d %a'),
self.end.strftime(u'%Y-%m-%d %a'))
# inactive
else:
if isinstance(self.start, datetime.datetime):
# if start and end are on same the day
if not self.verbose and self.start.year == self.end.year and\
self.start.month == self.end.month and\
self.start.day == self.end.day:
return u"[%s-%s]" % (
self.start.strftime(u'%Y-%m-%d %a %H:%M'),
self.end.strftime(u'%H:%M'))
else:
return u"[%s]--[%s]" % (
self.start.strftime(u'%Y-%m-%d %a %H:%M'),
self.end.strftime(u'%Y-%m-%d %a %H:%M'))
if isinstance(self.start, datetime.date):
return u"[%s]--[%s]" % (
self.start.strftime(u'%Y-%m-%d %a'),
self.end.strftime(u'%Y-%m-%d %a'))
def __str__(self):
return u_encode(self.__unicode__())
def duration(self):
return self.end - self.start
def str_duration(self):
duration = self.duration()
hours, minutes = divmod(duration.total_seconds(), 3600)
return u'%d:%d' % (hours, minutes // 60)
# vim: set noexpandtab: