Yuan Yijun (bbbush) wrote,
Yuan Yijun
bbbush

How to simplify such code?


 1 def generate_time_string(t):

 2   d = datetime.datetime.today().utcnow() - t

 3   if d.days < 0:

 4     sec = - (d.days * 24 * 60 * 60 + d.seconds)

 5   else:

 6     sec = d.days * 24 * 60 * 60 + d.seconds

 7   min = sec / 60

 8   hour = min / 60

 9   day = hour / 24

10   if day > 0:

11     if d.days < 0:

12       return gettext.ngettext("%d day in the future", \

13         "%d days in the future", day) % day

14     else:

15       return gettext.ngettext("%d day ago", \

16         "%d days ago", day) % day

17   elif hour > 0:

18     if d.days < 0:

19       return gettext.ngettext("%d hour in the future", \

20         "%d hours in the future", hour) % hour

21     else:

22       return gettext.ngettext("%d hour ago", \

23         "%d hours ago", hour) % hour

24   elif min > 0:

25     if d.days < 0:

26       return gettext.ngettext("%d minute in the future", \

27         "%d minutes in the future", min) % min

28     else:

29       return gettext.ngettext("%d minute ago", \

30         "%d minutes ago", min) % min

31   else:

32     if d.days < 0:

33       return gettext.ngettext("%d second in the future", \

34         "%d seconds in the future", sec) % sec

35     else:

36       return gettext.ngettext("%d second ago", \

37         "%d seconds ago", sec) % sec

Tags: fedora, python
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 3 comments