[haiku-commits] r35772 - in haiku/trunk: headers/os/support headers/private/shared src/add-ons/screen_savers/flurry src/apps/deskbar src/kits/shared ...

  • From: superstippi@xxxxxx
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Sun, 7 Mar 2010 13:08:47 +0100 (CET)

Author: stippi
Date: 2010-03-07 13:08:47 +0100 (Sun, 07 Mar 2010)
New Revision: 35772
Changeset: http://dev.haiku-os.org/changeset/35772/haiku

Added:
   haiku/trunk/headers/os/support/DateTime.h
   haiku/trunk/src/kits/support/DateTime.cpp
Removed:
   haiku/trunk/headers/private/shared/DateTime.h
   haiku/trunk/src/kits/shared/DateTime.cpp
Modified:
   haiku/trunk/src/add-ons/screen_savers/flurry/Flurry.cpp
   haiku/trunk/src/add-ons/screen_savers/flurry/Flurry.h
   haiku/trunk/src/add-ons/screen_savers/flurry/Jamfile
   haiku/trunk/src/apps/deskbar/CalendarMenuWindow.cpp
   haiku/trunk/src/apps/deskbar/CalendarMenuWindow.h
   haiku/trunk/src/kits/shared/Jamfile
   haiku/trunk/src/kits/support/Jamfile
   haiku/trunk/src/preferences/time/BaseView.cpp
   haiku/trunk/src/preferences/time/DateTimeEdit.h
Log:
 * 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.


Copied: haiku/trunk/headers/os/support/DateTime.h (from rev 35758, 
haiku/trunk/headers/private/shared/DateTime.h)
===================================================================
--- haiku/trunk/headers/os/support/DateTime.h                           (rev 0)
+++ haiku/trunk/headers/os/support/DateTime.h   2010-03-07 12:08:47 UTC (rev 
35772)
@@ -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 <String.h>
+
+
+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_

Modified: haiku/trunk/src/add-ons/screen_savers/flurry/Flurry.cpp
===================================================================
--- haiku/trunk/src/add-ons/screen_savers/flurry/Flurry.cpp     2010-03-07 
11:28:24 UTC (rev 35771)
+++ haiku/trunk/src/add-ons/screen_savers/flurry/Flurry.cpp     2010-03-07 
12:08:47 UTC (rev 35772)
@@ -274,8 +274,8 @@
 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));
 }
 
 

Modified: haiku/trunk/src/add-ons/screen_savers/flurry/Flurry.h
===================================================================
--- haiku/trunk/src/add-ons/screen_savers/flurry/Flurry.h       2010-03-07 
11:28:24 UTC (rev 35771)
+++ haiku/trunk/src/add-ons/screen_savers/flurry/Flurry.h       2010-03-07 
12:08:47 UTC (rev 35772)
@@ -39,8 +39,8 @@
                        double                                  fOldFrameTime;
                        flurry_info_t*                  fFlurryInfo_t;
 
-                       BPrivate::BTime                 fTime;
-                       BPrivate::BDateTime             fDateTime;
+                       BTime                                   fTime;
+                       BDateTime                               fDateTime;
 };
 
 

Modified: haiku/trunk/src/add-ons/screen_savers/flurry/Jamfile
===================================================================
--- haiku/trunk/src/add-ons/screen_savers/flurry/Jamfile        2010-03-07 
11:28:24 UTC (rev 35771)
+++ haiku/trunk/src/add-ons/screen_savers/flurry/Jamfile        2010-03-07 
12:08:47 UTC (rev 35772)
@@ -1,7 +1,5 @@
 SubDir HAIKU_TOP src add-ons screen_savers flurry ;
 
-UsePrivateHeaders shared ;
-
 ScreenSaver Flurry :
        Flurry.cpp
        Smoke.cpp

Modified: haiku/trunk/src/apps/deskbar/CalendarMenuWindow.cpp
===================================================================
--- haiku/trunk/src/apps/deskbar/CalendarMenuWindow.cpp 2010-03-07 11:28:24 UTC 
(rev 35771)
+++ haiku/trunk/src/apps/deskbar/CalendarMenuWindow.cpp 2010-03-07 12:08:47 UTC 
(rev 35772)
@@ -19,7 +19,6 @@
 
 
 using BPrivate::BCalendarView;
-using BPrivate::B_LOCAL_TIME;
 using BPrivate::B_WEEK_START_SUNDAY;
 using BPrivate::B_WEEK_START_MONDAY;
 

Modified: haiku/trunk/src/apps/deskbar/CalendarMenuWindow.h
===================================================================
--- haiku/trunk/src/apps/deskbar/CalendarMenuWindow.h   2010-03-07 11:28:24 UTC 
(rev 35771)
+++ haiku/trunk/src/apps/deskbar/CalendarMenuWindow.h   2010-03-07 12:08:47 UTC 
(rev 35772)
@@ -18,7 +18,6 @@
        class BCalendarView;
 }
 using BPrivate::BCalendarView;
-using BPrivate::BDate;
 
 
 class CalendarMenuWindow : public BWindow {

Modified: haiku/trunk/src/kits/shared/Jamfile
===================================================================
--- haiku/trunk/src/kits/shared/Jamfile 2010-03-07 11:28:24 UTC (rev 35771)
+++ haiku/trunk/src/kits/shared/Jamfile 2010-03-07 12:08:47 UTC (rev 35772)
@@ -16,7 +16,6 @@
        CalendarView.cpp
        ColorQuantizer.cpp
        CommandPipe.cpp
-       DateTime.cpp
        DragTrackingFilter.cpp
        HashString.cpp
        RWLockManager.cpp

Copied: haiku/trunk/src/kits/support/DateTime.cpp (from rev 35758, 
haiku/trunk/src/kits/shared/DateTime.cpp)
===================================================================
--- haiku/trunk/src/kits/support/DateTime.cpp                           (rev 0)
+++ haiku/trunk/src/kits/support/DateTime.cpp   2010-03-07 12:08:47 UTC (rev 
35772)
@@ -0,0 +1,1367 @@
+/*
+ * Copyright 2007-2010, Haiku, Inc. All Rights Reserved.
+ * Distributed under the terms of the MIT License.
+ *
+ * Authors:
+ *             Julun <host.haiku@xxxxxx>
+ *             Stephan Aßmus <superstippi@xxxxxx>
+ */
+
+#include "DateTime.h"
+
+
+#include <time.h>
+#include <sys/time.h>
+
+#include <Message.h>
+
+
+namespace BPrivate {
+
+
+const int32                    kSecondsPerMinute                       = 60;
+
+const int32                    kHoursPerDay                            = 24;
+const int32                    kMinutesPerDay                          = 1440;
+const int32                    kSecondsPerDay                          = 86400;
+const int32                    kMillisecondsPerDay                     = 
86400000;
+
+const bigtime_t                kMicrosecondsPerSecond          = 1000000LL;
+const bigtime_t                kMicrosecondsPerMinute          = 60000000LL;
+const bigtime_t                kMicrosecondsPerHour            = 3600000000LL;
+const bigtime_t                kMicrosecondsPerDay                     = 
86400000000LL;
+
+
+/*!
+       Constructs a new BTime object. Asked for its time representation, it 
will
+       return 0 for Hour(), Minute(), Second() etc. This can represent 
midnight,
+       but be aware IsValid() will return false.
+*/
+BTime::BTime()
+       : fMicroseconds(-1)
+{
+}
+
+
+/*!
+       Constructs a BTime object with \c hour \c minute, \c second, \c 
microsecond.
+
+       \c hour must be between 0 and 23, \c minute and \c second must be 
between
+       0 and 59 and \c microsecond should be in the range of 0 and 999999. If 
the
+       specified time is invalid, the time is not set and IsValid() returns 
false.
+*/
+BTime::BTime(int32 hour, int32 minute, int32 second, int32 microsecond)
+       : fMicroseconds(-1)
+{
+       _SetTime(hour, minute, second, 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.
+*/
+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.
+*/
+bool
+BTime::IsValid() const
+{
+       return fMicroseconds > -1 && fMicroseconds < kMicrosecondsPerDay;
+}
+
+
+/*!
+       This is an overloaded member function, provided for convenience.
+*/
+bool
+BTime::IsValid(const BTime& time) const
+{
+       return time.fMicroseconds > -1 && time.fMicroseconds < 
kMicrosecondsPerDay;
+}
+
+
+/*!
+       This is an overloaded member function, provided for convenience.
+*/
+bool
+BTime::IsValid(int32 hour, int32 minute, int32 second, int32 microsecond) const
+{
+       return BTime(hour, minute, second, microsecond).IsValid();
+}
+
+
+/*!
+       Returns the current time as reported by the system depending on the 
given
+       time_type \c type.
+*/
+BTime
+BTime::CurrentTime(time_type type)
+{
+       struct timeval tv;
+       if (gettimeofday(&tv, NULL) != 0) {
+               // gettimeofday failed?
+               time(&tv.tv_sec);
+       }
+
+       struct tm result;
+       struct tm* timeinfo;
+       if (type == B_GMT_TIME)
+               timeinfo = gmtime_r(&tv.tv_sec, &result);
+       else
+               timeinfo = localtime_r(&tv.tv_sec, &result);
+
+       int32 sec = timeinfo->tm_sec;
+       return BTime(timeinfo->tm_hour, timeinfo->tm_min, (sec > 59) ? 59 : sec,
+               tv.tv_usec);
+}
+
+
+/*!
+       Returns a copy of the current BTime object.
+*/
+BTime
+BTime::Time() const
+{
+       return *this;
+}
+
+
+/*!
+       This is an overloaded member function, provided for convenience. Set the
+       current BTime object to the passed BTime \c time object.
+*/
+bool
+BTime::SetTime(const BTime& time)
+{
+       fMicroseconds = time.fMicroseconds;
+       return IsValid();
+}
+
+
+/*!
+       Set the time to \c hour \c minute, \c second and \c microsecond.
+
+       \c hour must be between 0 and 23, \c minute and \c second must be 
between
+       0 and 59 and \c microsecond should be in the range of 0 and 999999. 
Returns
+       true if the time is valid; otherwise false. If the specified time is
+       invalid, the time is not set and the function returns false.
+*/
+bool
+BTime::SetTime(int32 hour, int32 minute, int32 second, int32 microsecond)
+{
+       return _SetTime(hour, minute, second, microsecond);
+}
+
+
+
+/*!
+       Adds \c hours to the current time. If the passed value is negativ it 
will
+       become earlier. Note: The time will wrap if it passes midnight.
+*/
+void
+BTime::AddHours(int32 hours)
+{
+       _AddMicroseconds(bigtime_t(hours % kHoursPerDay) * 
kMicrosecondsPerHour);
+}
+
+
+/*!
+       Adds \c minutes to the current time. If the passed value is negativ it 
will
+       become earlier. Note: The time will wrap if it passes midnight.
+*/
+void
+BTime::AddMinutes(int32 minutes)
+{
+       _AddMicroseconds(bigtime_t(minutes % kMinutesPerDay) *
+               kMicrosecondsPerMinute);
+}
+
+
+/*!
+       Adds \c seconds to the current time. If the passed value is negativ it 
will
+       become earlier. Note: The time will wrap if it passes midnight.
+*/
+void
+BTime::AddSeconds(int32 seconds)
+{
+       _AddMicroseconds(bigtime_t(seconds % kSecondsPerDay) *
+               kMicrosecondsPerSecond);
+}
+
+
+/*!
+       Adds \c milliseconds to the current time. If the passed value is 
negativ it
+       will become earlier. Note: The time will wrap if it passes midnight.
+*/
+void
+BTime::AddMilliseconds(int32 milliseconds)
+{
+       _AddMicroseconds(bigtime_t(milliseconds % kMillisecondsPerDay) * 1000);
+}
+
+
+/*!
+       Adds \c microseconds to the current time. If the passed value is 
negativ it
+       will become earlier. Note: The time will wrap if it passes midnight.
+*/
+void
+BTime::AddMicroseconds(int32 microseconds)
+{
+       _AddMicroseconds(microseconds);
+}
+
+
+/*!
+       Returns the hour fragment of the time.
+*/
+int32
+BTime::Hour() const
+{
+       return int32(_Microseconds() / kMicrosecondsPerHour);
+}
+
+
+/*!
+       Returns the minute fragment of the time.
+*/
+int32
+BTime::Minute() const
+{
+       return int32(((_Microseconds() % kMicrosecondsPerHour)) / 
kMicrosecondsPerMinute);
+}
+
+
+/*!
+       Returns the second fragment of the time.
+*/
+int32
+BTime::Second() const
+{
+       return int32(_Microseconds() / kMicrosecondsPerSecond) % 
kSecondsPerMinute;
+}
+
+
+/*!
+       Returns the millisecond fragment of the time.
+*/
+int32
+BTime::Millisecond() const
+{
+
+       return Microsecond() / 1000;
+}
+
+
+/*!
+       Returns the microsecond fragment of the time.
+*/
+int32
+BTime::Microsecond() const
+{
+       return int32(_Microseconds() % 1000000);
+}
+
+
+bigtime_t
+BTime::_Microseconds() const
+{
+       return fMicroseconds == -1 ? 0 : fMicroseconds;
+}
+
+
+/*!
+       Returns the difference between this time and the given BTime \c time 
based
+       on the passed diff_type \c type. If \c time is earlier the return value 
will
+       be negativ.
+
+       The return value then can be hours, minutes, seconds, milliseconds or
+       microseconds while its range will always be between -86400000000 and
+       86400000000 depending on diff_type \c type.
+*/
+bigtime_t
+BTime::Difference(const BTime& time, diff_type type) const
+{
+       bigtime_t diff = time._Microseconds() - _Microseconds();
+       switch (type) {
+               case B_HOURS_DIFF: {
+                       diff /= kMicrosecondsPerHour;
+               }       break;
+               case B_MINUTES_DIFF: {
+                       diff /= kMicrosecondsPerMinute;
+               }       break;
+               case B_SECONDS_DIFF: {
+                       diff /= kMicrosecondsPerSecond;
+               }       break;
+               case B_MILLISECONDS_DIFF: {
+                       diff /= 1000;
+               }       break;
+               case B_MICROSECONDS_DIFF:
+               default:        break;
+       }
+       return diff;
+}
+
+
+/*!
+       Returns true if this time is different from \c time, otherwise false.
+*/
+bool
+BTime::operator!=(const BTime& time) const
+{
+       return fMicroseconds != time.fMicroseconds;
+}
+
+
+/*!
+       Returns true if this time is equal to \c time, otherwise false.
+*/
+bool
+BTime::operator==(const BTime& time) const
+{
+       return fMicroseconds == time.fMicroseconds;
+}
+
+
+/*!
+       Returns true if this time is earlier than \c time, otherwise false.
+*/
+bool
+BTime::operator<(const BTime& time) const
+{
+       return fMicroseconds < time.fMicroseconds;
+}
+
+
+/*!
+       Returns true if this time is earlier than or equal to \c time, 
otherwise false.
+*/
+bool
+BTime::operator<=(const BTime& time) const
+{
+       return fMicroseconds <= time.fMicroseconds;
+}
+
+
+/*!
+       Returns true if this time is later than \c time, otherwise false.
+*/
+bool
+BTime::operator>(const BTime& time) const
+{
+       return fMicroseconds > time.fMicroseconds;
+}
+
+
+/*!
+       Returns true if this time is later than or equal to \c time, otherwise 
false.
+*/
+bool
+BTime::operator>=(const BTime& time) const
+{
+       return fMicroseconds >= time.fMicroseconds;
+}
+
+
+void
+BTime::_AddMicroseconds(bigtime_t microseconds)
+{
+       bigtime_t count = 0;
+       if (microseconds < 0) {
+               count = ((kMicrosecondsPerDay - microseconds) / 
kMicrosecondsPerDay) *
+                       kMicrosecondsPerDay;
+       }
+       fMicroseconds = (_Microseconds() + microseconds + count) % 
kMicrosecondsPerDay;
+}
+
+
+bool
+BTime::_SetTime(bigtime_t hour, bigtime_t minute, bigtime_t second,
+       bigtime_t microsecond)
+{
+       fMicroseconds = hour * kMicrosecondsPerHour +
+                                       minute * kMicrosecondsPerMinute +
+                                       second * kMicrosecondsPerSecond +
+                                       microsecond;
+
+       bool isValid = IsValid();
+       if (!isValid)
+               fMicroseconds = -1;
+
+       return isValid;
+}
+
+
+//     #pragma mark - BDate
+
+
+/*!
+       Constructs a new BDate object. IsValid() will return false.
+*/
+BDate::BDate()
+       : fDay(-1),
+         fYear(0),
+         fMonth(-1)
+{
+}
+
+
+/*!
+       Constructs a BDate object with \c year \c month and \c day.
+
+       Please note that a date before 1.1.4713 BC, a date with year 0 and a 
date
+       between 4.10.1582 and 15.10.1582 are considered invalid. If the 
specified
+       date is invalid, the date is not set and IsValid() returns false. Also 
note
+       that every passed year will be interpreted as is.
+
+*/
+BDate::BDate(int32 year, int32 month, int32 day)
+{
+       _SetDate(year, month, 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.
+*/
+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.
+
+       Please note that a date before 1.1.4713 BC, a date with year 0 and a 
date
+       between 4.10.1582 and 15.10.1582 are considered invalid.
+*/
+bool
+BDate::IsValid() const
+{
+       return IsValid(fYear, fMonth, fDay);
+}
+
+
+/*!
+       This is an overloaded member function, provided for convenience.
+*/
+bool
+BDate::IsValid(const BDate& date) const
+{
+       return IsValid(date.fYear, date.fMonth, date.fDay);
+}
+
+
+/*!
+       This is an overloaded member function, provided for convenience.
+*/
+bool
+BDate::IsValid(int32 year, int32 month, int32 day) const
+{
+       // no year 0 in Julian and nothing before 1.1.4713 BC
+       if (year == 0 || year < -4713)
+               return false;
+
+       if (month < 1 || month > 12)
+               return false;
+
+       if (day < 1 || day > _DaysInMonth(year, month))
+               return false;
+
+       // 'missing' days between switch julian - gregorian
+       if (year == 1582 && month == 10 && day > 4 && day < 15)
+               return false;
+
+       return true;
+}
+
+
+/*!
+       Returns the current date as reported by the system depending on the 
given
+       time_type \c type.
+*/
+BDate
+BDate::CurrentDate(time_type type)
+{
+       time_t timer;
+       struct tm result;
+       struct tm* timeinfo;
+
+       time(&timer);
+
+       if (type == B_GMT_TIME)
+               timeinfo = gmtime_r(&timer, &result);
+       else
+               timeinfo = localtime_r(&timer, &result);
+
+       return BDate(timeinfo->tm_year + 1900, timeinfo->tm_mon +1, 
timeinfo->tm_mday);
+}
+
+
+/*!
+       Returns a copy of the current BTime object.
+*/
+BDate
+BDate::Date() const
+{
+       return *this;
+}
+
+
+/*!
+       This is an overloaded member function, provided for convenience.
+*/
+bool
+BDate::SetDate(const BDate& date)
+{
+       return _SetDate(date.fYear, date.fMonth, date.fDay);
+}
+
+
+/*!
+       Set the date to \c year \c month and \c day.
+
+       Returns true if the date is valid; otherwise false. If the specified 
date is
+       invalid, the date is not set and the function returns false.
+*/
+bool
+BDate::SetDate(int32 year, int32 month, int32 day)
+{
+       return _SetDate(year, month, day);
+}
+
+
+/*!
+       This function sets the given \c year, \c month and \c day to the
+       representative values of this date. The pointers can be NULL. If the 
date is
+       invalid, the values will be set to -1 for \c month and \c day, the \c 
year
+       will be set to 0.
+*/
+void
+BDate::GetDate(int32* year, int32* month, int32* day)
+{
+       if (year)
+               *year = fYear;
+
+       if (month)
+               *month = fMonth;
+
+       if (day)
+               *day = fDay;
+}
+
+
+/*!
+       Adds \c days to the current date. If the passed value is negativ it will
+       become earlier. If the current date is invalid, the \c days are not 
added.
+*/
+void
+BDate::AddDays(int32 days)
+{
+       if (IsValid())
+               *this = JulianDayToDate(DateToJulianDay() + days);
+}
+
+
+/*!
+       Adds \c years to the current date. If the passed value is negativ it 
will
+       become earlier. If the current date is invalid, the \c years are not 
added.
+       The day/ month combination will be adjusted if it does not exist in the
+       resulting year, so this function will then return the latest valid date.
+*/
+void
+BDate::AddYears(int32 years)
+{
+       if (IsValid()) {
+               const int32 tmp = fYear;
+               fYear += years;
+
+               if ((tmp > 0 && fYear <= 0) || (tmp < 0 && fYear >= 0))
+                       fYear += (years > 0) ? +1 : -1;
+
+               fDay = min_c(fDay, _DaysInMonth(fYear, fMonth));
+       }
+}
+
+
+/*!
+       Adds \c months to the current date. If the passed value is negativ it 
will
+       become earlier. If the current date is invalid, the \c months are not 
added.
+       The day/ month combination will be adjusted if it does not exist in the
+       resulting year, so this function will then return the latest valid date.
+*/
+void
+BDate::AddMonths(int32 months)
+{
+       if (IsValid()) {
+               const int32 tmp = fYear;
+               fYear += months / 12;
+               fMonth +=  months % 12;
+
+               if (fMonth > 12) {
+                       fYear++;
+                       fMonth -= 12;
+               } else if (fMonth < 1) {
+                       fYear--;
+                       fMonth += 12;
+               }
+
+               if ((tmp > 0 && fYear <= 0) || (tmp < 0 && fYear >= 0))
+                       fYear += (months > 0) ? +1 : -1;
+
+               // '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());
+       }
+}
+
+

[... truncated: 725 lines follow ...]

Other related posts:

  • » [haiku-commits] r35772 - in haiku/trunk: headers/os/support headers/private/shared src/add-ons/screen_savers/flurry src/apps/deskbar src/kits/shared ... - superstippi