From c26731ec52d54eeeacd9df9498cc8867abd18193 Mon Sep 17 00:00:00 2001 From: Karsten Heimrich Date: Wed, 25 Mar 2009 22:31:11 +0000 Subject: [PATCH] * add default constructor * fix broken Time_t function * take the missing days into account when adding months git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@29713 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- headers/private/shared/DateTime.h | 3 ++- src/kits/shared/DateTime.cpp | 22 +++++++++++++++++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/headers/private/shared/DateTime.h b/headers/private/shared/DateTime.h index d5f4c75bed..98f83e45cb 100755 --- a/headers/private/shared/DateTime.h +++ b/headers/private/shared/DateTime.h @@ -1,5 +1,5 @@ /* - * Copyright 2007-2008, Haiku, Inc. All Rights Reserved. + * Copyright 2007-2009, Haiku, Inc. All Rights Reserved. * Distributed under the terms of the MIT License. */ #ifndef _DATE_TIME_H_ @@ -147,6 +147,7 @@ class BDate { class BDateTime { public: + BDateTime(); BDateTime(const BDate &date, const BTime &time); ~BDateTime(); diff --git a/src/kits/shared/DateTime.cpp b/src/kits/shared/DateTime.cpp index 2fdd0a5719..2f061ccfe8 100644 --- a/src/kits/shared/DateTime.cpp +++ b/src/kits/shared/DateTime.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2004-2008, Haiku, Inc. All Rights Reserved. + * Copyright 2007-2009, Haiku, Inc. All Rights Reserved. * Distributed under the terms of the MIT License. * * Authors: @@ -430,6 +430,11 @@ BDate::AddMonths(int32 months) fYear--; fMonth += 12; } + + // 'missing' days between switch julian - gregorian + if (fYear == 1582 && fMonth == 10 && fDay > 4 && fDay < 15) + fDay = (months > 0) ? 15 : 4; + fDay = min_c(fDay, DaysInMonth()); } @@ -536,10 +541,11 @@ bool BDate::IsLeapYear(int32 year) const { if (year < 1582) { - if (year < 0) year++; + if (year < 0) + year++; return (year % 4) == 0; } - return year % 400 == 0 || year % 4 == 0 && year % 100 != 0; + return (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0); } @@ -777,6 +783,12 @@ BDate::_DateToJulianDay(int32 _year, int32 month, int32 day) const // #pragma mark - BDateTime +BDateTime::BDateTime() + : fDate(BDate()), + fTime(BTime()) +{ +} + BDateTime::BDateTime(const BDate &date, const BTime &time) : fDate(date), fTime(time) @@ -845,6 +857,10 @@ BDateTime::SetTime(const BTime &time) uint32 BDateTime::Time_t() const { + BDate date(1970, 1, 1); + if (date.Difference(fDate) < 0) + return -1; + tm tm_struct; tm_struct.tm_hour = fTime.Hour();