~jconti/recent-notifications/trunk

« back to all changes in this revision

Viewing changes to recent_notifications/Timestamp.py

  • Committer: Jason Conti
  • Date: 2010-02-18 20:17:33 UTC
  • Revision ID: jason.conti@gmail.com-20100218201733-i671lnpp0g78nyco
Adding template for empty icon.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
"""
2
 
Timestamp.py
3
 
by Jason Conti
4
 
March 15, 2010
5
 
 
6
 
Functions for working with timestamps.
7
 
"""
8
 
 
9
 
import locale
10
 
 
11
 
from datetime import datetime
12
 
 
13
 
from Translations import _, ngettext
14
 
 
15
 
def now():
16
 
  """Returns a datetime objects with the current timestamp."""
17
 
  return datetime.now()
18
 
 
19
 
def locale_date(timestamp):
20
 
  """Returns a string with the date in the current locales format."""
21
 
  return timestamp.strftime(locale.nl_langinfo(locale.D_FMT))
22
 
 
23
 
def locale_datetime(timestamp):
24
 
  """Returns a string with the date and time in the current locales format."""
25
 
  return timestamp.strftime(locale.nl_langinfo(locale.D_T_FMT))
26
 
 
27
 
def locale_time(timestamp):
28
 
  """Returns a string with the time in the current locales format."""
29
 
  return timestamp.strftime(locale.nl_langinfo(locale.T_FMT))
30
 
 
31
 
def time_ago_in_words(timestamp):
32
 
  """Returns a string in a format such as "less than a minute ago" or "2 hours ago"
33
 
  the distance in time from timestamp to now. If the difference is more than
34
 
  a day, returns locale_datetime instead."""
35
 
  delta = now() - timestamp
36
 
  s = delta.seconds
37
 
  m = s / 60
38
 
  h = m / 60
39
 
  d = delta.days
40
 
 
41
 
  if d < 1:
42
 
    # Seconds
43
 
    if s < 60:
44
 
      return _("less than a minute ago")
45
 
    # Minutes
46
 
    elif m < 60:
47
 
      return ngettext("1 minute ago", "{0} minutes ago", m).format(m)
48
 
    # Hours
49
 
    else:
50
 
      return ngettext("1 hour ago", "{0} hours ago", h).format(h)
51
 
 
52
 
  # Greater than a day
53
 
  else:
54
 
    return locale_datetime(timestamp)
55