| 1 from datetime import datetime, date, time, timedelta |
1 from datetime import datetime, date, time, timedelta |
| 2 |
2 |
| 3 def tänään(): |
3 def tänään(): |
| 4 return date.today() |
4 return date.today() |
| 5 #return date(2017, 4, 10) |
5 #return date(2017, 1, 10) |
| 6 |
6 |
| 7 def nyt(): |
7 def nyt(): |
| 8 return datetime.now() |
8 return datetime.now() |
| 9 #return datetime.combine(tänään(), datetime.now().time()) |
9 #return datetime.combine(tänään(), datetime.now().time()) |
| 10 |
10 |
| 11 def muotoile_aika(aika, suhteellinen = True): |
11 def muotoile_aika(aika, suhteellinen = True): |
| 12 erotus = aika - nyt() |
12 erotus = aika - nyt() |
| 13 if suhteellinen and erotus < timedelta(minutes = 30): |
13 if suhteellinen and erotus < timedelta(minutes = 1): |
| |
14 return 'nyt' |
| |
15 elif suhteellinen and erotus < timedelta(minutes = 10): |
| 14 return '%dm' % round(erotus.seconds / 60) |
16 return '%dm' % round(erotus.seconds / 60) |
| 15 elif aika.date() == tänään(): |
17 elif aika.date() == tänään(): |
| 16 return '%d:%02d' % (aika.hour, aika.minute) |
18 return '%d:%02d' % (aika.hour, aika.minute) |
| 17 elif erotus < timedelta(7): |
19 elif erotus < timedelta(7): |
| 18 return aika.strftime('%-a %H:%M') |
20 return aika.strftime('%-a %H:%M') |