コード例 #1
0
 def test_nice_date_time(self):
     # TODO: migrate these tests (in res files) to respect the new
     # language loading features. Right now, some of them break if
     # their languages are not default.
     for lang in self.test_config:
         set_default_lang(lang)
         i = 1
         while (self.test_config[lang].get('test_nice_date_time') and
                self.test_config[lang]['test_nice_date_time'].get(str(i))):
             p = self.test_config[lang]['test_nice_date_time'][str(i)]
             dp = ast.literal_eval(p['datetime_param'])
             np = ast.literal_eval(p['now'])
             dt = datetime.datetime(
                 dp[0], dp[1], dp[2], dp[3], dp[4], dp[5])
             now = None if not np else datetime.datetime(
                 np[0], np[1], np[2], np[3], np[4], np[5])
             print('Testing for ' + lang + ' that ' + str(dt) +
                   ' is date time ' + p['assertEqual'])
             self.assertEqual(
                 p['assertEqual'],
                 nice_date_time(
                     dt, lang=lang, now=now,
                     use_24hour=ast.literal_eval(p['use_24hour']),
                     use_ampm=ast.literal_eval(p['use_ampm'])))
             i = i + 1
     set_default_lang('en')
コード例 #2
0
 def test_nice_date_time(self):
     lang = "cs-cz"
     i = 1
     while (self.test_config[lang].get('test_nice_date_time') and
             self.test_config[lang]['test_nice_date_time'].get(str(i).encode('utf8'))):
         p = self.test_config[lang]['test_nice_date_time'][str(i)]
         dp = ast.literal_eval(p['datetime_param'])
         np = ast.literal_eval(p['now'])
         dt = datetime.datetime(
             dp[0], dp[1], dp[2], dp[3], dp[4], dp[5])
         now = None if not np else datetime.datetime(
             np[0], np[1], np[2], np[3], np[4], np[5])
         print('Testing for ' + lang + ' that ' + str(dt) +
                 ' is date time ' + p['assertEqual'])
         self.assertEqual(
             p['assertEqual'],
             nice_date_time(
                 dt, lang=lang, now=now,
                 use_24hour=ast.literal_eval(p['use_24hour']),
                 use_ampm=ast.literal_eval(p['use_ampm'])))
         i = i + 1
コード例 #3
0
# pronounce datetime objects
import datetime

dt = datetime.datetime(2017, 1, 31,  13, 22, 3)

assert nice_date(dt) == "tuesday, january thirty-first, twenty seventeen"

assert nice_time(dt) == "one twenty two"
assert nice_time(dt, use_ampm=True) ==  "one twenty two p.m."
assert nice_time(dt, speech=False) == "1:22"
assert nice_time(dt, speech=False, use_ampm=True) == "1:22 PM"
assert nice_time(dt, speech=False, use_24hour=True) == "13:22"
assert nice_time(dt, speech=False, use_24hour=True, use_ampm=True) == "13:22"
assert nice_time(dt, use_24hour=True, use_ampm=True) == "thirteen twenty two"
assert nice_time(dt, use_24hour=True, use_ampm=False) == "thirteen twenty two"

assert nice_date_time(dt) == "tuesday, january thirty-first, twenty seventeen at one twenty two"

# pronounce durations
assert nice_duration(1) ==   "one second"
assert nice_duration(3) ==   "three seconds"
assert nice_duration(1, speech=False) ==   "0:01"
assert nice_duration(61), "one minute one second"
assert nice_duration(61, speech=False) ==   "1:01"
assert nice_duration(5000) ==  "one hour twenty three minutes twenty seconds"
assert nice_duration(5000, speech=False), "1:23:20"
assert nice_duration(50000) ==   "thirteen hours fifty three minutes twenty seconds"
assert nice_duration(50000, speech=False) ==   "13:53:20"
assert nice_duration(500000) ==   "five days  eighteen hours fifty three minutes twenty seconds"
assert nice_duration(500000, speech=False), "5d 18:53:20"
assert nice_duration(datetime.timedelta(seconds=500000), speech=False) ==  "5d 18:53:20"