def nice_time(dt, lang="en-us", speech=True, use_24hour=False, use_ampm=False): """ Format a time to a comfortable human format For example, generate 'five thirty' for speech or '5:30' for text display. Args: dt (datetime): date to format (assumes already in local timezone) lang (str): code for the language to use speech (bool): format for speech (default/True) or display (False) use_24hour (bool): output in 24-hour/military or 12-hour format use_ampm (bool): include the am/pm for 12-hour format Returns: (str): The formatted time string """ lang_lower = str(lang).lower() if lang_lower.startswith("en"): return nice_time_en(dt, speech, use_24hour, use_ampm) elif lang_lower.startswith("it"): return nice_time_it(dt, speech, use_24hour, use_ampm) elif lang_lower.startswith("fr"): return nice_time_fr(dt, speech, use_24hour, use_ampm) elif lang_lower.startswith("de"): return nice_time_de(dt, speech, use_24hour, use_ampm) elif lang_lower.startswith("hu"): return nice_time_hu(dt, speech, use_24hour, use_ampm) elif lang_lower.startswith("nl"): return nice_time_nl(dt, speech, use_24hour, use_ampm) # TODO: Other languages return str(dt)
def nice_time(dt, lang="en-us", speech=True, use_24hour=False, use_ampm=False): """ Format a time to a comfortable human format For example, generate 'five thirty' for speech or '5:30' for text display. Args: dt (datetime): date to format (assumes already in local timezone) lang (str): code for the language to use speech (bool): format for speech (default/True) or display (False) use_24hour (bool): output in 24-hour/military or 12-hour format use_ampm (bool): include the am/pm for 12-hour format Returns: (str): The formatted time string """ lang_lower = str(lang).lower() if lang_lower.startswith("en"): return nice_time_en(dt, speech, use_24hour, use_ampm) elif lang_lower.startswith("it"): return nice_time_it(dt, speech, use_24hour, use_ampm) elif lang_lower.startswith("fr"): return nice_time_fr(dt, speech, use_24hour, use_ampm) elif lang_lower.startswith("de"): return nice_time_de(dt, speech, use_24hour, use_ampm) # TODO: Other languages return str(dt)
def nice_time(dt, lang=None, speech=True, use_24hour=False, use_ampm=False): """ Format a time to a comfortable human format For example, generate 'five thirty' for speech or '5:30' for text display. Args: dt (datetime): date to format (assumes already in local timezone) lang (str): code for the language to use speech (bool): format for speech (default/True) or display (False) use_24hour (bool): output in 24-hour/military or 12-hour format use_ampm (bool): include the am/pm for 12-hour format Returns: (str): The formatted time string """ lang_code = get_primary_lang_code(lang) if lang_code == "en": return nice_time_en(dt, speech, use_24hour, use_ampm) elif lang_code == "es": return nice_time_es(dt, speech, use_24hour, use_ampm) elif lang_code == "it": return nice_time_it(dt, speech, use_24hour, use_ampm) elif lang_code == "fr": return nice_time_fr(dt, speech, use_24hour, use_ampm) elif lang_code == "de": return nice_time_de(dt, speech, use_24hour, use_ampm) elif lang_code == "hu": return nice_time_hu(dt, speech, use_24hour, use_ampm) elif lang_code == "nl": return nice_time_nl(dt, speech, use_24hour, use_ampm) elif lang_code == "da": return nice_time_da(dt, speech, use_24hour, use_ampm) elif lang_code == "pt": return nice_time_pt(dt, speech, use_24hour, use_ampm) elif lang_code == "sv": return nice_time_sv(dt, speech, use_24hour, use_ampm) # TODO: Other languages return str(dt)
def nice_time(dt, lang=None, speech=True, use_24hour=False, use_ampm=False): """ Format a time to a comfortable human format For example, generate 'five thirty' for speech or '5:30' for text display. Args: dt (datetime): date to format (assumes already in local timezone) lang (str): code for the language to use speech (bool): format for speech (default/True) or display (False) use_24hour (bool): output in 24-hour/military or 12-hour format use_ampm (bool): include the am/pm for 12-hour format Returns: (str): The formatted time string """ lang_code = get_primary_lang_code(lang) if lang_code == "en": return nice_time_en(dt, speech, use_24hour, use_ampm) elif lang_code == "es": return nice_time_es(dt, speech, use_24hour, use_ampm) elif lang_code == "it": return nice_time_it(dt, speech, use_24hour, use_ampm) elif lang_code == "fr": return nice_time_fr(dt, speech, use_24hour, use_ampm) elif lang_code == "de": return nice_time_de(dt, speech, use_24hour, use_ampm) elif lang_code == "hu": return nice_time_hu(dt, speech, use_24hour, use_ampm) elif lang_code == "nl": return nice_time_nl(dt, speech, use_24hour, use_ampm) elif lang_code == "da": return nice_time_da(dt, speech, use_24hour, use_ampm) elif lang_code == "pt": return nice_time_pt(dt, speech, use_24hour, use_ampm) # TODO: Other languages return str(dt)
def nice_time(dt, lang, speech=True, use_24hour=False, use_ampm=False): """ Format a time to a comfortable human format For example, generate 'five thirty' for speech or '5:30' for text display. Args: lang (string): ignored dt (datetime): date to format (assumes already in local timezone) speech (bool): format for speech (default/True) or display (False)=Fal use_24hour (bool): output in 24-hour/military or 12-hour format use_ampm (bool): include the am/pm for 12-hour format Returns: (str): The formatted time string """ # If language is German, use nice_time_de lang_lower = str(lang).lower() if lang_lower.startswith("de"): return nice_time_de(dt, speech, use_24hour, use_ampm) if use_24hour: # e.g. "03:01" or "14:22" string = dt.strftime("%H:%M") else: if use_ampm: # e.g. "3:01 AM" or "2:22 PM" string = dt.strftime("%I:%M %p") else: # e.g. "3:01" or "2:22" string = dt.strftime("%I:%M") if string[0] == '0': string = string[1:] # strip leading zeros if not speech: return string # Generate a speakable version of the time if use_24hour: speak = "" # Either "0 8 hundred" or "13 hundred" if string[0] == '0': speak += pronounce_number(int(string[0]), lang) + " " speak += pronounce_number(int(string[1]), lang) else: speak = pronounce_number(int(string[0:2]), lang) speak += " " if string[3:5] == '00': speak += "hundred" else: if string[3] == '0': speak += pronounce_number(0) + " " speak += pronounce_number(int(string[4]), lang) else: speak += pronounce_number(int(string[3:5]), lang) return speak else: if dt.hour == 0 and dt.minute == 0: return "midnight" if dt.hour == 12 and dt.minute == 0: return "noon" # TODO: "half past 3", "a quarter of 4" and other idiomatic times if dt.hour == 0: speak = pronounce_number(12, lang) elif dt.hour < 13: speak = pronounce_number(dt.hour, lang) else: speak = pronounce_number(dt.hour - 12, lang) if dt.minute == 0: if not use_ampm: return speak + " o'clock" else: if dt.minute < 10: speak += " oh" speak += " " + pronounce_number(dt.minute, lang) if use_ampm: if dt.hour > 11: speak += " PM" else: speak += " AM" return speak