def test_cut(self): # # Cut # self.assertTrue(Range('from january 10th 2010 to february 2nd 2010').cut('10 days') == Range('from january 10th 2010 to jan 20th 2010')) self.assertTrue(Date("jan 10") + '1 day' == Date("jan 11")) self.assertTrue(Date("jan 10") - '5 day' == Date("jan 5"))
def test_psql_infinity(self): d = Date('infinity') self.assertTrue(d > 'now') self.assertTrue(d > 'today') self.assertTrue(d > 'next week') self.assertFalse(d in Range('this year')) self.assertFalse(d in Range('next 5 years')) self.assertTrue(Range('month') < d) r = Range('today', 'infinity') self.assertTrue('next 5 years' in r) self.assertTrue(Date('today') in r) self.assertTrue(d in r) self.assertFalse(d > r) self.assertFalse(r > d) r = Range('["2013-12-09 06:57:46.54502-05",infinity)') self.assertTrue(r.end == 'infinity') self.assertTrue('next 5 years' in r) self.assertTrue(Date('today') in r) self.assertTrue(d in r) self.assertFalse(d > r) self.assertFalse(r > d) self.assertEqual(r.start.year, 2013) self.assertEqual(r.start.month, 12) self.assertEqual(r.start.day, 9) self.assertEqual(r.start.hour, 11) self.assertEqual(r.start.minute, 57) self.assertEqual(r.start.second, 46)
def test_dates(self): date = Date("August 25th, 2014 12:30 PM") [ self.assertEqual(*m) for m in ((date.year, 2014), (date.month, 8), (date.day, 25), (date.hour, 12), (date.minute, 30), (date.second, 0)) ] date = Date("may 23, 2018 1 pm") [ self.assertEqual(*m) for m in ((date.year, 2018), (date.month, 5), (date.day, 23), (date.hour, 13), (date.minute, 0), (date.second, 0)) ] date = Date("1-2-13 2 am") [ self.assertEqual(*m) for m in ((date.year, 2013), (date.month, 1), (date.day, 2), (date.hour, 2), (date.minute, 0), (date.second, 0)) ] date = Date("dec 15th '01 at 6:25:01 am") [ self.assertEqual(*m) for m in ((date.year, 2001), (date.month, 12), (date.day, 15), (date.hour, 6), (date.minute, 25), (date.second, 1)) ]
def test_in(self): # # in # self.assertTrue(Date('yesterday') in Range("last 7 days")) self.assertTrue(Date('today') in Range('this month')) self.assertTrue(Date('today') in Range('this month')) self.assertTrue(Range('this month') in Range('this year')) self.assertTrue(Range('this day') in Range('this week'))
def test_offset(self): now = datetime.now() # # Offset # self.assertEqual(Date("today", offset=dict(hour=6)).hour, 6) self.assertEqual(Date("today", offset=dict(hour=6)).day, now.day) self.assertEqual(Range("this week", offset=dict(hour=10)).start.hour, 10) self.assertEqual(Date("yesterday", offset=dict(hour=10)).hour, 10) self.assertEqual(Date("august 25th 7:30am", offset=dict(hour=10)).hour, 7)
def match_data(soup, url): dict = {} dict['date'] = Date(url[url.find('matches/') + 8:url.find('/england')]) if (Date(dict['date']) + 85000) < datetime.now(): dict['finalscore'] = soup.find('h3', { 'class': 'thick scoretime ' }).string.strip() if dict['finalscore'] != '0 - 0': dict['scores'] = [] try: goals = soup.find( 'div', {'id': 'page_match_1_block_match_goals_13-wrapper'}) for g in goals.find_all('tr', {'class': 'event expanded'}): dict['scores'].append({ g.find('span', { 'class': 'minute' }).string: g.find('td', { 'class': 'event-icon' }).string }) except: try: goals = soup.find( 'div', {'id': 'page_match_1_block_match_goals_12-wrapper'}) for g in goals.find_all('tr', {'class': 'event expanded'}): dict['scores'].append({ g.find('span', { 'class': 'minute' }).string: g.find('td', { 'class': 'event-icon' }).string }) except: dict['scores'] = 'Missing' dict['teamH'] = soup.find( 'div', { 'class': 'block_match_info real-content clearfix ' }).find('h3', { 'class': 'thick' }).findNext('a').string dict['teamA'] = \ soup.find('div', {'class': 'block_match_info real-content clearfix '}).find_all('h3', {'class': 'thick'})[ 1].findNext('a').string dict['date'] = str(dict['date']) return dict
def assert_date(self, date_str, expected: datetime, **kw): _date = Date(date_str, **kw) self.assertEqual( _date, expected, '\n Now: %s' % str(datetime.now()) + '\n Text: %s' % date_str + '\nExpected: %s' % str(expected) + '\n Actual: %s' % str(_date))
def test_weekdays(self): now = datetime.now() for i, day in enumerate(('monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday')): d = Date(day) self.assertLess(d.date - now, timedelta(7), day) self.assertEqual(d.weekday, i, day) self.assertEqual(d.isoweekday, 1 + i, day)
def test_adjustment(self): d = Date('Jan 1st 2014 at 10 am') self.assert_date(d, datetime(2014, 1, 1, 10)) d.hour = 5 d.day = 15 d.month = 4 d.year = 2013 d.minute = 40 d.second = 14 d.microsecond = 10001 self.assertEqual(d, datetime(2013, 4, 15, 5, 40, 14, 10001)) self.assertEqual(str(d.date), '2013-04-15 05:40:14.010001')
def frame(matrix_borrow): # create data for frame matrix_frame = [] for the_row in matrix_borrow: added = the_row[6] returning = the_row[7] if not added.startswith("Bor") and not returning.startswith("Ret"): date_obj = Date(added).date ret_date_obj = Date(returning).date now = dtime.now() days_kept = now - date_obj days_left = ret_date_obj - now new_row = the_row new_row.append(days_kept.days) new_row.append(days_left.days) matrix_frame.append(new_row) return matrix_frame
def test_plus(self): date_1 = Date('jan 10') date_1.microsecond = 1 date_2 = Date('jan 11') date_2.microsecond = 1 self.assertEqual(date_1 + '1 day', date_2) date1 = Date('october 18, 2013 10:04:32 PM') date2 = date1 + '10 seconds' self.assertEqual(date1.second + 10, date2.second)
def test_dow(self): # # DOW # for x, day in enumerate(('monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'Satruday', 'sunday')): d, r = Date(day), Range(day) self.assertEqual(d.hour, 0) self.assertEqual(d.weekday, 1 + x) # length is 1 day in seconds self.assertEqual(len(r), 86400) self.assertEqual(r.start.hour, 0) self.assertEqual(r.end.hour, 0) self.assertEqual(r.end.weekday, 1 if x+1 == 7 else (2+x))
def test_last(self): now = datetime.now() # # last year # year = Range('last year') self.assertEqual(year.start.year, now.year - 1) self.assertEqual(year.start.month, now.month) self.assertEqual(year.start.day, now.day) self.assertEqual(year.start.hour, 0) self.assertEqual(year.start.minute, 0) self.assertEqual(year.end.year, now.year) self.assertEqual(year.end.month, now.month) self.assertEqual(year.end.day, now.day) self.assertEqual(year.end.hour, 0) self.assertEqual(year.end.minute, 0) self.assertTrue(Date('today') in year) self.assertTrue(Date('last tuesday') in Range('8 days')) self.assertTrue(Date('monday') in Range('8 days')) self.assertTrue(Date('last fri') in Range('8 days')) self.assertEqual(Range('1 year ago'), Range('last year')) self.assertEqual(Range('year ago'), Range('last year'))
def test_offset(self): self.assert_date('now', datetime(2017, 6, 16, 19, 3, 22), offset=dict(minute=3)) self.assert_date('today', datetime(2017, 6, 5, 4, 3, 2, 1), offset=dict(day=5, hour=4, minute=3, second=2, microsecond=1)) self.assert_date('yesterday', datetime(2017, 6, 1, 2, 3, 4, 5), offset=dict(day=1, hour=2, minute=3, second=4, microsecond=5)) self.assertEqual( Date('august 25th 7:30am', offset=dict(hour=10)).hour, 7)
def test_date_and_time_formats(self): for date_str in ['sep 5 @ 11am', 'sep 5 @ 11am', '11am, sep 5']: # TODO sep 5 11pm # TODO sep 5, 11pm self.assert_date(date_str, datetime(2017, 9, 5, 11, 0, 0)) for date_str in [ '09/05/2012 at 7:35pm' 'Sep 5th, 2012 at 7:35 pm' '2012 sep 5 7:35PM' '7:35 pm on sept 5 2012' '2012-09-05T19:35:00' 'September 5th, 2012 7:35 PM' 'September 5, 2012 7:35 pm', 'September 5th, 2012 at 7:35pm' '5th of September, 2012 at 7:35pm' '9-5-12 7:35 pm' "sep 5th '12 at 7:35:00 am" ]: self.assert_date(date_str, datetime(2012, 9, 5, 19, 35, 0)) # Offset timezone self.assertEqual(Date('2014-03-06 15:33:43.764419-05').hour, 20)
def test_date_adjustment(self): d = Date("Jan 1st 2014 at 10 am") self.assertEqual(d.year, 2014) self.assertEqual(d.month, 1) self.assertEqual(d.day, 1) self.assertEqual(d.hour, 10) self.assertEqual(d.minute, 0) self.assertEqual(d.second, 0) d.hour = 5 d.day = 15 d.month = 4 d.year = 2013 d.minute = 40 d.second = 14 self.assertEqual(d.year, 2013) self.assertEqual(d.month, 4) self.assertEqual(d.day, 15) self.assertEqual(d.hour, 5) self.assertEqual(d.minute, 40) self.assertEqual(d.second, 14) self.assertEqual(str(d.date), "2013-04-15 05:40:14")
def test_date_adjustment(self): d = Date("Jan 1st 2014 at 10 am") self.assertEqual(d.year, 2014) self.assertEqual(d.month, 1) self.assertEqual(d.day, 1) self.assertEqual(d.hour, 10) self.assertEqual(d.minute, 0) self.assertEqual(d.second, 0) d.hour = 5 d.day = 15 d.month = 4 d.year = 2013 d.minute = 40 d.second = 14 self.assertEqual(d.year, 2013) self.assertEqual(d.month, 4) self.assertEqual(d.day, 15) self.assertEqual(d.hour, 5) self.assertEqual(d.minute, 40) self.assertEqual(d.second, 14) self.assertEqual(str(d.date), "2013-04-15 05:40:14")
def do_reservation(self, args, arguments): """ :: Usage: reservation info --user=USER --project=PROJECT reservation list [--name=NAME] [--user=USER] [--project=PROJECT] [--hosts=HOSTS] [--start=TIME_START] [--end=TIME_END] [--format=FORMAT] reservation delete [all] [--user=USER] [--project=PROJECT] [--name=NAME] [--start=TIME_START] [--end=TIME_END] [--hosts=HOSTS] reservation delete --file=FILE reservation update --name=NAME [--start=TIME_START] [--end=TIME_END] [--user=USER] [--project=PROJECT] [--hosts=HOSTS] [--description=DESCRIPTION] reservation add --name=NAME [--start=TIME_START] [--end=TIME_END] [--user=USER] [--project=PROJECT] [--hosts=HOSTS] [--description=DESCRIPTION] reservation add --file=FILE Arguments: NAME Name of the reservation USER Registration will be done for this user PROJECT Project to be used HOSTS Hosts to reserve TIME_START Start time of reservation TIME_END End time of reservation FORMAT Format of output DESCRIPTION Description for reservation FILE File that contains reservation data to be added/ deleted Options: --name=NAME Names of the reservation --user=USER user name --project=PROJECT project id --start=TIME_START Start time of the reservation, in MM/DD/YYYY at hh:mm aa format. (default value: 01/01/1901 at 12:00 am]) --end=TIME_END End time of the reservation, in MM/DD/YYYY at hh:mm aa format. (default value: 12/31/2100 at 11:59 pm]) --host=HOSTS host name --description=DESCRIPTION description summary of the reservation --file=FILE Adding multiple reservations from one file --format=FORMAT Format is either table, json, yaml or csv [default: table] Description: reservation info lists the resources that support reservation for a given user or project. """ # print (arguments) def _print_dict(d, header=None, format='table'): if format == "json": return json.dumps(d, indent=4) elif format == "yaml": return pyaml.dump(d) elif format == "table": return Printer.write(d, order=[ "id", "name", "start_time", "end_time", "user", "project", "hosts", "description", "cloud" ], output="table", sort_keys=True) elif format == "csv": TODO.implement() else: return d # return Printer.write(d,order=['cm_id, name, fingerprint']) def _get_db_date_format(date): """ Utility Function that accepts instance of Date object and returns a string with Datetime for DB. :param date: Date object :return: Date as string with format expected in DB. """ db_date = "{:}-{:}-{:} {:}:{:}".format( str(date.month).zfill(2), str(date.day).zfill(2), str(date.year).zfill(4), str(date.hour).zfill(2), str(date.minute).zfill(2)) return db_date if arguments["info"]: TODO.implement() elif arguments["list"]: try: _name = arguments['--name'] _user = arguments['--user'] _project = arguments['--project'] _format = arguments['--format'] _hosts = arguments['--hosts'] _start = arguments['--start'] _end = arguments['--end'] _format = arguments['--format'] reserve = Reservation() dictionary = reserve.list(_name, _start, _end, _user, _project, _hosts) print(_print_dict(dictionary, format=_format)) msg = "info. OK." Console.ok(msg) except Exception as e: Error.traceback(e) Console.error("Problem listing reservations") elif arguments["delete"]: if arguments["all"]: try: reserve = Reservation() reserve.delete() msg = "info. OK." Console.ok(msg) except Exception as e: Error.traceback(e) Console.error("Problem deleting all reservations") else: try: _name = arguments['--name'] _user = arguments['--user'] _project = arguments['--project'] _format = arguments['--format'] _hosts = arguments['--hosts'] _start = arguments['--start'] _end = arguments['--end'] _format = arguments['--format'] reserve = Reservation() reserve.delete(_name, _start, _end, _user, _project, _hosts) msg = "info. OK." Console.ok(msg) except Exception as e: Error.traceback(e) Console.error("Problem deleting reservations") elif arguments["add"]: if arguments["--file"] is None: name = None try: name = arguments["--name"] hosts = arguments["--hosts"] user = arguments["--user"] project = arguments["--project"] description = arguments["--description"] start_time = arguments[ "--start"] or "01/01/1901 at 07:30 pm" end_time = arguments["--end"] or "12/31/2100 at 11:59 pm" stime = Date(start_time) etime = Date(end_time) reserve = Reservation() reserve.add(name, _get_db_date_format(stime), _get_db_date_format(etime), hosts=hosts, user=user, project=project, description=description) print("Reservation {:} added successfully".format(name)) msg = "info. OK." Console.ok(msg) except Exception as e: Error.traceback(e) Console.error( "Problem adding reservation {:}".format(name)) else: try: TODO.implement() """ with open(os.path.join(sys.path[0], arguments["--file"])) as file: reader = csv.reader(file) for row in reader: reservations = Reservation(cm_id=row[0], label=row[1], user=row[2], project=row[3], start_time=row[4], end_time=row[5], host=row[6], summary=row[7]) db.add() """ except Exception as e: print("Error in adding from file. ", e) elif arguments["update"]: name = None try: name = arguments["--name"] hosts = arguments["--hosts"] user = arguments["--user"] project = arguments["--project"] description = arguments["--description"] start_time = arguments["--start"] or "01/01/1901 at 07:30 pm" end_time = arguments["--end"] or "12/31/2100 at 11:59 pm" stime = Date(start_time) etime = Date(end_time) reserve = Reservation() reserve.update(name, _get_db_date_format(stime), _get_db_date_format(etime), hosts=hosts, user=user, project=project, description=description) print("Reservation {:} updated successfully".format(name)) msg = "info. OK." Console.ok(msg) except Exception as e: Error.traceback(e) Console.error("Problem updating reservation {:}".format(name)) return ""
def test_singles(self): now = datetime.now() # # Single check # self.assertEqual(Date("2012").year, 2012) self.assertEqual(Date("January 2013").month, 1) self.assertEqual(Date("feb 2011").month, 2) self.assertEqual(Date("05/23/2012").month, 5) self.assertEqual(Date("01/10/2015 at 7:30pm").month, 1) self.assertEqual(Date("today").day, now.day) self.assertEqual(Range('january')[0].month, 1) self.assertEqual(Range('january')[0].day, 1) self.assertEqual(Range('january')[0].hour, 0) self.assertEqual(Range('january')[1].month, 2) self.assertEqual(Range('january')[1].day, 1) self.assertEqual(Range('january')[1].hour, 0) self.assertEqual(Range('2010')[0].year, 2010) self.assertEqual(Range('2010')[0].month, 1) self.assertEqual(Range('2010')[0].day, 1) self.assertEqual(Range('2010')[0].hour, 0) self.assertEqual(Range('2010')[1].year, 2011) self.assertEqual(Range('2010')[1].month, 1) self.assertEqual(Range('2010')[1].day, 1) self.assertEqual(Range('2010')[1].hour, 0) self.assertEqual(Range('january 2011')[0].year, 2011) self.assertEqual(Range('january 2011')[0].month, 1) self.assertEqual(Range('january 2011')[0].day, 1) self.assertEqual(Range('january 2011')[0].hour, 0) self.assertEqual(Range('january 2011')[1].year, 2011) self.assertEqual(Range('january 2011')[1].month, 2) self.assertEqual(Range('january 2011')[1].day, 1) self.assertEqual(Range('january 2011')[1].hour, 0) self.assertEqual(Date(1374681560).year, 2013) self.assertEqual(Date(1374681560).month, 7) self.assertEqual(Date(1374681560).day, 24) self.assertEqual(Date(str(1374681560)).year, 2013) self.assertEqual(Date(str(1374681560)).month, 7) self.assertEqual(Date(str(1374681560)).day, 24) self.assertEqual(Range(1374681560).start.day, 24) self.assertEqual(Range(1374681560).end.day, 25) # offset timezones self.assertEqual(Date("2014-03-06 15:33:43.764419-05").hour, 20)
def from_db_value(self, value, expression, connection): if value is not None: return Date(decrypter(value).decode()).date else: return value
def test_fullstring(self): now = datetime.now() # # DATE # date = Date("01/10/2015 at 7:30pm") self.assertEqual(date.year, 2015) self.assertEqual(date.month, 1) self.assertEqual(date.day, 10) self.assertEqual(date.hour, 19) self.assertEqual(date.minute, 30) date = Date("may 23rd, 1988 at 6:24 am") self.assertEqual(date.year, 1988) self.assertEqual(date.month, 5) self.assertEqual(date.day, 23) self.assertEqual(date.hour, 6) self.assertEqual(date.minute, 24) # # RANGE # r = Range('From 04/17/13 04:18:00 to 05/01/13 17:01:00', tz='US/Central') self.assertEqual(r.start.year, 2013) self.assertEqual(r.start.month, 4) self.assertEqual(r.start.day, 17) self.assertEqual(r.start.hour, 4) self.assertEqual(r.start.minute, 18) self.assertEqual(r.end.year, 2013) self.assertEqual(r.end.month, 5) self.assertEqual(r.end.day, 1) self.assertEqual(r.end.hour, 17) self.assertEqual(r.end.minute, 1) _range = Range("between january 15th at 3 am and august 5th 5pm") self.assertEqual(_range[0].year, now.year) self.assertEqual(_range[0].month, 1) self.assertEqual(_range[0].day, 15) self.assertEqual(_range[0].hour, 3) self.assertEqual(_range[1].year, now.year) self.assertEqual(_range[1].month, 8) self.assertEqual(_range[1].day, 5) self.assertEqual(_range[1].hour, 17) _range = Range("2012 feb 2 1:13PM to 6:41 am on sept 8 2012") self.assertEqual(_range[0].year, 2012) self.assertEqual(_range[0].month, 2) self.assertEqual(_range[0].day, 2) self.assertEqual(_range[0].hour, 13) self.assertEqual(_range[0].minute, 13) self.assertEqual(_range[1].year, 2012) self.assertEqual(_range[1].month, 9) self.assertEqual(_range[1].day, 8) self.assertEqual(_range[1].hour, 6) self.assertEqual(_range[1].minute, 41) date = Date('2013-09-10T10:45:50') self.assertEqual(date.year, 2013) self.assertEqual(date.month, 9) self.assertEqual(date.day, 10) self.assertEqual(date.hour, 10) self.assertEqual(date.minute, 45) self.assertEqual(date.second, 50) _range = Range('tomorrow 10am to 5pm') tomorrow = now + timedelta(days=1) self.assertEquals(_range.start.year, tomorrow.year) self.assertEquals(_range.end.year, tomorrow.year) self.assertEquals(_range.start.month, tomorrow.month) self.assertEquals(_range.end.month, tomorrow.month) self.assertEquals(_range.start.day, tomorrow.day) self.assertEquals(_range.end.day, tomorrow.day) self.assertEquals(_range.start.hour, 10) self.assertEquals(_range.end.hour, 17)
def test_exceptions(self): for x in ['yestserday', 'Satruday', Exception]: with self.assertRaises(TimestringInvalid): Date(x)
def test_tz(self): # # TZ # self.assertEqual(Date('today', tz="US/Central").tz.zone, 'US/Central')
def test_compare(self): self.assertFalse(Range('10 days') == Date('yestserday')) self.assertTrue(Date('yestserday') in Range('10 days')) self.assertTrue(Range('10 days') in Range('100 days')) self.assertTrue(Range('next 2 weeks') > Range('1 year')) self.assertTrue(Range('yesterday') < Range('now'))
def test_minus(self): date_1 = Date('jan 10') date_1.microsecond = 1 date_2 = Date('jan 5') date_2.microsecond = 1 self.assertEqual(Date(date_1) - '5 days', date_2)
def test_plus(self): date1 = Date("october 18, 2013 10:04:32 PM") date2 = date1 + "10 seconds" self.assertEqual(date1.second + 10, date2.second)
def test_tz(self): self.assertEqual(Date('today', tz='US/Central').tz.zone, 'US/Central')