Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def break_level(start, end, level, tzinfo='US/Eastern'):
"""
Return a list of unix timestamp break the start and end time according to given time frame.
start and end are not included.
:param tzinfo: A string for timezone, default is 'US/Eastern'.
:param start: integer of unix timestamp.
:param end: integer of unix timestamp.
:param level: int of time frame
:return: list of unix timestamp
"""
start_time = ts2datetime(start, tzinfo=tzinfo)
end_time = ts2datetime(end, tzinfo=tzinfo)
break_points = []
if level == 0:
break_points = arrow.Arrow.range('day', start_time, end_time)[1:-1]
else:
replace_arg = {LEVEL2STR[level]: 1}
if (level == 1 and start_time.isocalendar()[:2] == end_time.isocalendar()[:2]) or \
(level == 2 and start_time.strftime("%Y%m") == end_time.strftime("%Y%m")):
pass
else:
point = start_time
while True:
point = point.replace(**replace_arg).floor(LEVEL2STR[level][:-1])
if point >= end_time:
break
break_points.append(point)
break_points = [x.timestamp for x in break_points]
return break_points
def latest_30_days(self):
return arrow.Arrow.range(
'day',
self.date_value.replace(days=-29),
self.date_value,)
# all requested dates are assumed closed
dates = {r.date() : False for r in arrow.Arrow.range('day', begin_dt, end_dt)}
if resource.overlapping_unit:
for period in resource.overlapping_unit.periods.all():
if period.start < begin_d:
start = begin_dt
else:
start = arrow.get(period.start)
if period.end > end_d:
end = end_dt
else:
end = arrow.get(period.end)
for r in arrow.Arrow.range('day', start, end):
for day in period.days.all():
if day.weekday is r.weekday():
if day.closed:
dates[r.date()] = False
else:
if day.closes < day.opens:
# Day that closes before it opens means closing next day
closes = datetime.datetime.combine(
r.date() + datetime.timedelta(days=1),
day.closes)
else:
closes = datetime.datetime.combine(r.date(), day.closes)
opens = datetime.datetime.combine(r.date(), day.opens)
dates[r.date()] = OpenHours(opens, closes)
for period in resource.overlapping_periods:
def make_calendar_heatmap_fig(n, start_date, end_date):
start_date = arrow.get(start_date)
end_date = arrow.get(end_date)
categories = ["BAT", "Diary", "Exercise"]
dates = []
z = []
for _ in categories:
z.append([])
for r in arrow.Arrow.range("day", start_date, end_date):
offset_day = (arrow.now() - r).days
record_data = data_handler.read_record(days=-offset_day)
summary = record_data.get("summary", {})
for i, category in enumerate(categories):
do_category = summary.get(f"do_{category.lower()}", False)
z[i].append(int(do_category))
dates.append(r.format("YYYY-MM-DD"))
categories.append("All")
z_do_all = []
for i in range(len(dates)):
do_all = 0
for item in z:
categories = copy.deepcopy(task_categories)
categories.append("Empty")
task_reports = {}
colors = {"Empty": "#DEDEDE"}
WEEKDAY_SUNDAY = 6
sunday_dates = data_handler.get_base_of_range(start_date, end_date, weekday_value=WEEKDAY_SUNDAY)
for c in categories:
task_reports[c] = [0] * len(sunday_dates)
weekly_index = 0
for r in arrow.Arrow.range("day", start_date, end_date):
offset_day = (arrow.now() - r).days
record_data = data_handler.read_record(days=-offset_day)
for weekly_index, base_date in enumerate(sunday_dates):
days_diff = (base_date - r).days
if days_diff < 7 and days_diff >= 0:
break
activity_data = record_data.get("activity", {})
task_data = activity_data.get("task", [])
for t in task_data:
project = t["project"]
duration = (arrow.get(t["end_time"]) - arrow.get(t["start_time"])).seconds
duration_hours = round(duration / 60 / 60, 1)
the intervals is the wrong way to do it. The problem is the uneven months and leapyears
leads to inaccuracies. So this is the new way of doing it.
Use the rrule to get a list of all the dates that fall inside the given range. If you count
the dates (whether the frequency is YEARLY, MONTHLY, WEEKLY etc.) then the count will tell
you how many intervals fall inside the range. Here's the clever bit: the last date inside
the range is kind of your remainder. Set that to the start date for the next calculation and
you pick up at the starting poing where the last count ended!
Note. We knock one of the count because rrule includes the date you're counting from in the list,
which you don't really want.
:param interval: The interval we're calculating over: year, month, week, day, hour, minute or second.
:param start_datetime: When you're counting from
:param end_datetime: When you're counting to.
:return:
"""
datetime_list = arrow.Arrow.range(interval, start_datetime, end_datetime)
if datetime_list:
return len(datetime_list) - 1, datetime_list[-1]
else:
return 0, start_datetime
def accepted_query_params(self):
"""
provide to visualSearch.js, used for autocomplete.
user query via URL search.
autocomplete params key/value.
"""
# date range related.
days_range = arrow.Arrow.range("day",
ArrowParameter.get(self.date_begin),
ArrowParameter.get(self.date_end))
accepted_date_values = sorted(map(str, days_range))
# result
return {
"date_value": accepted_date_values,
"task_cls": self.ptm.task_class_names,
"luiti_package": self.ptm.task_package_names,
}
if pattern in ('YYYY', 'YY'):
interval = 'year'
elif pattern in ('YYYY.MM', 'YY.MM', 'YYYY.M', 'YY.M'):
interval = 'month'
elif pattern in ('YYYY.MM.DD', 'YY.MM.DD'):
interval = 'day'
elif pattern in ('YYYY.MM.DD.HH', 'YY.MM.DD.HH'):
interval = 'hour'
else:
interval = m.group(3) # the interval was specified, we dont need to guess
if base_name is None:
raise Exception("Base name for index could not be found, please specify a base name like: 'some-index-name-'")
retl = []
for r in arrow.Arrow.range(interval, start, end):
idx_name = "%s%s" % ( base_name, r.format(pattern) )
retl.append(idx_name)
return ','.join(retl)
x_data = [day.format('YYYY-MM-DD')
for day in arrow.Arrow.range('day', start_time, end_time)]
elif time_unit == 'month':
if start_time:
start_time = arrow.get(start_time)
else:
start_time = end_time.shift(years=-1)
x_data = [month.format('YYYY-MM')
for month in arrow.Arrow.range('month', start_time, end_time)]
elif time_unit == 'year':
if start_time:
start_time = arrow.get(start_time)
else:
start_time = end_time.shift(years=-5)
x_data = [year.format('YYYY')
for year in arrow.Arrow.range('year', start_time, end_time)]
else:
raise AttributeError('time_unit')
charts_config = {'start_time': start_time.naive, 'x_data': x_data[1:-1]}
return charts_config