[Sugar-devel] [PATCH Moon] Add date information to the information displayed - SL #2749

godiard at sugarlabs.org godiard at sugarlabs.org
Wed Feb 8 15:48:58 EST 2012


From: Gonzalo Odiard <godiard at gmail.com>

The patch set the locale to use calendar to get week days and mont days.

Signed-off-by: Flavio Danesse <fdanesse at gmail.com>
Reviewed-by: Gonzalo Odiard <gonzalo at laptop.org>
---
 moon.py |   19 ++++++++++++++++++-
 1 files changed, 18 insertions(+), 1 deletions(-)

diff --git a/moon.py b/moon.py
index f9eca9d..bd4bd2b 100755
--- a/moon.py
+++ b/moon.py
@@ -36,7 +36,9 @@ from sugar.graphics.toggletoolbutton import ToggleToolButton
 from gettext import gettext as _
 import math
 import time
+import calendar
 import os
+import locale
 
 try:
     import json
@@ -67,6 +69,12 @@ class MoonActivity(activity.Activity):
         self._name = handle
         self.set_title(_("Moon"))
         
+        # Set the locale to format dates
+        lang = os.getenv('LANG')
+        if lang.find('.') > -1:
+            lang = lang[:lang.find('.')]
+        locale.setlocale(locale.LC_TIME, lang)
+
         # Defaults (Resume priority, persistent file secondary, fall-back hardcoded)
         if handle.object_id == None:
             print "Launched from home."
@@ -242,12 +250,21 @@ class MoonActivity(activity.Activity):
         gobject.source_remove(self.update_moon_image_timeout)
         self.update_moon_image_view()
 
+    def format_date(self, date_time):
+        year = date_time.tm_year
+        month = calendar.month_name[date_time.tm_mon - 1]
+        day = date_time.tm_mday
+        week_day = calendar.day_name[date_time.tm_wday - 1]
+        return "%(week_day)s, %(day)s %(month)s %(year)s" % \
+                {'week_day':week_day, 'day':day, 'month':month, 'year':year}
+
     def update_text_information_view(self):
         """Generate Moon data and update text based information view.
         """
         self.data_model.update_moon_calculations(time.time())
         information_string = ""
-        information_string += _("Today's Moon Information\n\n")
+        information_string += _("Today's Moon Information\n")
+        information_string += "%s\n\n" % self.format_date(time.localtime())
         information_string += _("Phase:\n%s\n\n") % (self.data_model.moon_phase_name(self.data_model.phase_of_moon))
         information_string += _("Julian Date:\n%.2f (astronomical)\n\n") % (self.data_model.julian_date)
         information_string += _("Age:\n%(days).0f days, %(hours).0f hours, %(minutes).0f minutes\n\n") % {'days':self.data_model.days_old, 'hours':self.data_model.hours_old, 'minutes':self.data_model.minutes_old}
-- 
1.7.7.6



More information about the Sugar-devel mailing list