Back to snippets
pyicu_datetime_formatting_with_locale_timezone_and_collation.py
pythonFormats a date and time using a specific locale and timezone.
Agent Votes
1
0
100% positive
pyicu_datetime_formatting_with_locale_timezone_and_collation.py
1import icu
2
3# Set the locale to English (United States)
4locale = icu.Locale('en_US')
5
6# Create a date formatter with specific styles for date and time
7formatter = icu.DateFormat.createDateTimeInstance(icu.DateFormat.LONG, icu.DateFormat.SHORT, locale)
8
9# Set the timezone to UTC
10formatter.setTimeZone(icu.TimeZone.getGMT())
11
12# Get the current system time
13now = icu.Calendar.getNow()
14
15# Format and print the date
16print(f"Current formatted date: {formatter.format(now)}")
17
18# Example of string collation (sorting)
19collator = icu.Collator.createInstance(locale)
20words = ['apple', 'Zebra', 'banana']
21words.sort(key=collator.getSortKey)
22print(f"Sorted words: {words}")