| 1 | ## fuzzyclock.py |
|---|
| 2 | ## |
|---|
| 3 | ## Contributors for this file: |
|---|
| 4 | ## |
|---|
| 5 | ## - Yann Le Boulanger <asterix@lagaule.org> |
|---|
| 6 | ## - Christoph Neuroth <delmonico@gmx.net> |
|---|
| 7 | ## |
|---|
| 8 | ## Copyright (C) 2006 Christoph Neuroth <delmonico@gmx.net> |
|---|
| 9 | ## Yann Le Boulanger <asterix@lagaule.org> |
|---|
| 10 | ## Dimitur Kirov <dkirov@gmail.com> |
|---|
| 11 | ## Travis Shirk <travis@pobox.com> |
|---|
| 12 | ## |
|---|
| 13 | ## This program is free software; you can redistribute it and/or modify |
|---|
| 14 | ## it under the terms of the GNU General Public License as published |
|---|
| 15 | ## by the Free Software Foundation; version 2 only. |
|---|
| 16 | ## |
|---|
| 17 | ## This program is distributed in the hope that it will be useful, |
|---|
| 18 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 19 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 20 | ## GNU General Public License for more details. |
|---|
| 21 | ## |
|---|
| 22 | |
|---|
| 23 | ''' |
|---|
| 24 | Python class to show a "fuzzy clock". |
|---|
| 25 | Homepage: http://home.gna.org/fuzzyclock/ |
|---|
| 26 | Project Page: http://gna.org/projects/fuzzyclock |
|---|
| 27 | |
|---|
| 28 | The class has been ported from PHP code by |
|---|
| 29 | Henrique Recidive <henrique at recidive.com> which was |
|---|
| 30 | in turn based on the Fuzzy Clock Applet of Frerich Raabe (KDE). |
|---|
| 31 | So most of the credit goes to this guys, thanks :-) |
|---|
| 32 | ''' |
|---|
| 33 | |
|---|
| 34 | import time |
|---|
| 35 | |
|---|
| 36 | class FuzzyClock: |
|---|
| 37 | def __init__(self): |
|---|
| 38 | self.__hour = 0 |
|---|
| 39 | self.__minute = 0 |
|---|
| 40 | self.__dayOfWeek = 0 |
|---|
| 41 | |
|---|
| 42 | self.__hourNames = [ _('one'), _('two'), _('three'), _('four'), _('five'), _('six'), |
|---|
| 43 | _('seven'), _('eight'), _('nine'), _('ten'), _('eleven'), |
|---|
| 44 | _('twelve')] |
|---|
| 45 | |
|---|
| 46 | #Strings to use for the output. %0 will be replaced with the preceding hour (e.g. "x PAST %0"), %1 with the coming hour (e.g. "x TO %1). ''' |
|---|
| 47 | self.__normalFuzzy = [ _("$0 o'clock"), _('five past $0'), |
|---|
| 48 | _('ten past $0'), _('quarter past $0'), |
|---|
| 49 | _('twenty past $0'), _('twenty five past $0'), |
|---|
| 50 | _('half past $0'), _('twenty five to $1'), |
|---|
| 51 | _('twenty to $1'), _('quarter to $1'), |
|---|
| 52 | _('ten to $1'), _('five to $1'), _("$1 o'clock") ] |
|---|
| 53 | |
|---|
| 54 | #A "singular-form". It is used when talking about hour 0 |
|---|
| 55 | self.__normalFuzzyOne = [ _("$0 o'clock"), _('five past $0'), |
|---|
| 56 | _('ten past $0'), _('quarter past $0'), |
|---|
| 57 | _('twenty past $0'), _('twenty five past $0'), |
|---|
| 58 | _('half past $0'), _('twenty five to $1'), |
|---|
| 59 | _('twenty to $1'), _('quarter to $1'), |
|---|
| 60 | _('ten to $1'), _('five to $1'), |
|---|
| 61 | _("$1 o'clock") ] |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | self.__dayTime = [ _('Night'), _('Early morning'), _('Morning'), _('Almost noon'), |
|---|
| 65 | _('Noon'), _('Afternoon'), _('Evening'), _('Late evening') ] |
|---|
| 66 | |
|---|
| 67 | self.__fuzzyWeek = [ _('Start of week'), _('Middle of week'), _('End of week'), |
|---|
| 68 | _('Weekend!') ] |
|---|
| 69 | |
|---|
| 70 | self.setCurrent() |
|---|
| 71 | |
|---|
| 72 | def setHour(self,hour): |
|---|
| 73 | self.__hour = int(hour) |
|---|
| 74 | |
|---|
| 75 | def setMinute(self,minute): |
|---|
| 76 | self.__minute=int(minute) |
|---|
| 77 | |
|---|
| 78 | def setDayOfWeek(self,day): |
|---|
| 79 | self.__dayOfWeek=int(day) |
|---|
| 80 | |
|---|
| 81 | def setTime(self,time): |
|---|
| 82 | timeArray = time.split(":") |
|---|
| 83 | self.setHour(timeArray[0]) |
|---|
| 84 | self.setMinute(timeArray[1]) |
|---|
| 85 | |
|---|
| 86 | def setCurrent(self): |
|---|
| 87 | hour=time.strftime("%H") |
|---|
| 88 | minute=time.strftime("%M") |
|---|
| 89 | day=time.strftime("%w") |
|---|
| 90 | |
|---|
| 91 | self.setTime(hour+":"+minute) |
|---|
| 92 | self.setDayOfWeek(day) |
|---|
| 93 | |
|---|
| 94 | def getFuzzyTime(self, fuzzyness = 1): |
|---|
| 95 | sector = 0 |
|---|
| 96 | realHour = 0 |
|---|
| 97 | |
|---|
| 98 | if fuzzyness == 1 or fuzzyness == 2: |
|---|
| 99 | if fuzzyness == 1: |
|---|
| 100 | if self.__minute >2: |
|---|
| 101 | sector = (self.__minute - 3) / 5 +1 |
|---|
| 102 | else: |
|---|
| 103 | if self.__minute > 6: |
|---|
| 104 | sector = ((self.__minute - 7) / 15 + 1) * 3 |
|---|
| 105 | |
|---|
| 106 | newTimeStr = self.__normalFuzzy[sector] |
|---|
| 107 | #$0 or $1? |
|---|
| 108 | deltaHour = int(newTimeStr[newTimeStr.find("$")+1]) |
|---|
| 109 | |
|---|
| 110 | if (self.__hour + deltaHour) % 12 > 0: |
|---|
| 111 | realHour = (self.__hour + deltaHour) % 12 - 1 |
|---|
| 112 | else: |
|---|
| 113 | realHour = 12 - ((self.__hour + deltaHour) % 12 + 1) |
|---|
| 114 | |
|---|
| 115 | if realHour == 0: |
|---|
| 116 | newTimeStr = self.__normalFuzzyOne[sector] |
|---|
| 117 | |
|---|
| 118 | newTimeStr = newTimeStr.replace("$"+str(deltaHour), |
|---|
| 119 | self.__hourNames[realHour]) |
|---|
| 120 | |
|---|
| 121 | |
|---|
| 122 | elif fuzzyness == 3: |
|---|
| 123 | newTimeStr = self.__dayTime[self.__hour / 3] |
|---|
| 124 | |
|---|
| 125 | else: |
|---|
| 126 | dayOfWeek = self.__dayOfWeek |
|---|
| 127 | if dayOfWeek == 1: |
|---|
| 128 | newTimeStr = self.__fuzzyWeek[0] |
|---|
| 129 | elif dayOfWeek >= 2 and dayOfWeek <= 4: |
|---|
| 130 | newTimeStr = self.__fuzzyWeek[1] |
|---|
| 131 | elif dayOfWeek == 5: |
|---|
| 132 | newTimeStr = self.__fuzzyWeek[2] |
|---|
| 133 | else: |
|---|
| 134 | newTimeStr = self.__fuzzyWeek[3] |
|---|
| 135 | |
|---|
| 136 | return newTimeStr |
|---|