1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 *******************************************************************************
5 * Copyright (C) 2016, International Business Machines
6 * Corporation and others.  All Rights Reserved.
7 *******************************************************************************
8 * dayperiodrules.h
9 *
10 * created on: 2016-01-20
11 * created by: kazede
12 */
13 
14 #ifndef DAYPERIODRULES_H
15 #define DAYPERIODRULES_H
16 
17 #include "unicode/locid.h"
18 #include "unicode/unistr.h"
19 #include "unicode/uobject.h"
20 #include "unicode/utypes.h"
21 #include "resource.h"
22 #include "uhash.h"
23 
24 
25 
26 U_NAMESPACE_BEGIN
27 
28 struct DayPeriodRulesDataSink;
29 
30 class DayPeriodRules : public UMemory {
31     friend struct DayPeriodRulesDataSink;
32 public:
33     enum DayPeriod {
34         DAYPERIOD_UNKNOWN = -1,
35         DAYPERIOD_MIDNIGHT,
36         DAYPERIOD_NOON,
37         DAYPERIOD_MORNING1,
38         DAYPERIOD_AFTERNOON1,
39         DAYPERIOD_EVENING1,
40         DAYPERIOD_NIGHT1,
41         DAYPERIOD_MORNING2,
42         DAYPERIOD_AFTERNOON2,
43         DAYPERIOD_EVENING2,
44         DAYPERIOD_NIGHT2,
45         DAYPERIOD_AM,
46         DAYPERIOD_PM
47     };
48 
49     static const DayPeriodRules *getInstance(const Locale &locale, UErrorCode &errorCode);
50 
hasMidnight()51     UBool hasMidnight() const { return fHasMidnight; }
hasNoon()52     UBool hasNoon() const { return fHasNoon; }
getDayPeriodForHour(int32_t hour)53     DayPeriod getDayPeriodForHour(int32_t hour) const { return fDayPeriodForHour[hour]; }
54 
55     // Returns the center of dayPeriod. Half hours are indicated with a .5 .
56     double getMidPointForDayPeriod(DayPeriod dayPeriod, UErrorCode &errorCode) const;
57 
58 private:
59     DayPeriodRules();
60 
61     // Translates "morning1" to DAYPERIOD_MORNING1, for example.
62     static DayPeriod getDayPeriodFromString(const char *type_str);
63 
64     static void U_CALLCONV load(UErrorCode &errorCode);
65 
66     // Sets period type for all hours in [startHour, limitHour).
67     void add(int32_t startHour, int32_t limitHour, DayPeriod period);
68 
69     // Returns true if for all i, DayPeriodForHour[i] has a type other than UNKNOWN.
70     // Values of HasNoon and HasMidnight do not affect the return value.
71     UBool allHoursAreSet();
72 
73     // Returns the hour that starts dayPeriod. Returns 0 for MIDNIGHT and 12 for NOON.
74     int32_t getStartHourForDayPeriod(DayPeriod dayPeriod, UErrorCode &errorCode) const;
75 
76     // Returns the hour that ends dayPeriod, i.e. that starts the next period.
77     // E.g. if fDayPeriodForHour[13] thru [16] are AFTERNOON1, then this function returns 17 if
78     // queried with AFTERNOON1.
79     // Returns 0 for MIDNIGHT and 12 for NOON.
80     int32_t getEndHourForDayPeriod(DayPeriod dayPeriod, UErrorCode &errorCode) const;
81 
82     UBool fHasMidnight;
83     UBool fHasNoon;
84     DayPeriod fDayPeriodForHour[24];
85 };
86 
87 U_NAMESPACE_END
88 
89 #endif /* DAYPERIODRULES_H */
90