format

advertisement
Date/Time/Number Formatting
Alan Liu
Globalization Center of Competency
IBM Emerging Technology Center
Cupertino, CA, USA / September, 2000
1
First ICU DeveloperWorkshop
Overview
Cupertino, CA, USA / September, 2000
2
First ICU DeveloperWorkshop
The Problem
• Display a date in different languages
– English:
– German:
– French:
Cupertino, CA, USA / September, 2000
Friday, June 4, 1999
Freitag, 4. Juni 1999
vendredi 4 juin 1999
3
First ICU DeveloperWorkshop
The Problem
• Display a date in different languages
• Convert a time between local time zones
– Los Angeles: June 3, 1999 5:00 PM PDT
– New York: June 3, 1999 8:00 PM EDT
– Paris:
June 4, 1999 2:00 AM CEST
Cupertino, CA, USA / September, 2000
4
First ICU DeveloperWorkshop
The Problem
• Display a date in different languages
• Convert a time between local time zones
• Handle daylight savings time
– Sat. Oct. 28, 2000 3:00 PM PDT + 24 hours =
– Sun. Oct. 29, 2000 2:00 PM PST
Cupertino, CA, USA / September, 2000
5
First ICU DeveloperWorkshop
The Problem
• These problems boil down to:
– Formatting
– Parsing
– Data conversion
• These operations apply to different data types:
– Dates and times
– Numbers, including currency amounts
– Messages
Cupertino, CA, USA / September, 2000
6
First ICU DeveloperWorkshop
Formatting and Parsing
• Formatting = conversion of binary data to
text
• Parsing = conversion of text to binary data
• Different locales use different text to
represent the same piece of binary data
Cupertino, CA, USA / September, 2000
7
First ICU DeveloperWorkshop
Data Conversion
• In different locales, not only the form but
also the value of displayed data may
change
• Reflects different local conventions: time
zone, definition of the week, calendar
• Applies only to dates and times
Cupertino, CA, USA / September, 2000
8
First ICU DeveloperWorkshop
Data Conversion
• Displayed data values differ depending on
the locale
• Underlying binary data does not change
• Displayed data: Calendar fields
• Underlying data: UDate scalar
Cupertino, CA, USA / September, 2000
9
First ICU DeveloperWorkshop
Data Conversion
• Conversion happens implicitly during
formatting and parsing
• Conversion can also be done explicitly
Cupertino, CA, USA / September, 2000
10
First ICU DeveloperWorkshop
Date and Time
Cupertino, CA, USA / September, 2000
11
First ICU DeveloperWorkshop
Date and Time
•
•
•
•
Date and time stored together as a scalar
Resolution of 1 millisecond
Range roughly 70,000 BC to 70,000 AD
Stored as absolute time independent of
local time zones (GMT-based)
Cupertino, CA, USA / September, 2000
12
First ICU DeveloperWorkshop
Date and Time
• Extensible calendar system
• Rule-based time zones with built-in data for
all modern international zones (over 400)
• Rule-based date format with built-in data
for many locales (over 140)
Cupertino, CA, USA / September, 2000
13
First ICU DeveloperWorkshop
Date and Time Components
•
•
•
•
UDate: GMT-based date and time (ms)
Calendar: Convert UDate to/from fields
TimeZone: Convert GMT to/from local zone
DateFormat: Convert UDate to/from text
Cupertino, CA, USA / September, 2000
14
First ICU DeveloperWorkshop
Date and Time Components
• Fit together in a specific nested pattern
DateFormat
Calendar
TimeZone
Cupertino, CA, USA / September, 2000
15
First ICU DeveloperWorkshop
Date and Time Components
•
Fit together in a specific nested pattern
1) Create
• TimeZone object zone
• Calendar object cal
• DateFormat object fmt
2) Set cal’s time zone to zone
3) Set fmt’s calendar to cal
Cupertino, CA, USA / September, 2000
16
First ICU DeveloperWorkshop
Date and Time Components
• If components are not nested explicitly,
then they use default objects, which can
lead to errors
• DateFormat uses a default
Calendar
• Calendar uses a default TimeZone
Cupertino, CA, USA / September, 2000
17
First ICU DeveloperWorkshop
Calendar: Two Roles
• Always set to a specific UDate
• Converts UDate to/from fields
– Fields are integer values for the month, day,
year, hour, minutes, day of week, etc.
– See calendar.h for complete field list
Cupertino, CA, USA / September, 2000
18
First ICU DeveloperWorkshop
Detail: Calendar Fields
• Conversion of fields to UDate
cal.clear();
cal.set(YEAR, 1999);
cal.set(MONTH, JUNE);
cal.set(DAY_OF_WEEK, MONDAY);
cal.set(WEEK_OF_MONTH, 2);
UDate d = cal.getTime();
Cupertino, CA, USA / September, 2000
19
First ICU DeveloperWorkshop
Detail: Calendar Fields
• Conversion of UDate to fields
cal.setTime(myUDate);
int32_t month = cal.get(MONTH);
Cupertino, CA, USA / September, 2000
20
First ICU DeveloperWorkshop
Time Zones
•
•
•
•
•
Standard Olson (UNIX) time zone data
Over 400 international time zones
Does not rely on host OS
No historical time zone support
Time zones always used with calendars
Cupertino, CA, USA / September, 2000
21
First ICU DeveloperWorkshop
Formatters: Code + Patterns
• Code contains generic pattern engine
• Locale resources contain different patterns
– Different groups of patterns for different locales
– Within each group, different patterns for different
variants; e.g., one for “Wed 6 Sep 00” and another
for “Wednesday 7 September 2000”
Cupertino, CA, USA / September, 2000
22
First ICU DeveloperWorkshop
Formatter: Types
• DateFormat already discussed
• NumberFormat handles “$1,234.56”
• MessageFormat handles “3 Files
Found”
• ChoiceFormat handles “no
files”/“one file”
Cupertino, CA, USA / September, 2000
23
First ICU DeveloperWorkshop
Formatters: Pattern Syntax
• Each formatter supports its own pattern syntax
• For number and date formats, most users can
use locale data
• For message format, users will have to write
their own patterns
• See headers for pattern syntax documentation
Cupertino, CA, USA / September, 2000
24
First ICU DeveloperWorkshop
Future Directions
• Support for time zone aliasing
• Support for traditional calendar systems
• Support for rule-based number
formatting (e.g., “three hundred and
sixty-four”)
Cupertino, CA, USA / September, 2000
25
First ICU DeveloperWorkshop
Exercises
• Exercise 1 – datefmt
– Use DateFormat to format a date and
time into several localized strings
– Convert the date and time into local time
zones
Cupertino, CA, USA / September, 2000
26
First ICU DeveloperWorkshop
Exercises
• Exercise 2 – msgfmt
– Use MessageFormat to format a number
and date
– Use a ChoiceFormat pattern to format
the number idiomatically
Cupertino, CA, USA / September, 2000
27
First ICU DeveloperWorkshop
Download