Created
March 5, 2021 03:52
-
-
Save kordless/14f53833e045b39b764602fa33596331 to your computer and use it in GitHub Desktop.
Mitta Timewarp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import datetime | |
| from dateutil.relativedelta import relativedelta | |
| import re | |
| # timewarp.py | |
| # Kord Campbell | |
| # Copyright 2020, Mitta Corp. | |
| # All Rights Reserved. | |
| # description: common name to solr timerange converter in python3 | |
| # example: on 11/1, +yesterday returns ["2020-10-31T00:00:00Z","2020-10-31T23:59:59Z"] | |
| # timewarp function translates and slices the command function calls from any numeric values | |
| # example: +week10 returns ["2020-02-03T00:00:00Z","2020-02-09T23:59:59Z"] | |
| # function names are pulled from the argument to be called with or without target date values | |
| # example: +day31 calls function day() with value 31: day(31) | |
| # example: +yesterday calls function yesterday() with no value | |
| commands = {} | |
| command = lambda f: commands.setdefault(f.__name__, f) | |
| # solr formatted timestring | |
| timestring="%Y-%m-%dT%H:%M:%SZ" | |
| # @2020-11-01T02:43:11Z +hour11 | |
| # returns ["2020-10-31T11:00:00Z","2020-10-31T11:59:59Z"] | |
| @command | |
| def hour(hour): | |
| # hours are from 0 to 23 in the last 24 hours | |
| hour = int(hour) | |
| now = then = datetime.datetime.now() | |
| if int(hour) in range(24): | |
| # set end time | |
| nowhour = int(now.strftime("%H")) | |
| if hour > nowhour: | |
| # take a delta then map | |
| then = now - datetime.timedelta(days=1) | |
| start = then.strftime("%Y-%m-%dT" + '{:0>2}'.format(hour) + ":00:00Z") | |
| end = then.strftime("%Y-%m-%dT" + '{:0>2}'.format(hour) + ":59:59Z") | |
| else: | |
| # return last hour to now | |
| end = now.strftime(timestring) | |
| then = now - datetime.timedelta(hours=1) | |
| start = then.strftime(timestring) | |
| return [start, end] | |
| @command | |
| def lasthour(): | |
| # return last hour to now | |
| now = datetime.datetime.now() | |
| end = now.strftime(timestring) | |
| then = now - datetime.timedelta(hours=1) | |
| start = then.strftime(timestring) | |
| return [start, end] | |
| @command | |
| def last(hour): | |
| # last X hours | |
| now = datetime.datetime.now() | |
| end = now.strftime(timestring) | |
| start = (now - datetime.timedelta(hours=int(hour))).strftime(timestring) | |
| return [start, end] | |
| @command | |
| def today(): | |
| # today from midnight 00:00 till now | |
| now = datetime.datetime.now() | |
| end = now.strftime(timestring) | |
| start = now.strftime("%Y-%m-%dT00:00:00Z") | |
| return [start, end] | |
| @command | |
| def lastday(): | |
| # last 24 hours | |
| now = datetime.datetime.now() | |
| end = now.strftime(timestring) | |
| start = (now - datetime.timedelta(days=1)).strftime(timestring) | |
| return [start, end] | |
| @command | |
| def yesterday(): | |
| # all in yesterday | |
| now = datetime.datetime.now() | |
| then = now - datetime.timedelta(days=1) | |
| start = then.strftime("%Y-%m-%dT00:00:00Z") | |
| end = then.strftime("%Y-%m-%dT23:59:59Z") | |
| return [start, end] | |
| @command | |
| def daybeforeyesterday(): | |
| # all in yesterday | |
| now = datetime.datetime.now() | |
| then = now - datetime.timedelta(days=2) | |
| start = then.strftime("%Y-%m-%dT00:00:00Z") | |
| end = then.strftime("%Y-%m-%dT23:59:59Z") | |
| return [start, end] | |
| @command | |
| def day(day): | |
| # 24 hours of a given day in the last month | |
| lastmonth = datetime.datetime.now() - relativedelta(months=1) | |
| dayslastmonth = (lastmonth.replace(month = lastmonth.month % 12 +1, day = 1)-datetime.timedelta(days=1)).day | |
| # make sure requested day is not zero or more than last month's day count | |
| if int(day) > 0 and int(day) in range(int(dayslastmonth)+1): | |
| now = then = datetime.datetime.now() | |
| nowday = int(now.strftime("%d")) | |
| if nowday < int(day): | |
| then = now - relativedelta(months=1) # go back a month | |
| start = then.strftime("%Y-%m-" + '{:0>2}'.format(day) + "T00:00:00Z") | |
| end = then.strftime("%Y-%m-" + '{:0>2}'.format(day) + "T23:59:59Z") | |
| return [start, end] | |
| # else just give last 24 hours | |
| now = datetime.datetime.now() | |
| end = now.strftime(timestring) | |
| start = (now - datetime.timedelta(days=1)).strftime(timestring) | |
| return [start, end] | |
| @command | |
| def thisweek(): | |
| # starting on Sunday @ midnight, through now | |
| now = then = datetime.datetime.now() | |
| nowdaynum = int(now.strftime("%d")) | |
| then = now - datetime.timedelta(days=nowdaynum-1) | |
| start = then.strftime("%Y-%m-%dT00:00:00Z") | |
| end = now.strftime(timestring) | |
| return [start, end] | |
| @command | |
| def week(week): | |
| # starting on Sunday @ midnight, through that numeric week's end | |
| now = datetime.datetime.now() | |
| # its of week and day num | |
| nowweeknum = int(now.strftime("%U")) | |
| nowdaynum = int(now.strftime("%d")) | |
| # go back to sunday | |
| sunday = now + datetime.timedelta(days=nowdaynum) | |
| # calculate deltas | |
| if nowweeknum > int(week): | |
| delta = nowweeknum - int(week) | |
| else: | |
| delta = 52 - ( int(week) - nowweeknum ) | |
| # I really don't understand it but I wrote it nevertheless | |
| then = sunday - datetime.timedelta(weeks=delta+1) | |
| end = then.strftime("%Y-%m-%dT23:59:59Z") | |
| that = then - datetime.timedelta(weeks=1) + datetime.timedelta(days=1) | |
| start = that.strftime("%Y-%m-%dT00:00:00Z") | |
| # seems to work! | |
| return [start, end] | |
| @command | |
| def lastweek(): | |
| # starting on Sunday @ midnight last week, through the week's end | |
| now = datetime.datetime.now() | |
| nowdaynum = int(now.strftime("%d")) | |
| # go back to saturday | |
| saturday = now - datetime.timedelta(days=nowdaynum) | |
| end = saturday.strftime("%Y-%m-%dT23:59:59Z") # saturday night | |
| then = saturday - datetime.timedelta(days=6) | |
| start = then.strftime("%Y-%m-%dT00:00:00Z") | |
| return [start,end] | |
| @command | |
| def thismonth(): | |
| now = datetime.datetime.now() | |
| nowmonth = int(now.strftime("%m")) | |
| nowdaynum = int(now.strftime("%d")) | |
| # go back to first | |
| first = now - datetime.timedelta(days=nowdaynum) | |
| start = first.strftime("%Y-%m-01T00:00:00Z") # hack zeroing | |
| end = now.strftime(timestring) | |
| return [start,end] | |
| @command | |
| def lastmonth(): | |
| # get this month, go to last month | |
| now = datetime.datetime.now() | |
| nowday = int(now.strftime("%d")) | |
| then = now - relativedelta(months=1) # go back a month | |
| start = then.strftime("%Y-%m-01T00:00:00Z") # hack zeroing | |
| that = now - datetime.timedelta(days=nowday) # back to the end of last month | |
| end = that.strftime("%Y-%m-%dT23:59:59Z") # hack ending | |
| return [start, end] | |
| @command | |
| def month(month): | |
| now = datetime.datetime.now() | |
| nowmonth = int(now.strftime("%m")) | |
| nowdaynum = int(now.strftime("%d")) | |
| # go back to first | |
| first = now - datetime.timedelta(days=nowdaynum) | |
| if nowmonth > int(month): | |
| delta = nowmonth - int(month) | |
| else: | |
| delta = 12 - ( int(month) - nowmonth ) | |
| then = first - relativedelta(months=delta-1) | |
| start = then.strftime("%Y-%m-01T00:00:00Z") # hack zeroing | |
| that = first - relativedelta(months=delta-1) | |
| end = that.strftime("%Y-%m-%dT23:59:59Z") # hack zeroing | |
| return [start, end] | |
| @command | |
| def thisyear(): | |
| pass | |
| @command | |
| def lastyear(): | |
| pass | |
| @command | |
| def year(year): | |
| pass | |
| # time is +important | |
| def timewarp(argument): | |
| # we receive some string without spaces | |
| # +hour3, +day12, +yesterday, #tag (returned) | |
| # or +2020-11-04T03:00:00Z_2020-11-04T03:59:59Z | |
| # timewarp expects a + on the front of the timeword | |
| # strip the + sign off, or return argument as is | |
| args = argument.split('+') | |
| if args[0] == argument: | |
| # process any other options like |sparkline | |
| # return it if we don't have a + (which is handy for passthrough parsing) | |
| return argument | |
| # check for full specific date range | |
| argss = args[1].split('_') | |
| if argss[0] != args[1]: | |
| return [argss[0], argss[1]] | |
| # check the string if it ends in numbers | |
| m = re.search(r'\d+$', args[1]) | |
| if m is not None: | |
| try: | |
| # numberss on the end | |
| argss = args[1].split(m.group()) | |
| # call the function name + value | |
| return commands[argss[0]](m.group()) | |
| except: | |
| try: | |
| return commands[argss[0]]() | |
| except: | |
| return argument | |
| else: | |
| # just call the function name | |
| return commands[args[1]]() | |
| def timewords(): | |
| return commands | |
| # some tests | |
| """ | |
| print(timewarp("+lastweek")) | |
| print(timewarp("+lasthour")) | |
| print(timewarp("+month1")) | |
| print(timewarp("+month10")) | |
| print(timewarp("+month11")) | |
| print(timewarp("+month12")) | |
| print(timewarp("+lastmonth")) | |
| print(timewarp("+2020-11-04T03:00:00Z_2020-11-04T03:59:59Z")) | |
| print(timewarp("+week1")) | |
| print(timewarp("+week12")) | |
| print(timewarp("+week50")) | |
| print(timewarp("+week45")) | |
| print(timewarp("+hour0")) | |
| print(timewarp("+hour3")) | |
| print(timewarp("+hour22")) | |
| print(timewarp("+hour99")) | |
| print(timewarp("+lasthour")) | |
| print(timewarp("+today")) | |
| print(timewarp("+lastday")) | |
| print(timewarp("+last24")) | |
| print(timewarp("+last22")) | |
| print(timewarp("+yesterday")) | |
| print(timewarp("+day12")) | |
| print(timewarp("+day1")) | |
| print(timewarp("+day0")) | |
| print(timewarp("+day31")) | |
| print(timewarp("+day30")) | |
| print(timewarp("+day300000")) | |
| print(timewarp("+thisweek")) | |
| """ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment