From a070cf5306662fec199ccf79c0e9872fbcd0fc75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20A=C3=9Fmus?= Date: Sun, 7 Mar 2010 12:08:47 +0000 Subject: [PATCH] * Indentation update in DateTime.h * Extended BTime, BDate and BDateTime with archiving functionality. * Adjusted code which uses these classes, since including DateTime.h already imports the classes from the BPrivate namespace. * Moved DateTime.h into Support Kit. It is still in the BPrivate namespace, as I am uncertain what to do with time_type and diff_type. I'd favor moving the constants into the classes itself. Possibly removing the B_ prefix from them. Feedback welcome. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@35772 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- headers/os/support/DateTime.h | 215 ++++++++++++++++++++ headers/private/shared/DateTime.h | 186 ----------------- src/add-ons/screen_savers/flurry/Flurry.cpp | 4 +- src/add-ons/screen_savers/flurry/Flurry.h | 4 +- src/add-ons/screen_savers/flurry/Jamfile | 2 - src/apps/deskbar/CalendarMenuWindow.cpp | 1 - src/apps/deskbar/CalendarMenuWindow.h | 1 - src/kits/shared/Jamfile | 1 - src/kits/{shared => support}/DateTime.cpp | 102 +++++++++- src/kits/support/Jamfile | 1 + src/preferences/time/BaseView.cpp | 6 - src/preferences/time/DateTimeEdit.h | 4 - 12 files changed, 318 insertions(+), 209 deletions(-) create mode 100644 headers/os/support/DateTime.h delete mode 100644 headers/private/shared/DateTime.h rename src/kits/{shared => support}/DateTime.cpp (92%) diff --git a/headers/os/support/DateTime.h b/headers/os/support/DateTime.h new file mode 100644 index 0000000000..fe92d86745 --- /dev/null +++ b/headers/os/support/DateTime.h @@ -0,0 +1,215 @@ +/* + * Copyright 2007-2010, Haiku, Inc. All Rights Reserved. + * Distributed under the terms of the MIT License. + */ +#ifndef _DATE_TIME_H_ +#define _DATE_TIME_H_ + + +#include + + +class BMessage; + + +namespace BPrivate { + + +enum time_type { + B_GMT_TIME, + B_LOCAL_TIME +}; + + +enum diff_type { + B_HOURS_DIFF, + B_MINUTES_DIFF, + B_SECONDS_DIFF, + B_MILLISECONDS_DIFF, + B_MICROSECONDS_DIFF +}; + + +class BTime { +public: + BTime(); + BTime(int32 hour, int32 minute, int32 second, + int32 microsecond = 0); + BTime(const BMessage* archive); + ~BTime(); + + status_t Archive(BMessage* into) const; + + bool IsValid() const; + bool IsValid(const BTime& time) const; + bool IsValid(int32 hour, int32 minute, int32 second, + int32 microsecond = 0) const; + + static BTime CurrentTime(time_type type); + + BTime Time() const; + bool SetTime(const BTime& time); + bool SetTime(int32 hour, int32 minute, int32 second, + int32 microsecond = 0); + + void AddHours(int32 hours); + void AddMinutes(int32 minutes); + void AddSeconds(int32 seconds); + void AddMilliseconds(int32 milliseconds); + void AddMicroseconds(int32 microseconds); + + int32 Hour() const; + int32 Minute() const; + int32 Second() const; + int32 Millisecond() const; + int32 Microsecond() const; + bigtime_t Difference(const BTime& time, + diff_type type) const; + + bool operator!=(const BTime& time) const; + bool operator==(const BTime& time) const; + + bool operator<(const BTime& time) const; + bool operator<=(const BTime& time) const; + + bool operator>(const BTime& time) const; + bool operator>=(const BTime& time) const; + +private: + bigtime_t _Microseconds() const; + void _AddMicroseconds(bigtime_t microseconds); + bool _SetTime(bigtime_t hour, bigtime_t minute, + bigtime_t second, bigtime_t microsecond); + +private: + bigtime_t fMicroseconds; +}; + + +class BDate { +public: + BDate(); + BDate(int32 year, int32 month, int32 day); + BDate(const BMessage* archive); + ~BDate(); + + status_t Archive(BMessage* into) const; + + bool IsValid() const; + bool IsValid(const BDate& date) const; + bool IsValid(int32 year, int32 month, + int32 day) const; + + static BDate CurrentDate(time_type type); + + BDate Date() const; + bool SetDate(const BDate& date); + + bool SetDate(int32 year, int32 month, int32 day); + void GetDate(int32* year, int32* month, int32* day); + + void AddDays(int32 days); + void AddYears(int32 years); + void AddMonths(int32 months); + + int32 Day() const; + int32 Year() const; + int32 Month() const; + int32 Difference(const BDate& date) const; + + int32 DayOfWeek() const; + int32 DayOfYear() const; + + int32 WeekNumber() const; + bool IsLeapYear(int32 year) const; + + int32 DaysInYear() const; + int32 DaysInMonth() const; + + BString ShortDayName(int32 day) const; + BString ShortMonthName(int32 month) const; + + BString LongDayName(int32 day) const; + BString LongMonthName(int32 month) const; + + int32 DateToJulianDay() const; + static BDate JulianDayToDate(int32 julianDay); + + bool operator!=(const BDate& date) const; + bool operator==(const BDate& date) const; + + bool operator<(const BDate& date) const; + bool operator<=(const BDate& date) const; + + bool operator>(const BDate& date) const; + bool operator>=(const BDate& date) const; + +private: + int32 _DaysInMonth(int32 year, int32 month) const; + bool _SetDate(int32 year, int32 month, int32 day); + int32 _DateToJulianDay(int32 year, int32 month, + int32 day) const; + +private: + int32 fDay; + int32 fYear; + int32 fMonth; +}; + + +class BDateTime { +public: + BDateTime(); + BDateTime(const BDate &date, const BTime &time); + BDateTime(const BMessage* archive); + ~BDateTime(); + + status_t Archive(BMessage* into) const; + + bool IsValid() const; + + static BDateTime CurrentDateTime(time_type type); + void SetDateTime(const BDate &date, const BTime &time); + + BDate Date() const; + void SetDate(const BDate &date); + + BTime Time() const; + void SetTime(const BTime &time); + + int32 Time_t() const; + void SetTime_t(uint32 seconds); + + bool operator!=(const BDateTime& dateTime) const; + bool operator==(const BDateTime& dateTime) const; + + bool operator<(const BDateTime& dateTime) const; + bool operator<=(const BDateTime& dateTime) const; + + bool operator>(const BDateTime& dateTime) const; + bool operator>=(const BDateTime& dateTime) const; + +private: + BDate fDate; + BTime fTime; +}; + + +} // namespace BPrivate + + +using BPrivate::time_type; +using BPrivate::B_GMT_TIME; +using BPrivate::B_LOCAL_TIME; +using BPrivate::diff_type; +using BPrivate::B_HOURS_DIFF; +using BPrivate::B_MINUTES_DIFF; +using BPrivate::B_SECONDS_DIFF; +using BPrivate::B_MILLISECONDS_DIFF; +using BPrivate::B_MICROSECONDS_DIFF; +using BPrivate::BTime; +using BPrivate::BDate; +using BPrivate::BDateTime; + + +#endif // _DATE_TIME_H_ diff --git a/headers/private/shared/DateTime.h b/headers/private/shared/DateTime.h deleted file mode 100644 index 77c26754d0..0000000000 --- a/headers/private/shared/DateTime.h +++ /dev/null @@ -1,186 +0,0 @@ -/* - * Copyright 2007-2009, Haiku, Inc. All Rights Reserved. - * Distributed under the terms of the MIT License. - */ -#ifndef _DATE_TIME_H_ -#define _DATE_TIME_H_ - - -#include - - -namespace BPrivate { - - -enum time_type { - B_GMT_TIME, - B_LOCAL_TIME -}; - - -enum diff_type { - B_HOURS_DIFF, - B_MINUTES_DIFF, - B_SECONDS_DIFF, - B_MILLISECONDS_DIFF, - B_MICROSECONDS_DIFF -}; - - -class BTime { - public: - BTime(); - BTime(int32 hour, int32 minute, int32 second, - int32 microsecond = 0); - ~BTime(); - - bool IsValid() const; - bool IsValid(const BTime& time) const; - bool IsValid(int32 hour, int32 minute, int32 second, - int32 microsecond = 0) const; - - static BTime CurrentTime(time_type type); - - BTime Time() const; - bool SetTime(const BTime& time); - bool SetTime(int32 hour, int32 minute, int32 second, - int32 microsecond = 0); - - void AddHours(int32 hours); - void AddMinutes(int32 minutes); - void AddSeconds(int32 seconds); - void AddMilliseconds(int32 milliseconds); - void AddMicroseconds(int32 microseconds); - - int32 Hour() const; - int32 Minute() const; - int32 Second() const; - int32 Millisecond() const; - int32 Microsecond() const; - bigtime_t Difference(const BTime& time, diff_type type) const; - - bool operator!=(const BTime& time) const; - bool operator==(const BTime& time) const; - - bool operator<(const BTime& time) const; - bool operator<=(const BTime& time) const; - - bool operator>(const BTime& time) const; - bool operator>=(const BTime& time) const; - - private: - bigtime_t _Microseconds() const; - void _AddMicroseconds(bigtime_t microseconds); - bool _SetTime(bigtime_t hour, bigtime_t minute, bigtime_t second, - bigtime_t microsecond); - - private: - bigtime_t fMicroseconds; -}; - - -class BDate { - public: - BDate(); - BDate(int32 year, int32 month, int32 day); - ~BDate(); - - bool IsValid() const; - bool IsValid(const BDate& date) const; - bool IsValid(int32 year, int32 month, int32 day) const; - - static BDate CurrentDate(time_type type); - - BDate Date() const; - bool SetDate(const BDate& date); - - bool SetDate(int32 year, int32 month, int32 day); - void GetDate(int32* year, int32* month, int32* day); - - void AddDays(int32 days); - void AddYears(int32 years); - void AddMonths(int32 months); - - int32 Day() const; - int32 Year() const; - int32 Month() const; - int32 Difference(const BDate& date) const; - - int32 DayOfWeek() const; - int32 DayOfYear() const; - - int32 WeekNumber() const; - bool IsLeapYear(int32 year) const; - - int32 DaysInYear() const; - int32 DaysInMonth() const; - - BString ShortDayName(int32 day) const; - BString ShortMonthName(int32 month) const; - - BString LongDayName(int32 day) const; - BString LongMonthName(int32 month) const; - - int32 DateToJulianDay() const; - static BDate JulianDayToDate(int32 julianDay); - - bool operator!=(const BDate& date) const; - bool operator==(const BDate& date) const; - - bool operator<(const BDate& date) const; - bool operator<=(const BDate& date) const; - - bool operator>(const BDate& date) const; - bool operator>=(const BDate& date) const; - - private: - int32 _DaysInMonth(int32 year, int32 month) const; - bool _SetDate(int32 year, int32 month, int32 day); - int32 _DateToJulianDay(int32 year, int32 month, int32 day) const; - - private: - int32 fDay; - int32 fYear; - int32 fMonth; -}; - - -class BDateTime { - public: - BDateTime(); - BDateTime(const BDate &date, const BTime &time); - ~BDateTime(); - - bool IsValid() const; - - static BDateTime CurrentDateTime(time_type type); - void SetDateTime(const BDate &date, const BTime &time); - - BDate Date() const; - void SetDate(const BDate &date); - - BTime Time() const; - void SetTime(const BTime &time); - - int32 Time_t() const; - void SetTime_t(uint32 seconds); - - bool operator!=(const BDateTime& dateTime) const; - bool operator==(const BDateTime& dateTime) const; - - bool operator<(const BDateTime& dateTime) const; - bool operator<=(const BDateTime& dateTime) const; - - bool operator>(const BDateTime& dateTime) const; - bool operator>=(const BDateTime& dateTime) const; - - private: - BDate fDate; - BTime fTime; -}; - - -} // namespace BPrivate - - -#endif // _DATE_TIME_H_ diff --git a/src/add-ons/screen_savers/flurry/Flurry.cpp b/src/add-ons/screen_savers/flurry/Flurry.cpp index 73315c9b9d..c385c3074a 100644 --- a/src/add-ons/screen_savers/flurry/Flurry.cpp +++ b/src/add-ons/screen_savers/flurry/Flurry.cpp @@ -274,8 +274,8 @@ FlurryView::_SetupFlurryBaseInfo() double FlurryView::_CurrentTime() const { - return double(fDateTime.CurrentDateTime(B_LOCAL_TIME).Time_t() + - double(fTime.CurrentTime(B_LOCAL_TIME).Millisecond() / 1000.0)); + return double(BDateTime::CurrentDateTime(B_LOCAL_TIME).Time_t() + + double(BTime::CurrentTime(B_LOCAL_TIME).Millisecond() / 1000.0)); } diff --git a/src/add-ons/screen_savers/flurry/Flurry.h b/src/add-ons/screen_savers/flurry/Flurry.h index 5e678cc28b..0cd0b60f74 100644 --- a/src/add-ons/screen_savers/flurry/Flurry.h +++ b/src/add-ons/screen_savers/flurry/Flurry.h @@ -39,8 +39,8 @@ private: double fOldFrameTime; flurry_info_t* fFlurryInfo_t; - BPrivate::BTime fTime; - BPrivate::BDateTime fDateTime; + BTime fTime; + BDateTime fDateTime; }; diff --git a/src/add-ons/screen_savers/flurry/Jamfile b/src/add-ons/screen_savers/flurry/Jamfile index 59afa77271..927212cc50 100644 --- a/src/add-ons/screen_savers/flurry/Jamfile +++ b/src/add-ons/screen_savers/flurry/Jamfile @@ -1,7 +1,5 @@ SubDir HAIKU_TOP src add-ons screen_savers flurry ; -UsePrivateHeaders shared ; - ScreenSaver Flurry : Flurry.cpp Smoke.cpp diff --git a/src/apps/deskbar/CalendarMenuWindow.cpp b/src/apps/deskbar/CalendarMenuWindow.cpp index a1f56366ea..a54ac9b714 100644 --- a/src/apps/deskbar/CalendarMenuWindow.cpp +++ b/src/apps/deskbar/CalendarMenuWindow.cpp @@ -19,7 +19,6 @@ using BPrivate::BCalendarView; -using BPrivate::B_LOCAL_TIME; using BPrivate::B_WEEK_START_SUNDAY; using BPrivate::B_WEEK_START_MONDAY; diff --git a/src/apps/deskbar/CalendarMenuWindow.h b/src/apps/deskbar/CalendarMenuWindow.h index ddd67f80b6..9b27be4030 100644 --- a/src/apps/deskbar/CalendarMenuWindow.h +++ b/src/apps/deskbar/CalendarMenuWindow.h @@ -18,7 +18,6 @@ namespace BPrivate { class BCalendarView; } using BPrivate::BCalendarView; -using BPrivate::BDate; class CalendarMenuWindow : public BWindow { diff --git a/src/kits/shared/Jamfile b/src/kits/shared/Jamfile index 486861bfa9..950414f800 100644 --- a/src/kits/shared/Jamfile +++ b/src/kits/shared/Jamfile @@ -16,7 +16,6 @@ StaticLibrary libshared.a : CalendarView.cpp ColorQuantizer.cpp CommandPipe.cpp - DateTime.cpp DragTrackingFilter.cpp HashString.cpp RWLockManager.cpp diff --git a/src/kits/shared/DateTime.cpp b/src/kits/support/DateTime.cpp similarity index 92% rename from src/kits/shared/DateTime.cpp rename to src/kits/support/DateTime.cpp index 2c7cb26bed..7818dc36c2 100644 --- a/src/kits/shared/DateTime.cpp +++ b/src/kits/support/DateTime.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2007-2009, Haiku, Inc. All Rights Reserved. + * Copyright 2007-2010, Haiku, Inc. All Rights Reserved. * Distributed under the terms of the MIT License. * * Authors: @@ -7,12 +7,14 @@ * Stephan Aßmus */ -#include +#include "DateTime.h" #include #include +#include + namespace BPrivate { @@ -55,6 +57,18 @@ BTime::BTime(int32 hour, int32 minute, int32 second, int32 microsecond) } +/*! + Constructs a new BTime object from the provided BMessage archive. +*/ +BTime::BTime(const BMessage* archive) + : fMicroseconds(-1) +{ + if (archive == NULL) + return; + archive->FindInt64("mircoseconds", &fMicroseconds); +} + + /*! Empty destructor. */ @@ -63,6 +77,22 @@ BTime::~BTime() } +/*! + Archives the BTime object into the provided BMessage object. + @returns \c B_OK if all went well. + \c B_BAD_VALUE, if the message is \c NULL. + \c other error codes, depending on failure to append + fields to the message. +*/ +status_t +BTime::Archive(BMessage* into) const +{ + if (into == NULL) + return B_BAD_VALUE; + return into->AddInt64("mircoseconds", fMicroseconds); +} + + /*! Returns true if the time is valid, otherwise false. A valid time can be BTime(23, 59, 59, 999999) while BTime(24, 00, 01) would be invalid. @@ -424,6 +454,22 @@ BDate::BDate(int32 year, int32 month, int32 day) } +/*! + Constructs a new BDate object from the provided archive. +*/ +BDate::BDate(const BMessage* archive) + : fDay(-1), + fYear(0), + fMonth(-1) +{ + if (archive == NULL) + return; + archive->FindInt32("day", &fDay); + archive->FindInt32("year", &fYear); + archive->FindInt32("month", &fMonth); +} + + /*! Empty destructor. */ @@ -432,6 +478,27 @@ BDate::~BDate() } +/*! + Archives the BDate object into the provided BMessage object. + @returns \c B_OK if all went well. + \c B_BAD_VALUE, if the message is \c NULL. + \c other error codes, depending on failure to append + fields to the message. +*/ +status_t +BDate::Archive(BMessage* into) const +{ + if (into == NULL) + return B_BAD_VALUE; + status_t ret = into->AddInt32("day", fDay); + if (ret == B_OK) + ret = into->AddInt32("year", fYear); + if (ret == B_OK) + ret = into->AddInt32("month", fMonth); + return ret; +} + + /*! Returns true if the date is valid, otherwise false. @@ -1064,8 +1131,8 @@ BDate::_DateToJulianDay(int32 _year, int32 month, int32 day) const Constructs a new BDateTime object. IsValid() will return false. */ BDateTime::BDateTime() - : fDate(BDate()), - fTime(BTime()) + : fDate(), + fTime() { } @@ -1081,6 +1148,16 @@ BDateTime::BDateTime(const BDate& date, const BTime& time) } +/*! + Constructs a new BDateTime object. IsValid() will return false. +*/ +BDateTime::BDateTime(const BMessage* archive) + : fDate(archive), + fTime(archive) +{ +} + + /*! Empty destructor. */ @@ -1089,6 +1166,23 @@ BDateTime::~BDateTime() } +/*! + Archives the BDateTime object into the provided BMessage object. + @returns \c B_OK if all went well. + \c B_BAD_VALUE, if the message is \c NULL. + \c other error codes, depending on failure to append + fields to the message. +*/ +status_t +BDateTime::Archive(BMessage* into) const +{ + status_t ret = fDate.Archive(into); + if (ret == B_OK) + ret = fTime.Archive(into); + return ret; +} + + /*! Returns true if the date time is valid, otherwise false. */ diff --git a/src/kits/support/Jamfile b/src/kits/support/Jamfile index 4d6ead789e..42bd958e41 100644 --- a/src/kits/support/Jamfile +++ b/src/kits/support/Jamfile @@ -10,6 +10,7 @@ MergeObject support_kit.o : BlockCache.cpp ByteOrder.cpp DataIO.cpp + DateTime.cpp BufferIO.cpp Flattenable.cpp List.cpp diff --git a/src/preferences/time/BaseView.cpp b/src/preferences/time/BaseView.cpp index 6a47bec9e1..efe2752226 100644 --- a/src/preferences/time/BaseView.cpp +++ b/src/preferences/time/BaseView.cpp @@ -15,12 +15,6 @@ #include -using BPrivate::BDate; -using BPrivate::BTime; -using BPrivate::BDateTime; -using BPrivate::B_LOCAL_TIME; - - TTimeBaseView::TTimeBaseView(BRect frame, const char *name) : BView(frame, name, B_FOLLOW_NONE, B_PULSE_NEEDED), fMessage(H_TIME_UPDATE) diff --git a/src/preferences/time/DateTimeEdit.h b/src/preferences/time/DateTimeEdit.h index 5889fd04b4..92de438631 100644 --- a/src/preferences/time/DateTimeEdit.h +++ b/src/preferences/time/DateTimeEdit.h @@ -18,10 +18,6 @@ #include -using BPrivate::BDate; -using BPrivate::BTime; - - class TTimeEdit : public TSectionEdit { public: TTimeEdit(BRect frame, const char *name, uint32 sections);