1/*
2 * Copyright (C) 2012 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1.  Redistributions of source code must retain the above copyright
8 *     notice, this list of conditions and the following disclaimer.
9 * 2.  Redistributions in binary form must reproduce the above copyright
10 *     notice, this list of conditions and the following disclaimer in the
11 *     documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
20 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
23 * DAMAGE.
24 */
25
26#ifndef DateTimeFormat_h
27#define DateTimeFormat_h
28
29#include "platform/PlatformExport.h"
30#include "wtf/Forward.h"
31
32namespace blink {
33
34// DateTimeFormat parses date time format defined in Unicode Technical
35// standard 35, Locale Data Markup Language (LDML)[1].
36// [1] LDML http://unicode.org/reports/tr35/tr35-6.html#Date_Format_Patterns
37class PLATFORM_EXPORT DateTimeFormat {
38public:
39    enum FieldType {
40        FieldTypeInvalid,
41        FieldTypeLiteral,
42
43        // Era: AD
44        FieldTypeEra = 'G',
45
46        // Year: 1996
47        FieldTypeYear = 'y',
48        FieldTypeYearOfWeekOfYear = 'Y',
49        FieldTypeExtendedYear = 'u',
50
51        // Quater: Q2
52        FieldTypeQuater = 'Q',
53        FieldTypeQuaterStandAlone = 'q',
54
55        // Month: September
56        FieldTypeMonth = 'M',
57        FieldTypeMonthStandAlone = 'L',
58
59        // Week: 42
60        FieldTypeWeekOfYear = 'w',
61        FieldTypeWeekOfMonth = 'W',
62
63        // Day: 12
64        FieldTypeDayOfMonth = 'd',
65        FieldTypeDayOfYear = 'D',
66        FieldTypeDayOfWeekInMonth = 'F',
67        FieldTypeModifiedJulianDay = 'g',
68
69        // Week Day: Tuesday
70        FieldTypeDayOfWeek = 'E',
71        FieldTypeLocalDayOfWeek = 'e',
72        FieldTypeLocalDayOfWeekStandAlon = 'c',
73
74        // Period: AM or PM
75        FieldTypePeriod = 'a',
76
77        // Hour: 7
78        FieldTypeHour12 = 'h',
79        FieldTypeHour23 = 'H',
80        FieldTypeHour11 = 'K',
81        FieldTypeHour24 = 'k',
82
83        // Minute: 59
84        FieldTypeMinute = 'm',
85
86        // Second: 12
87        FieldTypeSecond = 's',
88        FieldTypeFractionalSecond = 'S',
89        FieldTypeMillisecondsInDay = 'A',
90
91        // Zone: PDT
92        FieldTypeZone = 'z',
93        FieldTypeRFC822Zone = 'Z',
94        FieldTypeNonLocationZone = 'v',
95    };
96
97    class TokenHandler {
98    public:
99        virtual ~TokenHandler() { }
100        virtual void visitField(FieldType, int numberOfPatternCharacters) = 0;
101        virtual void visitLiteral(const String&) = 0;
102    };
103
104    // Returns true if succeeded, false if failed.
105    static bool parse(const String&, TokenHandler&);
106    static void quoteAndAppendLiteral(const String&, StringBuilder&);
107};
108
109} // namespace blink
110
111#endif // DateTimeFormat_h
112