blob: 4bfca762b8079b15ade638bb54dcf1230ec3abb2 [file] [log] [blame]
Fredrik Roubert0596fae2017-04-18 21:34:02 +02001// © 2016 and later: Unicode, Inc. and others.
Fredrik Roubert64339d32016-10-21 19:43:16 +02002// License & terms of use: http://www.unicode.org/copyright.html
Fredrik Roubert8de051c2016-03-10 13:13:27 +01003/*
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
26U_NAMESPACE_BEGIN
27
28struct DayPeriodRulesDataSink;
29
30class DayPeriodRules : public UMemory {
31 friend struct DayPeriodRulesDataSink;
32public:
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
51 UBool hasMidnight() const { return fHasMidnight; }
52 UBool hasNoon() const { return fHasNoon; }
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
58private:
59 DayPeriodRules();
60
61 // Translates "morning1" to DAYPERIOD_MORNING1, for example.
62 static DayPeriod getDayPeriodFromString(const char *type_str);
63
Fredrik Roubert64339d32016-10-21 19:43:16 +020064 static void U_CALLCONV load(UErrorCode &errorCode);
Fredrik Roubert8de051c2016-03-10 13:13:27 +010065
66 // Sets period type for all hours in [startHour, limitHour).
67 void add(int32_t startHour, int32_t limitHour, DayPeriod period);
68
Victor Chang978167a2021-01-18 17:56:33 +000069 // Returns true if for all i, DayPeriodForHour[i] has a type other than UNKNOWN.
Fredrik Roubert8de051c2016-03-10 13:13:27 +010070 // 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
87U_NAMESPACE_END
88
89#endif /* DAYPERIODRULES_H */