dayperiodrules.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. #ifndef DAYPERIODRULES_H
  14. #define DAYPERIODRULES_H
  15. #include "unicode/locid.h"
  16. #include "unicode/unistr.h"
  17. #include "unicode/uobject.h"
  18. #include "unicode/utypes.h"
  19. #include "resource.h"
  20. #include "uhash.h"
  21. U_NAMESPACE_BEGIN
  22. struct DayPeriodRulesDataSink;
  23. class DayPeriodRules : public UMemory {
  24. friend struct DayPeriodRulesDataSink;
  25. public:
  26. enum DayPeriod {
  27. DAYPERIOD_UNKNOWN = -1,
  28. DAYPERIOD_MIDNIGHT,
  29. DAYPERIOD_NOON,
  30. DAYPERIOD_MORNING1,
  31. DAYPERIOD_AFTERNOON1,
  32. DAYPERIOD_EVENING1,
  33. DAYPERIOD_NIGHT1,
  34. DAYPERIOD_MORNING2,
  35. DAYPERIOD_AFTERNOON2,
  36. DAYPERIOD_EVENING2,
  37. DAYPERIOD_NIGHT2,
  38. DAYPERIOD_AM,
  39. DAYPERIOD_PM
  40. };
  41. static const DayPeriodRules *getInstance(const Locale &locale, UErrorCode &errorCode);
  42. UBool hasMidnight() const { return fHasMidnight; }
  43. UBool hasNoon() const { return fHasNoon; }
  44. DayPeriod getDayPeriodForHour(int32_t hour) const { return fDayPeriodForHour[hour]; }
  45. // Returns the center of dayPeriod. Half hours are indicated with a .5 .
  46. double getMidPointForDayPeriod(DayPeriod dayPeriod, UErrorCode &errorCode) const;
  47. private:
  48. DayPeriodRules();
  49. // Translates "morning1" to DAYPERIOD_MORNING1, for example.
  50. static DayPeriod getDayPeriodFromString(const char *type_str);
  51. static void U_CALLCONV load(UErrorCode &errorCode);
  52. // Sets period type for all hours in [startHour, limitHour).
  53. void add(int32_t startHour, int32_t limitHour, DayPeriod period);
  54. // Returns TRUE if for all i, DayPeriodForHour[i] has a type other than UNKNOWN.
  55. // Values of HasNoon and HasMidnight do not affect the return value.
  56. UBool allHoursAreSet();
  57. // Returns the hour that starts dayPeriod. Returns 0 for MIDNIGHT and 12 for NOON.
  58. int32_t getStartHourForDayPeriod(DayPeriod dayPeriod, UErrorCode &errorCode) const;
  59. // Returns the hour that ends dayPeriod, i.e. that starts the next period.
  60. // E.g. if fDayPeriodForHour[13] thru [16] are AFTERNOON1, then this function returns 17 if
  61. // queried with AFTERNOON1.
  62. // Returns 0 for MIDNIGHT and 12 for NOON.
  63. int32_t getEndHourForDayPeriod(DayPeriod dayPeriod, UErrorCode &errorCode) const;
  64. UBool fHasMidnight;
  65. UBool fHasNoon;
  66. DayPeriod fDayPeriodForHour[24];
  67. };
  68. U_NAMESPACE_END
  69. #endif /* DAYPERIODRULES_H */