1 /* 2 * Copyright 2007-2010 Haiku, Inc. All rights reserved. 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef _DATE_TIME_H 6 #define _DATE_TIME_H 7 8 9 #include <String.h> 10 11 12 class BMessage; 13 14 15 namespace BPrivate { 16 17 enum time_type { 18 B_GMT_TIME, 19 B_LOCAL_TIME 20 }; 21 22 23 enum diff_type { 24 B_HOURS_DIFF, 25 B_MINUTES_DIFF, 26 B_SECONDS_DIFF, 27 B_MILLISECONDS_DIFF, 28 B_MICROSECONDS_DIFF 29 }; 30 31 32 class BTime { 33 public: 34 BTime(); 35 BTime(const BTime& other); 36 BTime(int32 hour, int32 minute, int32 second, 37 int32 microsecond = 0); 38 BTime(const BMessage* archive); 39 ~BTime(); 40 41 status_t Archive(BMessage* into) const; 42 43 bool IsValid() const; 44 static bool IsValid(const BTime& time); 45 static bool IsValid(int32 hour, int32 minute, int32 second, 46 int32 microsecond = 0); 47 48 static BTime CurrentTime(time_type type); 49 50 BTime Time() const; 51 bool SetTime(const BTime& time); 52 bool SetTime(int32 hour, int32 minute, int32 second, 53 int32 microsecond = 0); 54 55 BTime& AddHours(int32 hours); 56 BTime& AddMinutes(int32 minutes); 57 BTime& AddSeconds(int32 seconds); 58 BTime& AddMilliseconds(int32 milliseconds); 59 BTime& AddMicroseconds(int32 microseconds); 60 61 int32 Hour() const; 62 int32 Minute() const; 63 int32 Second() const; 64 int32 Millisecond() const; 65 int32 Microsecond() const; 66 bigtime_t Difference(const BTime& time, 67 diff_type type) const; 68 69 bool operator!=(const BTime& time) const; 70 bool operator==(const BTime& time) const; 71 72 bool operator<(const BTime& time) const; 73 bool operator<=(const BTime& time) const; 74 75 bool operator>(const BTime& time) const; 76 bool operator>=(const BTime& time) const; 77 78 private: 79 bigtime_t _Microseconds() const; 80 BTime& _AddMicroseconds(bigtime_t microseconds); 81 bool _SetTime(bigtime_t hour, bigtime_t minute, 82 bigtime_t second, bigtime_t microsecond); 83 84 private: 85 bigtime_t fMicroseconds; 86 }; 87 88 89 class BDate { 90 public: 91 BDate(); 92 BDate(const BDate& other); 93 BDate(int32 year, int32 month, int32 day); 94 BDate(time_t time, 95 time_type type = B_LOCAL_TIME); 96 BDate(const BMessage* archive); 97 ~BDate(); 98 99 status_t Archive(BMessage* into) const; 100 101 bool IsValid() const; 102 static bool IsValid(const BDate& date); 103 static bool IsValid(int32 year, int32 month, 104 int32 day); 105 106 static BDate CurrentDate(time_type type); 107 108 BDate Date() const; 109 bool SetDate(const BDate& date); 110 111 bool SetDate(int32 year, int32 month, int32 day); 112 void GetDate(int32* year, int32* month, 113 int32* day) const; 114 115 void AddDays(int32 days); 116 void AddYears(int32 years); 117 void AddMonths(int32 months); 118 119 int32 Day() const; 120 int32 Year() const; 121 int32 Month() const; 122 int32 Difference(const BDate& date) const; 123 124 void SetDay(int32 day); 125 void SetMonth(int32 month); 126 void SetYear(int32 year); 127 128 int32 DayOfWeek() const; 129 int32 DayOfYear() const; 130 131 int32 WeekNumber() const; 132 bool IsLeapYear() const; 133 static bool IsLeapYear(int32 year); 134 135 int32 DaysInYear() const; 136 int32 DaysInMonth() const; 137 138 BString ShortDayName() const; 139 static BString ShortDayName(int32 day); 140 141 BString ShortMonthName() const; 142 static BString ShortMonthName(int32 month); 143 144 BString LongDayName() const; 145 static BString LongDayName(int32 day); 146 147 BString LongMonthName() const; 148 static BString LongMonthName(int32 month); 149 150 int32 DateToJulianDay() const; 151 static BDate JulianDayToDate(int32 julianDay); 152 153 bool operator!=(const BDate& date) const; 154 bool operator==(const BDate& date) const; 155 156 bool operator<(const BDate& date) const; 157 bool operator<=(const BDate& date) const; 158 159 bool operator>(const BDate& date) const; 160 bool operator>=(const BDate& date) const; 161 162 private: 163 static int32 _DaysInMonth(int32 year, int32 month); 164 bool _SetDate(int32 year, int32 month, int32 day); 165 static int32 _DateToJulianDay(int32 year, int32 month, 166 int32 day); 167 168 private: 169 int32 fDay; 170 int32 fYear; 171 int32 fMonth; 172 }; 173 174 175 class BDateTime { 176 public: 177 BDateTime(); 178 BDateTime(const BDate &date, const BTime &time); 179 BDateTime(const BMessage* archive); 180 ~BDateTime(); 181 182 status_t Archive(BMessage* into) const; 183 184 bool IsValid() const; 185 186 static BDateTime CurrentDateTime(time_type type); 187 void SetDateTime(const BDate &date, const BTime &time); 188 189 BDate& Date(); 190 const BDate& Date() const; 191 void SetDate(const BDate &date); 192 193 BTime& Time(); 194 const BTime& Time() const; 195 void SetTime(const BTime &time); 196 197 time_t Time_t() const; 198 void SetTime_t(time_t seconds); 199 200 bool operator!=(const BDateTime& dateTime) const; 201 bool operator==(const BDateTime& dateTime) const; 202 203 bool operator<(const BDateTime& dateTime) const; 204 bool operator<=(const BDateTime& dateTime) const; 205 206 bool operator>(const BDateTime& dateTime) const; 207 bool operator>=(const BDateTime& dateTime) const; 208 209 private: 210 BDate fDate; 211 BTime fTime; 212 }; 213 214 } // namespace BPrivate 215 216 using BPrivate::time_type; 217 using BPrivate::B_GMT_TIME; 218 using BPrivate::B_LOCAL_TIME; 219 using BPrivate::diff_type; 220 using BPrivate::B_HOURS_DIFF; 221 using BPrivate::B_MINUTES_DIFF; 222 using BPrivate::B_SECONDS_DIFF; 223 using BPrivate::B_MILLISECONDS_DIFF; 224 using BPrivate::B_MICROSECONDS_DIFF; 225 using BPrivate::BTime; 226 using BPrivate::BDate; 227 using BPrivate::BDateTime; 228 229 230 #endif // _DATE_TIME_H 231