|
| 1 | +from models.time import Time, TimeType |
| 2 | +import logging, os |
| 3 | + |
| 4 | +logger = logging.getLogger(__name__) |
| 5 | + |
| 6 | +class Times(): |
| 7 | + start_time = None |
| 8 | + end_time = None |
| 9 | + |
| 10 | + @classmethod |
| 11 | + def from_shortcut_string(cls, times_string, assume_type=None): |
| 12 | + """ |
| 13 | + create a time object from a string |
| 14 | + """ |
| 15 | + logger.debug("creating times object from shortcut: " + times_string) |
| 16 | + if times_string is None: |
| 17 | + raise TypeError("Cannot create Times Object from value None") |
| 18 | + |
| 19 | + day = times_string.lower() |
| 20 | + |
| 21 | + # set up some shortcut ranges |
| 22 | + allday = cls(Time(0, 0, TimeType.AM), Time(11, 59, TimeType.PM)) |
| 23 | + workhours = cls(Time(9, 0, TimeType.AM), Time(5, 0, TimeType.PM)) |
| 24 | + |
| 25 | + if "24" in day: |
| 26 | + return allday |
| 27 | + elif "business" in day: |
| 28 | + return workhours |
| 29 | + elif "work" in day: |
| 30 | + return workhours |
| 31 | + elif "all day" in day: |
| 32 | + return allday |
| 33 | + |
| 34 | + raise ValueError("string '" + times_string + "' does not match a known pattern") |
| 35 | + |
| 36 | + @classmethod |
| 37 | + def from_parse_results(cls, result, assume_type=None): |
| 38 | + """ Takes values from the pyparsing results and converts them to the appropriate internal objects """ |
| 39 | + # assumes that all three (hours, minutes, am_pm) are the same length |
| 40 | + res_dct = result.asDict() |
| 41 | + |
| 42 | + start = res_dct.get("starttime")[0] |
| 43 | + starttime = Time.from_parse_results(start) |
| 44 | + |
| 45 | + end = res_dct.get("endtime")[0] |
| 46 | + endtime = Time.from_parse_results(end) |
| 47 | + |
| 48 | + if starttime.is_unknown() and assume_type is not None: |
| 49 | + starttime.set_type(assume_type) |
| 50 | + |
| 51 | + if endtime.is_unknown() and assume_type is not None: |
| 52 | + endtime.set_type(assume_type) |
| 53 | + |
| 54 | + |
| 55 | + if starttime.is_am() and endtime.is_am() and starttime.get_hours() > endtime.get_hours(): |
| 56 | + endtime.set_type(TimeType.PM) |
| 57 | + |
| 58 | + return cls(starttime, endtime) |
| 59 | + |
| 60 | + def __init__(self, start_time, end_time): |
| 61 | + if start_time is None or end_time is None: |
| 62 | + raise TypeError("Cannot create Times Object from value None") |
| 63 | + |
| 64 | + logger.debug("creating times from " + str(start_time) + " and " + str(end_time)) |
| 65 | + |
| 66 | + self.start_time = start_time |
| 67 | + self.end_time = end_time |
| 68 | + |
| 69 | + def get_start_time(self): |
| 70 | + return self.start_time |
| 71 | + |
| 72 | + def get_end_time(self): |
| 73 | + return self.end_time |
| 74 | + |
| 75 | + def __str__(self): |
| 76 | + return self.start_time + to + self.end_time |
| 77 | + |
| 78 | + |
| 79 | + def __eq__(self, other): |
| 80 | + if not isinstance(other, Times): |
| 81 | + # don't attempt to compare against unrelated types |
| 82 | + raise NotImplementedError() |
| 83 | + return self.start_time == other.start_time and self.end_time == other.end_time |
| 84 | + |
| 85 | + |
0 commit comments