-
Notifications
You must be signed in to change notification settings - Fork 0
/
dateslib.py
executable file
·544 lines (393 loc) · 14.7 KB
/
dateslib.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
#!/usr/bin/python
from datetime import datetime, timedelta
import time
import re
from itertools import *
YmdHM = "%Y:%m:%d:%H:%M" # standard formatting of datetime-input
extra = timedelta # alias to make it easy
# helpermethods that return the start, the end and the start/end of
# different timeunits
def startofyear(date):
return datetime(year=date.year, month=1, day=1, hour=0, minute=0)
def endofyear(date):
return datetime(year=date.year + 1, month=1, day=1, hour=0, minute=0)
def startofmonth(date):
return datetime(year=date.year, month=date.month, day=1, hour=0,
minute=0)
def endofmonth(datex):
date = (startofmonth(datex) + extra(days=32)).replace(day=1)
return datetime(year=date.year, month=date.month, day=1, hour=0, minute=0)
def startofday(date):
return datetime(year=date.year, month=date.month, day=date.day, hour=0,
minute=0)
def endofday(datex):
date = startofday(datex) + extra(days=1)
return datetime(year=date.year, month=date.month, day=date.day, hour=0,
minute=0)
def startofhour(date):
return datetime(year=date.year, month=date.month, day=date.day,
hour=date.hour, minute=0)
def endofhour(datex):
date = startofhour(datex) + extra(hours=1)
return datetime(year=date.year, month=date.month, day=date.day,
hour=date.hour, minute=0)
def startofminute(date):
return datetime(year=date.year, month=date.month, day=date.day,
hour=date.hour, minute=date.minute)
def endofminute(datex):
date = startofminute(datex) + timespan(minute=1)
return datetime(year=date.year, month=date.month, day=date.day,
hour=date.hour, minute=date)
def startofweek(datex):
weekday = datex.weekday()
monday = datex - extra(days=weekday)
return startofday(monday)
def endofweek(datex):
weekday = datex.weekday()
nextsunday = datex + extra(days=7 - weekday)
return endofday(nextsunday)
def rangeyear(date):
return startofyear(date), endofyear(date)
def rangemonth(date):
return startofmonth(date), endofmonth(date)
def rangeday(date):
return startofday(date), endofday(date)
def rangehour(date):
return startofhour(date), endofhour(date)
def rangeminute(date):
return startofminute(date), startofminute(date)
def rangeweek(date):
return startofweek(date), endofweek(date)
# this* returns the start of d, m, h, y, mi till now
def thisyear(date, i):
return startofyear(date), date
def thismonth(date, i):
return startofmonth(date), date
def thisday(date, i):
return startofday(date), date
def thishour(date, i):
return startofhour(date), date
def thisminute(date, i):
return startofminute(date), date
def thisweek(date, i):
return startofweek(date), date
def thismorning(date, i):
beginx = startofday(date)
begin = beginx + extra(hours=6)
end = begin + extra(hours=6)
return begin, end
def thisevening(date, i):
bx, ex = thisafternoon(date, i)
begin = ex
end = ex + extra(hours=6)
return begin, end
def thisafternoon(date, i):
bx, ex = thismorning(date, i)
begin = ex
end = ex + extra(hours=6)
return begin, end
def thisnight(date, i):
bx, ex = thisevening(date, i)
begin = ex
end = ex + extra(hours=6)
return begin, end
# prev* y, m, d, h, m,... means :inclusive now, count back y, m, d, ...
# if now is the end of the month count back to the end of the previous months
# ex: if your halfway april you get the same date in feb;if you are 30 or
# 31 april you get the last day of feb"""
def prevyear(date, i):
som = startofmonth(date)
yearagomonth = som.replace(year=(date.year - 1 * i))
yearago = yearagomonth + extra(days=date.day - 1, hours=date.hour,
minutes=date.minute)
return yearago, date
def prevmonth(datex, i):
date = datex
monthago = date.day
for months in xrange(i):
if monthago != 1:
date = date.replace(day=1)
date = date - extra(days=1)
while True:
try:
date = date.replace(day=monthago)
return date,datex
except ValueError:
monthago -= 1
def prevday(date, i):
return date - extra(days=1 * i), date
def prevhour(date, i):
return date - extra(hours=1 * i), date
def prevminute(date, i):
return date - extra(minutes=1 * i), date
def prevweek(date, i):
startprevweek = startofday(date - extra(days=7 * (i * 1)))
return startprevweek, date
def prevmorning(date, i):
yesterday = startofday(date) - extra(days=(1 * i))
begin = yesterday + extra(hours=6)
end = begin + extra(hours=6)
return begin, end
def prevafternoon(date, i):
__, begin = prevmorning(date, i)
end = begin + extra(hours=6)
return begin, end
def prevevening(date, i):
__, begin = prevafternoon(date, i)
end = begin + extra(hours=6)
return begin, end
def prevnight(date, i):
end = startofday(date) - extra(days=(1 * i))
begin = end - extra(hours=6)
return begin, end
# last y, m, d, ... means: the whole previous y m d h m, not till now
def lastyear(date, i):
end = startofyear(date)
begin = end.replace(year=(end.year - (1 * i)))
return begin, end
def lastmonth(date, i):
end = startofmonth(date)
start = end
for x in range(i):
prevmonth = start - extra(days=1)
start = prevmonth.replace(day=1)
begin = start
return begin, end
def lastday(date, i):
end = startofday(date)
begin = end - extra(days=(i * 1))
return begin, end
def lasthour(date, i):
end = startofhour(date)
begin = end - extra(hours=(i * 1))
return begin, end
def lastminute(date, i):
end = startofminute(date)
begin = end - extra(minutes=(i * 1))
return begin, end
def lastweek(date, i):
end = startofday(date - extra(days=date.weekday()))
begin = end - extra(days=(7 * i))
return begin, end
def lastnight(date, i):
begin = startofday(date) - extra(days=(1 * i))
end = begin + extra(hours=6)
return begin, end
def lastevening(date, i):
end = startofday(date) - extra(days=(1 * i))
begin = end - extra(hours=6)
return begin, end
# helpermethods that do the work
def getwordsrange(words):
third = 1 # if you ask for prev 6 days this is the 6
a, b, c = words[0]
if b.strip().isdigit():
third = int(b)
if a in TODAY:
first = "THIS"
second = "DAY"
elif a in YESTERDAY:
first = "PREV"
second = "DAY"
elif a:
first = [cat for cat, i in timeUnits.items() if a in i][0]
if c:
second = [cat for cat, i in timeUnits.items() if c in i][0]
a = SOLUTION[first][second]
begin, end = a(datetime.now(), third) # this calls all the above methods
return begin, end
def process((a, b, c)):
"""this should take care of the + and = and x in exact date queries
it copies itself, resets,... starting from the datetime now
the asked-for number gets put in the slot m, h, d, M, Y
= copies the number of the now-slot m, h, d, M, Y
x sets the number to zero in the slot m, h, d, M, Y
"""
if not b:
return str(a)
if b == '=':
return str(a)
if b == 'x':
return str(c(0))
if int(b) < 0:
return str(c(a + int(b)))
if int(b) >= 0:
return str(c(int(b)))
def fromdigits(ip):
# parses the input to a wellformed datetime
n = -1
a = ["x"] * 5
for m in re.finditer(digitaldate, ip):
n = n + 1
if n < 5:
a[n] = (m.group(1))
return a
# helpers to check the min/max of date
def checkyear(y):
if y < 1900:
return 1900 # lowest allowed in structs
return y if y < 9999 else 99999
def checkmonths(M):
if M <= 0:
return 1
return M if (M > 0 and M < 13) else 12
def checkdays(d):
if d <= 0:
return 1
return d if (d > 0 and d < 31) else 31
def checkhours(h):
return h if (h > -1 and h < 25) else 0
def checkminutes(m):
return m if (m > -1 and m < 61) else 0
# stick all the checkers in a tuple for easy access
checkmethods = (checkyear, checkmonths, checkdays, checkhours, checkminutes)
def nowformatted():
return datetime.now().strftime(YmdHM).split(":")
def getnumbersrange(numbers):
timeunit = 0 # this is the datetimeslot the user wants...year, month, day
for i, l in enumerate(numbers):
if l.isdigit() or l == "=":
timeunit = i
nstr = nowformatted() # datetime now formatted
inputdate = ":".join(imap(process, izip_longest(nstr, numbers,
checkmethods)))
onedate = datetime.strptime(inputdate, YmdHM)
# now we have to maximise it
# so that it makes the asked-for date inclusive
# so 2012/08 becomes 2012/09/01 at midnight
if timeunit == 0:
begin, end = rangeyear(onedate)
if timeunit == 1:
begin, end = rangemonth(onedate)
if timeunit == 2:
begin, end = rangeday(onedate)
if timeunit == 3:
begin, end = rangehour(onedate)
if timeunit == 4:
begin, end = rangeminute(onedate)
return begin, end
# collection of words used to parse time requests
# maybe let user add their own words to this ???
PREV = ("prev", "p")
LAST = ("last", "l")
THIS = ("this", "t")
YEAR = ("years", "year", "y")
MONTH = ("months", "month", "m", "mo")
WEEK = ("weeks", "week", "w")
DAY = ("days", "day", "d")
HOUR = ("hours", "hour", "h")
MINUTE = ("minutes", "minute", "mi")
MORNING = ("morning", "mor")
AFTERNOON = ("afternoon", "aft")
EVENING = ("evening", "eve")
NIGHT = ("night", "nig", "tonight")
WEEKDAYS = ("monday", "thuesday", "wednesday", "thursday",
"friday", "saturday", "sunday")
TODAY = ("today", "now")
YESTERDAY = ("yesterday", "yes")
itswords = PREV + LAST + THIS + YEAR + MONTH + WEEK + DAY + HOUR + MINUTE + MORNING + AFTERNOON\
+ EVENING + NIGHT + WEEKDAYS + TODAY + YESTERDAY
timeUnits = {"YEAR": YEAR, "MONTH": MONTH, "WEEK": WEEK, "DAY": DAY,
"HOUR": HOUR, "MINUTE": MINUTE, "THIS": THIS, "LAST": LAST,
"PREV": PREV, "NIGHT": NIGHT, "MORNING": MORNING,
"AFTERNOON": AFTERNOON, "EVENING": EVENING}
# regexpatterns to parse input
digitaldate = re.compile('(=|x|(-[0-9]+)|([0-9]+))')
alphadate = re.compile('([a-zA-Z]+)(\s*\d*\s*)([a-zA-Z]*)')
onedigitaldate = re.compile('(=|x|[0-9]+)')
digits = re.compile('[0-9]+')
ellipsepat = re.compile('(.*)(\.\.)(.*)')
THIS = {"YEAR": thisyear, "MONTH": thismonth, "WEEK": thisweek, "DAY": thisday,
"HOUR": thishour, "MINUTE": thisminute, "MORNING": thismorning,
"AFTERNOON": thisafternoon, "EVENING": thisevening, "NIGHT": thisnight}
LAST = {"YEAR": lastyear, "MONTH": lastmonth, "WEEK": lastweek, "DAY": lastday,
"HOUR": lasthour, "MINUTE": lastminute, "NIGHT": lastnight,
"EVENING": lastevening}
PREV = {"YEAR": prevyear, "MONTH": prevmonth, "WEEK": prevweek, "DAY": prevday,
"HOUR": prevhour, "MINUTE": prevminute, "MORNING": prevmorning,
"AFTERNOON": prevafternoon, "EVENING": prevevening, "NIGHT": prevnight}
SOLUTION = {"THIS": THIS, "PREV": PREV, "LAST": LAST}
def makestructfromdatetime(dt):
"""given a datetime returns a struct"""
return dt.timetuple()
def makefloatfromstruct(t):
""" given a struct returns a float"""
try:
return time.mktime(t)
except Exception, e:
print "caught:", e
def makefloatfromdatetime(dt):
"""given a datetime returns a float"""
return makefloatfromstruct(makestructfromdatetime(dt))
def getinput(n):
"""puts up a command prompt to get input"""
print ">", n
return convertinput(re.sub('\s+\.\.\s+', '..', raw_input(">")))
def inputstring(n):
""" use this to ask input queries it returns min, max float"""
b, e = convertinput(re.sub('\s+\.\.s+', '..', n))
return (makefloatfromdatetime(b), makefloatfromdatetime(e))
def convertinput(ip):
""" see if we have an ellipse, so 2 requests"""
m = re.findall(ellipsepat, ip)
if m:
return get_range_fromEllipseInput(m)
else:
return get_range_fromSingleInput(ip)
def get_range_fromSingleInput(ip):
""" 1 input, see if its num or words"""
if any(word in ip.split() for word in itswords):
words = re.findall(alphadate, ip)
begin, end = getwordsrange(words)
else:
numbers = re.finditer(digitaldate, ip)
date = [n.group(1) for n in numbers][:6]
begin, end = getnumbersrange(date)
return begin, end
def get_range_fromEllipseInput(m):
"""2 requests, split them up and get the min max"""
a, b, c = m[0]
begin = ""
end = ""
if a != ".." and a != "":
begin, __ = get_range_fromSingleInput(a)
if b != ".." and b != "":
__, end = get_range_fromSingleInput(b)
if c != ".." and c != "":
__, end = get_range_fromSingleInput(c)
if not begin:
begin = datetime(1990, 01, 01)
if not end:
end = datetime.max
return begin, end
if __name__ == '__main__':
""" this module returns a min-float,max-float from a request.
The request can be a verbal request or a numeric request
a verbal request is ex: this afternoon, yesterday, last night,
this week, last 2 months, prev year, last year, this year,
prev 5 months..yesterday night, this morning.. prev 2 minutes
a numeric request is ex: 2012.10.(returns the whole 10th month)
2012..2013.02(start of 2012 upto 2013.03.01 00:00:00)
You can combine verbal and numeric requests ex: 2012..last night
verbal requests: 3 parts of which the second part is a number
the first part can be :
this: this year, month, week, day, hour, minute, morning,
afternoon, evening, night
last:returns the last year, month,week... till now, so :now is
the end and count year, month back...
if its friday than last week is friday to now
prev : if its friday than the prev week is ending the sunday
before and a week further back
and then there are these special words: yesterday, today
for numeric requests: 2012/10/12/23/59(Y,m,d,H,mi)
when you give part of this, it gets that: 2012 gives you all of 2012
2012/10/01 gives you everything form the first of 10
if you put in a '=' , you copy the number form now
so 2012==== copies the values form today after the 2012.
an x sets the value to zero 2012:10xxxx(its okay to give
more x-es or =-es, gets cut off) is 2012/10/01/00:00
You cann't do 20121001, we need something between the numbers,;:/
are all okay
spaces or not before after '..' is ok
for beets users ;:if you got whitespace between words you should
use " or ' at the beginning and end"""