blob: 00a417e3e14d4097eec6cf9b93e4a6256894742f [file] [log] [blame]
Srikanth Vavilapalli1725e492014-12-01 17:50:52 -08001#
2# Copyright (c) 2011,2012,2013 Big Switch Networks, Inc.
3#
4# Licensed under the Eclipse Public License, Version 1.0 (the
5# "License"); you may not use this file except in compliance with the
6# License. You may obtain a copy of the License at
7#
8# http://www.eclipse.org/legal/epl-v10.html
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13# implied. See the License for the specific language governing
14# permissions and limitations under the License.
15#
16
17import datetime
18
19def ungettext(a,b,count):
20 if count > 1: # plural
21 return b
22 return a
23
24def ugettext(a):
25 return a
26
27def timesince_sec(since):
28 chunks = (
29 (60 * 60 * 24 * 365, lambda n: ungettext('year', 'years', n)),
30 (60 * 60 * 24 * 30, lambda n: ungettext('month', 'months', n)),
31 (60 * 60 * 24 * 7, lambda n : ungettext('week', 'weeks', n)),
32 (60 * 60 * 24, lambda n : ungettext('day', 'days', n)),
33 (60 * 60, lambda n: ungettext('hour', 'hours', n)),
34 (60, lambda n: ungettext('minute', 'minutes', n))
35 )
36 for i, (seconds, name) in enumerate(chunks):
37 count = since // seconds
38 if count != 0:
39 break
40 s = ugettext('%(number)d %(type)s') % {'number': count, 'type': name(count)}
41 if i + 1 < len(chunks):
42 # Now get the second item
43 seconds2, name2 = chunks[i + 1]
44 count2 = (since - (seconds * count)) // seconds2
45 if count2 != 0:
46 s += ugettext(', %(number)d %(type)s') % {'number': count2, 'type': name2(count2)}
47 return s
48
49def timesince(d, now=None):
50
51 """
52 Takes two datetime objects and returns the time between d and now
53 as a nicely formatted string, e.g. "10 minutes". If d occurs after now,
54 then "0 minutes" is returned.
55
56 Units used are years, months, weeks, days, hours, and minutes.
57 Seconds and microseconds are ignored. Up to two adjacent units will be
58 displayed. For example, "2 weeks, 3 days" and "1 year, 3 months" are
59 possible outputs, but "2 weeks, 3 hours" and "1 year, 5 days" are not.
60
61 Adapted from http://blog.natbat.co.uk/archive/2003/Jun/14/time_since
62 """
63 # Convert datetime.date to datetime.datetime for comparison.
64 if not isinstance(d, datetime.datetime):
65 d = datetime.datetime(d.year, d.month, d.day)
66 if now and not isinstance(now, datetime.datetime):
67 now = datetime.datetime(now.year, now.month, now.day)
68
69 if not now:
70 if d.tzinfo:
71 now = datetime.datetime.now(d.tzinfo)
72 else:
73 now = datetime.datetime.now()
74
75 # ignore microsecond part of 'd' since we removed it from 'now'
76 delta = now - (d - datetime.timedelta(0, 0, d.microsecond))
77 since = delta.days * 24 * 60 * 60 + delta.seconds
78 if since < 0:
79 # d is in the future compared to now, stop processing.
80 return 'future'
81 if since == 0:
82 return '0 minute' # possbibly a better string here?
83 return timesince_sec(since)