Stores and handles locale-specific information related to time. ATTRIBUTES: f_weekday -- full weekday names (7-item list) a_weekday -- abbreviated weekday names (7-item list) f_month -- full month names (13-item list; dummy value in [0], which i
| 28 | return locale.getlocale(locale.LC_TIME) |
| 29 | |
| 30 | class LocaleTime(object): |
| 31 | """Stores and handles locale-specific information related to time. |
| 32 | |
| 33 | ATTRIBUTES: |
| 34 | f_weekday -- full weekday names (7-item list) |
| 35 | a_weekday -- abbreviated weekday names (7-item list) |
| 36 | f_month -- full month names (13-item list; dummy value in [0], which |
| 37 | is added by code) |
| 38 | a_month -- abbreviated month names (13-item list, dummy value in |
| 39 | [0], which is added by code) |
| 40 | am_pm -- AM/PM representation (2-item list) |
| 41 | LC_date_time -- format string for date/time representation (string) |
| 42 | LC_date -- format string for date representation (string) |
| 43 | LC_time -- format string for time representation (string) |
| 44 | timezone -- daylight- and non-daylight-savings timezone representation |
| 45 | (2-item list of sets) |
| 46 | lang -- Language used by instance (2-item tuple) |
| 47 | """ |
| 48 | |
| 49 | def __init__(self): |
| 50 | """Set all attributes. |
| 51 | |
| 52 | Order of methods called matters for dependency reasons. |
| 53 | |
| 54 | The locale language is set at the offset and then checked again before |
| 55 | exiting. This is to make sure that the attributes were not set with a |
| 56 | mix of information from more than one locale. This would most likely |
| 57 | happen when using threads where one thread calls a locale-dependent |
| 58 | function while another thread changes the locale while the function in |
| 59 | the other thread is still running. Proper coding would call for |
| 60 | locks to prevent changing the locale while locale-dependent code is |
| 61 | running. The check here is done in case someone does not think about |
| 62 | doing this. |
| 63 | |
| 64 | Only other possible issue is if someone changed the timezone and did |
| 65 | not call tz.tzset . That is an issue for the programmer, though, |
| 66 | since changing the timezone is worthless without that call. |
| 67 | |
| 68 | """ |
| 69 | self.lang = _getlang() |
| 70 | self.__calc_weekday() |
| 71 | self.__calc_month() |
| 72 | self.__calc_am_pm() |
| 73 | self.__calc_timezone() |
| 74 | self.__calc_date_time() |
| 75 | if _getlang() != self.lang: |
| 76 | raise ValueError("locale changed during initialization") |
| 77 | if time.tzname != self.tzname or time.daylight != self.daylight: |
| 78 | raise ValueError("timezone changed during initialization") |
| 79 | |
| 80 | def __calc_weekday(self): |
| 81 | # Set self.a_weekday and self.f_weekday using the calendar |
| 82 | # module. |
| 83 | a_weekday = [calendar.day_abbr[i].lower() for i in range(7)] |
| 84 | f_weekday = [calendar.day_name[i].lower() for i in range(7)] |
| 85 | self.a_weekday = a_weekday |
| 86 | self.f_weekday = f_weekday |
| 87 |