* changed some methods to static as that's what they are in the end.

* added some methods to find out easter sunday, ascension and pentecost days.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@40116 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Jérôme Duval
2011-01-04 19:43:23 +00:00
parent 1fd632e67c
commit daf3c50529
2 changed files with 127 additions and 55 deletions
+17 -11
View File
@@ -42,9 +42,9 @@ public:
status_t Archive(BMessage* into) const; status_t Archive(BMessage* into) const;
bool IsValid() const; bool IsValid() const;
bool IsValid(const BTime& time) const; static bool IsValid(const BTime& time);
bool IsValid(int32 hour, int32 minute, int32 second, static bool IsValid(int32 hour, int32 minute, int32 second,
int32 microsecond = 0) const; int32 microsecond = 0);
static BTime CurrentTime(time_type type); static BTime CurrentTime(time_type type);
@@ -98,9 +98,9 @@ public:
status_t Archive(BMessage* into) const; status_t Archive(BMessage* into) const;
bool IsValid() const; bool IsValid() const;
bool IsValid(const BDate& date) const; static bool IsValid(const BDate& date);
bool IsValid(int32 year, int32 month, static bool IsValid(int32 year, int32 month,
int32 day) const; int32 day);
static BDate CurrentDate(time_type type); static BDate CurrentDate(time_type type);
@@ -108,7 +108,8 @@ public:
bool SetDate(const BDate& date); bool SetDate(const BDate& date);
bool SetDate(int32 year, int32 month, int32 day); bool SetDate(int32 year, int32 month, int32 day);
void GetDate(int32* year, int32* month, int32* day); void GetDate(int32* year, int32* month,
int32* day) const;
void AddDays(int32 days); void AddDays(int32 days);
void AddYears(int32 years); void AddYears(int32 years);
@@ -123,7 +124,8 @@ public:
int32 DayOfYear() const; int32 DayOfYear() const;
int32 WeekNumber() const; int32 WeekNumber() const;
bool IsLeapYear(int32 year) const; bool IsLeapYear() const;
static bool IsLeapYear(int32 year);
int32 DaysInYear() const; int32 DaysInYear() const;
int32 DaysInMonth() const; int32 DaysInMonth() const;
@@ -143,6 +145,10 @@ public:
int32 DateToJulianDay() const; int32 DateToJulianDay() const;
static BDate JulianDayToDate(int32 julianDay); static BDate JulianDayToDate(int32 julianDay);
static BDate EasterSunday(int32 year);
static BDate AscensionDay(int32 year);
static BDate PentecostDay(int32 year);
bool operator!=(const BDate& date) const; bool operator!=(const BDate& date) const;
bool operator==(const BDate& date) const; bool operator==(const BDate& date) const;
@@ -153,10 +159,10 @@ public:
bool operator>=(const BDate& date) const; bool operator>=(const BDate& date) const;
private: private:
int32 _DaysInMonth(int32 year, int32 month) const; static int32 _DaysInMonth(int32 year, int32 month);
bool _SetDate(int32 year, int32 month, int32 day); bool _SetDate(int32 year, int32 month, int32 day);
int32 _DateToJulianDay(int32 year, int32 month, static int32 _DateToJulianDay(int32 year, int32 month,
int32 day) const; int32 day);
private: private:
int32 fDay; int32 fDay;
+110 -44
View File
@@ -78,7 +78,7 @@ BTime::BTime(const BMessage* archive)
{ {
if (archive == NULL) if (archive == NULL)
return; return;
archive->FindInt64("mircoseconds", &fMicroseconds); archive->FindInt64("microseconds", &fMicroseconds);
} }
@@ -102,7 +102,7 @@ BTime::Archive(BMessage* into) const
{ {
if (into == NULL) if (into == NULL)
return B_BAD_VALUE; return B_BAD_VALUE;
return into->AddInt64("mircoseconds", fMicroseconds); return into->AddInt64("microseconds", fMicroseconds);
} }
@@ -120,18 +120,18 @@ BTime::IsValid() const
/*! /*!
This is an overloaded member function, provided for convenience. This is an overloaded member function, provided for convenience.
*/ */
bool /*static*/ bool
BTime::IsValid(const BTime& time) const BTime::IsValid(const BTime& time)
{ {
return time.fMicroseconds > -1 && time.fMicroseconds < kMicrosecondsPerDay; return time.IsValid();
} }
/*! /*!
This is an overloaded member function, provided for convenience. This is an overloaded member function, provided for convenience.
*/ */
bool /*static*/ bool
BTime::IsValid(int32 hour, int32 minute, int32 second, int32 microsecond) const BTime::IsValid(int32 hour, int32 minute, int32 second, int32 microsecond)
{ {
return BTime(hour, minute, second, microsecond).IsValid(); return BTime(hour, minute, second, microsecond).IsValid();
} }
@@ -205,8 +205,8 @@ BTime::SetTime(int32 hour, int32 minute, int32 second, int32 microsecond)
/*! /*!
Adds \c hours to the current time. If the passed value is negativ it will Adds \c hours to the current time. If the passed value is negative it
become earlier. Note: The time will wrap if it passes midnight. will become earlier. Note: The time will wrap if it passes midnight.
*/ */
BTime& BTime&
BTime::AddHours(int32 hours) BTime::AddHours(int32 hours)
@@ -217,8 +217,8 @@ BTime::AddHours(int32 hours)
/*! /*!
Adds \c minutes to the current time. If the passed value is negativ it will Adds \c minutes to the current time. If the passed value is negative it
become earlier. Note: The time will wrap if it passes midnight. will become earlier. Note: The time will wrap if it passes midnight.
*/ */
BTime& BTime&
BTime::AddMinutes(int32 minutes) BTime::AddMinutes(int32 minutes)
@@ -229,8 +229,8 @@ BTime::AddMinutes(int32 minutes)
/*! /*!
Adds \c seconds to the current time. If the passed value is negativ it will Adds \c seconds to the current time. If the passed value is negative
become earlier. Note: The time will wrap if it passes midnight. it will become earlier. Note: The time will wrap if it passes midnight.
*/ */
BTime& BTime&
BTime::AddSeconds(int32 seconds) BTime::AddSeconds(int32 seconds)
@@ -241,8 +241,8 @@ BTime::AddSeconds(int32 seconds)
/*! /*!
Adds \c milliseconds to the current time. If the passed value is negativ it Adds \c milliseconds to the current time. If the passed value is negative
will become earlier. Note: The time will wrap if it passes midnight. it will become earlier. Note: The time will wrap if it passes midnight.
*/ */
BTime& BTime&
BTime::AddMilliseconds(int32 milliseconds) BTime::AddMilliseconds(int32 milliseconds)
@@ -253,8 +253,8 @@ BTime::AddMilliseconds(int32 milliseconds)
/*! /*!
Adds \c microseconds to the current time. If the passed value is negativ it Adds \c microseconds to the current time. If the passed value is negative
will become earlier. Note: The time will wrap if it passes midnight. it will become earlier. Note: The time will wrap if it passes midnight.
*/ */
BTime& BTime&
BTime::AddMicroseconds(int32 microseconds) BTime::AddMicroseconds(int32 microseconds)
@@ -279,7 +279,8 @@ BTime::Hour() const
int32 int32
BTime::Minute() const BTime::Minute() const
{ {
return int32(((_Microseconds() % kMicrosecondsPerHour)) / kMicrosecondsPerMinute); return int32(((_Microseconds() % kMicrosecondsPerHour))
/ kMicrosecondsPerMinute);
} }
@@ -310,7 +311,7 @@ BTime::Millisecond() const
int32 int32
BTime::Microsecond() const BTime::Microsecond() const
{ {
return int32(_Microseconds() % 1000000); return int32(_Microseconds() % kMicrosecondsPerSecond);
} }
@@ -335,20 +336,21 @@ BTime::Difference(const BTime& time, diff_type type) const
{ {
bigtime_t diff = time._Microseconds() - _Microseconds(); bigtime_t diff = time._Microseconds() - _Microseconds();
switch (type) { switch (type) {
case B_HOURS_DIFF: { case B_HOURS_DIFF:
diff /= kMicrosecondsPerHour; diff /= kMicrosecondsPerHour;
} break; break;
case B_MINUTES_DIFF: { case B_MINUTES_DIFF:
diff /= kMicrosecondsPerMinute; diff /= kMicrosecondsPerMinute;
} break; break;
case B_SECONDS_DIFF: { case B_SECONDS_DIFF:
diff /= kMicrosecondsPerSecond; diff /= kMicrosecondsPerSecond;
} break; break;
case B_MILLISECONDS_DIFF: { case B_MILLISECONDS_DIFF:
diff /= 1000; diff /= 1000;
} break; break;
case B_MICROSECONDS_DIFF: case B_MICROSECONDS_DIFF:
default: break; default:
break;
} }
return diff; return diff;
} }
@@ -548,8 +550,8 @@ BDate::IsValid() const
/*! /*!
This is an overloaded member function, provided for convenience. This is an overloaded member function, provided for convenience.
*/ */
bool /*static*/ bool
BDate::IsValid(const BDate& date) const BDate::IsValid(const BDate& date)
{ {
return IsValid(date.fYear, date.fMonth, date.fDay); return IsValid(date.fYear, date.fMonth, date.fDay);
} }
@@ -558,8 +560,8 @@ BDate::IsValid(const BDate& date) const
/*! /*!
This is an overloaded member function, provided for convenience. This is an overloaded member function, provided for convenience.
*/ */
bool /*static*/ bool
BDate::IsValid(int32 year, int32 month, int32 day) const BDate::IsValid(int32 year, int32 month, int32 day)
{ {
// no year 0 in Julian and nothing before 1.1.4713 BC // no year 0 in Julian and nothing before 1.1.4713 BC
if (year == 0 || year < -4713) if (year == 0 || year < -4713)
@@ -644,7 +646,7 @@ BDate::SetDate(int32 year, int32 month, int32 day)
will be set to 0. will be set to 0.
*/ */
void void
BDate::GetDate(int32* year, int32* month, int32* day) BDate::GetDate(int32* year, int32* month, int32* day) const
{ {
if (year) if (year)
*year = fYear; *year = fYear;
@@ -855,12 +857,23 @@ BDate::DayOfYear() const
} }
/*!
Returns true if the year of this object is a leap year, otherwise false. If
the \c year passed is before 4713 BC, the result is undefined.
*/
bool
BDate::IsLeapYear() const
{
return IsLeapYear(fYear);
}
/*! /*!
Returns true if the passed \c year is a leap year, otherwise false. If the Returns true if the passed \c year is a leap year, otherwise false. If the
\c year passed is before 4713 BC, the result is undefined. \c year passed is before 4713 BC, the result is undefined.
*/ */
bool /*static*/ bool
BDate::IsLeapYear(int32 year) const BDate::IsLeapYear(int32 year)
{ {
if (year < 1582) { if (year < 1582) {
if (year < 0) if (year < 0)
@@ -1036,12 +1049,12 @@ BDate::DateToJulianDay() const
} }
/* /*!
Converts the passed \c julianDay to an BDate. If the \c julianDay is negativ, Converts the passed \c julianDay to an BDate. If the \c julianDay is negativ,
the function will return an invalid date. Because of the switch from Julian the function will return an invalid date. Because of the switch from Julian
calendar to Gregorian calendar the 4.10.1582 is followed by the 15.10.1582. calendar to Gregorian calendar the 4.10.1582 is followed by the 15.10.1582.
*/ */
BDate /*static*/ BDate
BDate::JulianDayToDate(int32 julianDay) BDate::JulianDayToDate(int32 julianDay)
{ {
BDate date; BDate date;
@@ -1056,8 +1069,8 @@ BDate::JulianDayToDate(int32 julianDay)
int32 a = (db / 365 + 1) * 3 / 4; int32 a = (db / 365 + 1) * 3 / 4;
int32 da = db - a * 365; int32 da = db - a * 365;
int32 m = (da * 5 + 308) / 153 - 2; int32 m = (da * 5 + 308) / 153 - 2;
date.fYear = ((j / 146097) * 400 + c * 100 + (dc / 1461) * 4 + a) - 4800 + date.fYear = ((j / 146097) * 400 + c * 100 + (dc / 1461) * 4 + a)
(m + 2) / 12; - 4800 + (m + 2) / 12;
date.fMonth = (m + 2) % 12 + 1; date.fMonth = (m + 2) % 12 + 1;
date.fDay = int32((da - (m + 4) * 153 / 5 + 122) + 1.5); date.fDay = int32((da - (m + 4) * 153 / 5 + 122) + 1.5);
} else if (julianDay >= 0) { } else if (julianDay >= 0) {
@@ -1077,6 +1090,57 @@ BDate::JulianDayToDate(int32 julianDay)
} }
/*!
Returns the date for easter sunday for the \c year.
*/
/*static*/ BDate
BDate::EasterSunday(int32 year)
{
// http://bloggingabout.net/blogs/jschreuder/archive/2005/06/24/7019.aspx
int32 gold = year % 19;
int32 century = year / 100;
int32 h = (century - (int32)(century / 4)
- (int32)((century * 8 + 13) / 25) + 19 * gold + 15) % 30;
int32 i = h - (int32)(h / 28) * (1 - (int32)(h / 28)
* (int32)(29 / (h + 1)) * (int32)((21 - gold) / 11));
BDate date;
date.fDay = i - ((year + (int32)(year / 4) + i + 2 - century
+ (int32)(century / 4)) % 7) + 28;
date.fMonth = 3;
date.fYear = year;
if (date.fDay > 31) {
date.fMonth++;
date.fDay -= 31;
}
return date;
}
/*!
Returns the date for ascension day for the \c year.
*/
/*static*/ BDate
BDate::AscensionDay(int32 year)
{
BDate date = AscensionDay(year);
date.AddDays(39);
return date;
}
/*!
Returns the date for pentecost day for the \c year.
*/
/*static*/ BDate
BDate::PentecostDay(int32 year)
{
BDate date = AscensionDay(year);
date.AddDays(49);
return date;
}
/*! /*!
Returns true if this date is different from \c date, otherwise false. Returns true if this date is different from \c date, otherwise false.
*/ */
@@ -1108,7 +1172,8 @@ BDate::operator<(const BDate& date) const
/*! /*!
Returns true if this date is earlier than or equal to \c date, otherwise false. Returns true if this date is earlier than or equal to \c date, otherwise
false.
*/ */
bool bool
BDate::operator<=(const BDate& date) const BDate::operator<=(const BDate& date) const
@@ -1128,7 +1193,8 @@ BDate::operator>(const BDate& date) const
/*! /*!
Returns true if this date is later than or equal to \c date, otherwise false. Returns true if this date is later than or equal to \c date, otherwise
false.
*/ */
bool bool
BDate::operator>=(const BDate& date) const BDate::operator>=(const BDate& date) const
@@ -1156,7 +1222,7 @@ BDate::_SetDate(int32 year, int32 month, int32 day)
int32 int32
BDate::_DaysInMonth(int32 year, int32 month) const BDate::_DaysInMonth(int32 year, int32 month)
{ {
if (month == 2 && IsLeapYear(year)) if (month == 2 && IsLeapYear(year))
return 29; return 29;
@@ -1169,7 +1235,7 @@ BDate::_DaysInMonth(int32 year, int32 month) const
int32 int32
BDate::_DateToJulianDay(int32 _year, int32 month, int32 day) const BDate::_DateToJulianDay(int32 _year, int32 month, int32 day)
{ {
if (IsValid(_year, month, day)) { if (IsValid(_year, month, day)) {
int32 year = _year; int32 year = _year;
@@ -1373,7 +1439,7 @@ BDateTime::Time_t() const
tm_struct.tm_mon = fDate.Month() - 1; tm_struct.tm_mon = fDate.Month() - 1;
tm_struct.tm_mday = fDate.Day(); tm_struct.tm_mday = fDate.Day();
// set less 0 as we wan't use it // set less 0 as we won't use it
tm_struct.tm_isdst = -1; tm_struct.tm_isdst = -1;
// return secs_since_jan1_1970 or -1 on error // return secs_since_jan1_1970 or -1 on error