def test_date_from_string_week(self): dates = DateUtils.date_from_string("w1/2019") self.assertEqual(dates[0], datetime.date(2018, 12, 31)) self.assertEqual(dates[1], datetime.date(2019, 1, 6)) dates = DateUtils.date_from_string("w01/2019") self.assertEqual(dates[0], datetime.date(2018, 12, 31)) self.assertEqual(dates[1], datetime.date(2019, 1, 6)) dates = DateUtils.date_from_string("w01") self.assertEqual(dates[0], DateUtils.from_week_number(CURRENT_YEAR, 1)) self.assertEqual(dates[1], DateUtils.from_week_number(CURRENT_YEAR, 1, end=True)) dates = DateUtils.date_from_string("w52/2016") self.assertEqual(dates[0], datetime.date(2016, 12, 26)) self.assertEqual(dates[1], datetime.date(2017, 1, 1)) dates = DateUtils.date_from_string("w1/2017") self.assertEqual(dates[0], datetime.date(2017, 1, 2)) self.assertEqual(dates[1], datetime.date(2017, 1, 8)) with self.assertRaises(ValueError): dates = DateUtils.date_from_string("w02/232") with self.assertRaises(ValueError): dates = DateUtils.date_from_string("w111/2012") with self.assertRaises(ValueError): dates = DateUtils.date_from_string("w0/2012")
def test_date_from_string_half(self): dates = DateUtils.date_from_string("h1/2019") self.assertEqual(dates[0], datetime.date(2019, 1, 1)) self.assertEqual(dates[1], datetime.date(2019, 6, 30)) dates = DateUtils.date_from_string("H1/2019") self.assertEqual(dates[0], datetime.date(2019, 1, 1)) self.assertEqual(dates[1], datetime.date(2019, 6, 30)) dates = DateUtils.date_from_string("h2/2019") self.assertEqual(dates[0], datetime.date(2019, 7, 1)) self.assertEqual(dates[1], datetime.date(2019, 12, 31)) dates = DateUtils.date_from_string("H2/2019") self.assertEqual(dates[0], datetime.date(2019, 7, 1)) self.assertEqual(dates[1], datetime.date(2019, 12, 31)) with self.assertRaises(ValueError): dates = DateUtils.date_from_string("H2/232") with self.assertRaises(ValueError): dates = DateUtils.date_from_string("H2/00232") with self.assertRaises(ValueError): dates = DateUtils.date_from_string("H5/2012") with self.assertRaises(ValueError): dates = DateUtils.date_from_string("H0/2012") with self.assertRaises(ValueError): dates = DateUtils.date_from_string("H0234/2012")
def generate_report_for_period(period, table_contents): amount = period.time_worked() amount_onsite = period.time_worked(only_onsite=True) hours_worked_string = DateUtils.format_delta_as_hours(amount) if amount_onsite: hours_worked_string += " (%s onsite)" % \ DateUtils.format_delta_as_hours(amount_onsite) table_contents.append([period.short_string(), hours_worked_string]) return (amount, amount_onsite)
def is_valid_foreign_ssn(self, ssn): """ usage: KoreanUtils().is_valid_foreign_ssn('1234561234567') returns False refernece: http://www.egovframe.go.kr/wiki/doku.php?id=egovframework:%EB%B2%88%ED%98%B8%EC%9C%A0%ED%9A%A8%EC%84%B1%EC%B2%B4%ED%81%AC https://github.com/kenu/egov/blob/master/ok.commoncomp/src/main/java/egovframework/com/utl/fcc/service/EgovNumberCheckUtil.java """ if not ssn: return False # 입력값이 숫자인지 확인 if not ssn.isdigit(): # print('ssn is not digit.') return False # 외국인등록번호 자리수 확인 if 13 != len(ssn): # print('Length of ssn is not equal to 13.') return False # 외국인등록번호 앞자리 날짜 및 성별구분 숫자 유효성 체크 if 0 == int(ssn[0]) or 1 == int(ssn[0]): if not int(ssn[6]) in [7, 8]: # print('ssn[0]:' + ssn[0] + '|ssn[6]:' + ssn[6]) return False birthdate = '20' + str(ssn[0:6]) else: if not int(ssn[6]) in [5, 6]: # print('ssn[0]:' + ssn[0] + '|ssn[6]:' + ssn[6]) return False birthdate = '19' + str(ssn[0:6]) if not DateUtils().is_valid_date(birthdate): # print('birthdate: ' + birthdate) return False # 외국인등록번호 검증식 check = 0 for i in range(len(ssn) - 1): check += ((9 - i % 8) * int(ssn[i])) if 0 == check % 11: check = 1 elif 10 == check % 11: check = 0 else: check = check % 11 if 9 < check + 2: check = check + 2 - 10 else: check = check + 2 if int(ssn[12]) != check: return False return True
def __init__(self, start_date, num_days, task_filter=TaskFilter(), tasks=None): self.start_date = start_date self.num_days = num_days self.users = set() self.tasks = tasks self.task_filter = task_filter.create_same_filter_with_different_dates( self.start_date, DateUtils.from_date_offset(self.start_date, num_days - 1))
def add_task_if_starts_in_period(self, task): ending_date = DateUtils.from_date_offset(self.start_date, self.num_days) if task.date < self.start_date: return False if task.date >= ending_date: return False self.add_task(task) return True
def filter_task(task): if user is not None and task.user != user: return False if date is not None and not DateUtils.same_date(task.date, date): return False if only_onsite and not task.onsite: return False return True
def create_from_string(cls, string, task_filter): dates = DateUtils.date_range_from_string(string) weeks = cls.create_array_of_weeks_between_dates(dates[0], dates[1], task_filter) cls.fetch_tasks_for_all(weeks) return weeks
def generate_report(self): self.formatter.generate_header(self.header) table_contents = [] total = datetime.timedelta() total_onsite = datetime.timedelta() for period in self.time_periods: (time, time_onsite) = AggregateReport.generate_report_for_period(period, table_contents) total += time total_onsite += time_onsite self.formatter.generate_table(table_contents, has_headers=False) self.formatter.generate_header( "Total hours worked: %s" % DateUtils.format_delta_as_hours(total)) self.formatter.generate_header( "Total onsite hours worked: %s" % DateUtils.format_delta_as_hours(total_onsite)) return self.formatter.flatten()
def is_valid_korean_ssn(self, ssn): """ usage: KoreanUtils().is_valid_korean_ssn('1234561234567') returns False refernece: http://www.egovframe.go.kr/wiki/doku.php?id=egovframework:%EB%B2%88%ED%98%B8%EC%9C%A0%ED%9A%A8%EC%84%B1%EC%B2%B4%ED%81%AC https://github.com/kenu/egov/blob/master/ok.commoncomp/src/main/java/egovframework/com/utl/fcc/service/EgovNumberCheckUtil.java """ if not ssn: return False # 입력값이 숫자인지 확인 if not ssn.isdigit(): # print('ssn is not digit.') return False # 주민등록번호 자리수 확인 if 13 != len(ssn): # print('Length of ssn is not equal to 13.') return False # 주민등록번호 앞자리 날짜 및 성별구분 숫자 유효성 체크 if 0 == int(ssn[0]) or 1 == int(ssn[0]): if not int(ssn[6]) in [3, 4]: # print('ssn[0]:' + ssn[0] + '|ssn[6]:' + ssn[6]) return False birthdate = '20' + str(ssn[0:6]) else: if not int(ssn[6]) in [1, 2]: # print('ssn[0]:' + ssn[0] + '|ssn[6]:' + ssn[6]) return False birthdate = '19' + str(ssn[0:6]) if not DateUtils().is_valid_date(birthdate): # print('birthdate: ' + birthdate) return False id_add = '234567892345' # 주민등록번호에 가산할 값 total_id = 0 # 검증을 위한 변수 for index in range(len(ssn) - 1): count_num = int(ssn[index]) add_num = int(id_add[index]) total_id += count_num * add_num # 마지막 유효숫자와 검증식을 통한 값 비교 if (11 - (total_id % 11)) % 10 != int(ssn[12]): # print('total_id%11: ' + str(total_id % 11) + ' | 11-(total_id % 11))%10: ' + str((11 - (total_id % 11)) % 10) + ' | ssn[12]: ' + ssn[12]) return False return True
def filter_tasks(self, date=None, day_offset=None, user=None, only_onsite=False): if date is None and day_offset is not None: date = DateUtils.from_date_offset(self.start_date, day_offset) def filter_task(task): if user is not None and task.user != user: return False if date is not None and not DateUtils.same_date(task.date, date): return False if only_onsite and not task.onsite: return False return True return list(filter(filter_task, self.tasks))
def generate_hours(self): table = [] table.append([""] + list(map(DetailedReport.format_date, self.time_period.get_all_dates())) + ["Total"]) for user in sorted(self.time_period.get_users()): table.append([user.login] + self.time_worked(user=user) + self.time_worked(user=user, total=True)) table.append(["everyone"] + self.time_worked() + self.time_worked(total=True)) self.formatter.generate_table(table) onsite_time = self.time_period.time_worked(only_onsite=True) if onsite_time > datetime.timedelta(0): self.formatter.generate_large_text("Onsite hours worked: %s" % DateUtils.format_delta_as_hours(onsite_time))
def test_date_from_string_quarter(self): dates = DateUtils.date_from_string("q1/2019") self.assertEqual(dates[0], datetime.date(2019, 1, 1)) self.assertEqual(dates[1], datetime.date(2019, 3, 31)) dates = DateUtils.date_from_string("Q1/2019") self.assertEqual(dates[0], datetime.date(2019, 1, 1)) self.assertEqual(dates[1], datetime.date(2019, 3, 31)) dates = DateUtils.date_from_string("Q2") self.assertEqual(dates[0], datetime.date(CURRENT_YEAR, 4, 1)) self.assertEqual(dates[1], datetime.date(CURRENT_YEAR, 6, 30)) with self.assertRaises(ValueError): dates = DateUtils.date_from_string("Q2/232") with self.assertRaises(ValueError): dates = DateUtils.date_from_string("Q2/00232") with self.assertRaises(ValueError): dates = DateUtils.date_from_string("Q5/2012") with self.assertRaises(ValueError): dates = DateUtils.date_from_string("Q0/2012") with self.assertRaises(ValueError): dates = DateUtils.date_from_string("Q0234/2012")
def test_date_from_string_exact(self): dates = DateUtils.date_from_string("01/01/2018") self.assertEqual(dates[0], datetime.date(2018, 1, 1)) self.assertEqual(dates[1], datetime.date(2018, 1, 1)) dates = DateUtils.date_from_string("31/01/2018") self.assertEqual(dates[0], datetime.date(2018, 1, 31)) self.assertEqual(dates[1], datetime.date(2018, 1, 31)) with self.assertRaises(ValueError): dates = DateUtils.date_from_string("01/0/2018") with self.assertRaises(ValueError): dates = DateUtils.date_from_string("01/13/2018") with self.assertRaises(ValueError): dates = DateUtils.date_from_string("01/99/2018") with self.assertRaises(ValueError): dates = DateUtils.date_from_string("32/1/2018") with self.assertRaises(ValueError): dates = DateUtils.date_from_string("0/1/2018") dates = DateUtils.date_from_string("12/06/2323") self.assertEqual(dates[0], datetime.date(2323, 6, 12)) self.assertEqual(dates[1], datetime.date(2323, 6, 12))
def test_date_from_string_year(self): dates = DateUtils.date_from_string("2018") self.assertEqual(dates[0], datetime.date(2018, 1, 1)) self.assertEqual(dates[1], datetime.date(2018, 12, 31)) dates = DateUtils.date_from_string("3000") self.assertEqual(dates[0], datetime.date(3000, 1, 1)) self.assertEqual(dates[1], datetime.date(3000, 12, 31)) dates = DateUtils.date_from_string("1950") self.assertEqual(dates[0], datetime.date(1950, 1, 1)) self.assertEqual(dates[1], datetime.date(1950, 12, 31)) # We don't support years that don't have four digits. with self.assertRaises(ValueError): dates = DateUtils.date_from_string("659") with self.assertRaises(ValueError): dates = DateUtils.date_from_string("23") with self.assertRaises(ValueError): dates = DateUtils.date_from_string("1") with self.assertRaises(ValueError): dates = DateUtils.date_from_string("65900") with self.assertRaises(ValueError): dates = DateUtils.date_from_string("100000000000")
def test_date_from_string_month(self): dates = DateUtils.date_from_string("12/2018") self.assertEqual(dates[0], datetime.date(2018, 12, 1)) self.assertEqual(dates[1], datetime.date(2018, 12, 31)) dates = DateUtils.date_from_string("2/2016") self.assertEqual(dates[0], datetime.date(2016, 2, 1)) self.assertEqual(dates[1], datetime.date(2016, 2, 29)) dates = DateUtils.date_from_string("02/2016") self.assertEqual(dates[0], datetime.date(2016, 2, 1)) self.assertEqual(dates[1], datetime.date(2016, 2, 29)) # We don't support years that don't have four digits. with self.assertRaises(ValueError): dates = DateUtils.date_from_string("02/232") with self.assertRaises(ValueError): dates = DateUtils.date_from_string("111/2012") with self.assertRaises(ValueError): dates = DateUtils.date_from_string("0/2012")
def __init__(self, year, week, task_filter=TaskFilter(), tasks=None): self.week = week self.year = year date = DateUtils.from_week_number(year, week) super(WeekOfWork, self).__init__(date, 7, task_filter, tasks)
def get_all_dates(self): return [DateUtils.from_date_offset(self.start_date, offset) for offset in range(0, self.num_days)]
def test_date_range(self): dates = DateUtils.date_range_from_string("w11-") self.assertEqual(dates[0], DateUtils.date_from_string("w11")[0]) self.assertEqual(dates[1], datetime.date.today())
def time_worked(self, user=None, total=False): if total: return [DateUtils.format_delta_as_hours(self.time_period.time_worked(user=user))] all_dates = self.time_period.get_all_dates() return [DateUtils.format_delta_as_hours(self.time_period.time_worked(date=x, user=user)) for x in all_dates]
def test_last_day_of_month(self): self.assertEqual(DateUtils.last_day_of_month(2019, 3), 31) self.assertEqual(DateUtils.last_day_of_month(2018, 7), 31) self.assertEqual(DateUtils.last_day_of_month(2016, 2), 29) self.assertEqual(DateUtils.last_day_of_month(2017, 2), 28)
def create_array_of_weeks_between_dates(cls, start, end, task_filter): week_dates = DateUtils.get_weeks_in_date_range(start, end) weeks = [cls(*DateUtils.year_and_week_number(week_date), task_filter=task_filter, tasks=[]) for week_date in week_dates] return weeks