1999-11-30 21:55:00 +00:00
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Name: wx/datetime.inl
|
|
|
|
// Purpose: definition of inline functions of wxDateTime and related
|
|
|
|
// classes declared in datetime.h
|
|
|
|
// Author: Vadim Zeitlin
|
|
|
|
// Remarks: having the inline functions here allows us to minimize the
|
|
|
|
// dependencies (and hence the rebuild time) in debug builds.
|
|
|
|
// Modified by:
|
|
|
|
// Created: 30.11.99
|
|
|
|
// RCS-ID: $Id$
|
|
|
|
// Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
|
|
|
|
// Licence: wxWindows license
|
|
|
|
/////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef INCLUDED_FROM_WX_DATETIME_H
|
|
|
|
#error "This file is only included by wx/datetime.h, don't include it manually!"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// wxDateTime construction
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// only define this once, when included from datetime.cpp
|
|
|
|
#ifdef wxDEFINE_TIME_CONSTANTS
|
1999-12-21 01:44:45 +00:00
|
|
|
const long wxDateTime::TIME_T_FACTOR = 1000l;
|
1999-11-30 21:55:00 +00:00
|
|
|
#endif // wxDEFINE_TIME_CONSTANTS
|
|
|
|
|
1999-12-01 22:15:53 +00:00
|
|
|
bool wxDateTime::IsInStdRange() const
|
1999-11-30 21:55:00 +00:00
|
|
|
{
|
1999-12-21 01:44:45 +00:00
|
|
|
return m_time >= 0l && (m_time / TIME_T_FACTOR) < LONG_MAX;
|
1999-11-30 21:55:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
wxDateTime wxDateTime::Now()
|
|
|
|
{
|
|
|
|
return wxDateTime(GetTimeNow());
|
|
|
|
}
|
|
|
|
|
1999-12-21 01:44:45 +00:00
|
|
|
/* static */
|
|
|
|
wxDateTime wxDateTime::Today()
|
|
|
|
{
|
|
|
|
return wxDateTime((time_t)(86400*(GetTimeNow() / 86400)));
|
|
|
|
}
|
|
|
|
|
1999-11-30 21:55:00 +00:00
|
|
|
wxDateTime& wxDateTime::Set(time_t timet)
|
|
|
|
{
|
|
|
|
// assign first to avoid long multiplication overflow!
|
|
|
|
m_time = timet;
|
|
|
|
m_time *= TIME_T_FACTOR;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
wxDateTime& wxDateTime::SetToCurrent()
|
|
|
|
{
|
|
|
|
return Set(GetTimeNow());
|
|
|
|
}
|
|
|
|
|
|
|
|
wxDateTime::wxDateTime(time_t timet)
|
|
|
|
{
|
|
|
|
Set(timet);
|
|
|
|
}
|
|
|
|
|
|
|
|
wxDateTime::wxDateTime(const struct tm& tm)
|
|
|
|
{
|
|
|
|
Set(tm);
|
|
|
|
}
|
|
|
|
|
|
|
|
wxDateTime::wxDateTime(const Tm& tm)
|
|
|
|
{
|
|
|
|
Set(tm);
|
|
|
|
}
|
|
|
|
|
1999-12-02 01:01:26 +00:00
|
|
|
wxDateTime::wxDateTime(double jdn)
|
|
|
|
{
|
|
|
|
Set(jdn);
|
|
|
|
}
|
|
|
|
|
1999-11-30 21:55:00 +00:00
|
|
|
wxDateTime& wxDateTime::Set(const Tm& tm)
|
|
|
|
{
|
|
|
|
wxASSERT_MSG( tm.IsValid(), _T("invalid broken down date/time") );
|
|
|
|
|
|
|
|
return Set(tm.mday, (Month)tm.mon, tm.year, tm.hour, tm.min, tm.sec);
|
|
|
|
}
|
|
|
|
|
|
|
|
wxDateTime::wxDateTime(wxDateTime_t hour,
|
|
|
|
wxDateTime_t minute,
|
|
|
|
wxDateTime_t second,
|
|
|
|
wxDateTime_t millisec)
|
|
|
|
{
|
|
|
|
Set(hour, minute, second, millisec);
|
|
|
|
}
|
|
|
|
|
|
|
|
wxDateTime::wxDateTime(wxDateTime_t day,
|
|
|
|
Month month,
|
|
|
|
int year,
|
|
|
|
wxDateTime_t hour,
|
|
|
|
wxDateTime_t minute,
|
|
|
|
wxDateTime_t second,
|
|
|
|
wxDateTime_t millisec)
|
|
|
|
{
|
|
|
|
Set(day, month, year, hour, minute, second, millisec);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// wxDateTime accessors
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
wxLongLong wxDateTime::GetValue() const
|
|
|
|
{
|
1999-12-21 01:44:45 +00:00
|
|
|
wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
|
1999-11-30 21:55:00 +00:00
|
|
|
|
|
|
|
return m_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
time_t wxDateTime::GetTicks() const
|
|
|
|
{
|
1999-12-21 01:44:45 +00:00
|
|
|
wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
|
1999-11-30 21:55:00 +00:00
|
|
|
if ( !IsInStdRange() )
|
|
|
|
{
|
|
|
|
return (time_t)-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (time_t)((m_time / (long)TIME_T_FACTOR).GetLo());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool wxDateTime::SetToLastWeekDay(WeekDay weekday,
|
|
|
|
Month month,
|
|
|
|
int year)
|
|
|
|
{
|
|
|
|
return SetToWeekDay(weekday, -1, month, year);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// wxDateTime comparison
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
bool wxDateTime::IsEqualTo(const wxDateTime& datetime) const
|
|
|
|
{
|
1999-12-21 01:44:45 +00:00
|
|
|
wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
|
1999-11-30 21:55:00 +00:00
|
|
|
|
|
|
|
return m_time == datetime.m_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool wxDateTime::IsEarlierThan(const wxDateTime& datetime) const
|
|
|
|
{
|
1999-12-21 01:44:45 +00:00
|
|
|
wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
|
1999-11-30 21:55:00 +00:00
|
|
|
|
|
|
|
return m_time < datetime.m_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool wxDateTime::IsLaterThan(const wxDateTime& datetime) const
|
|
|
|
{
|
1999-12-21 01:44:45 +00:00
|
|
|
wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
|
1999-11-30 21:55:00 +00:00
|
|
|
|
|
|
|
return m_time > datetime.m_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool wxDateTime::IsStrictlyBetween(const wxDateTime& t1,
|
|
|
|
const wxDateTime& t2) const
|
|
|
|
{
|
|
|
|
// no need for assert, will be checked by the functions we call
|
|
|
|
return IsLaterThan(t1) && IsEarlierThan(t2);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool wxDateTime::IsBetween(const wxDateTime& t1, const wxDateTime& t2) const
|
|
|
|
{
|
|
|
|
// no need for assert, will be checked by the functions we call
|
|
|
|
return IsEqualTo(t1) || IsEqualTo(t2) || IsStrictlyBetween(t1, t2);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// wxDateTime arithmetics
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
1999-12-21 01:44:45 +00:00
|
|
|
wxDateTime wxDateTime::Add(const wxTimeSpan& diff) const
|
|
|
|
{
|
|
|
|
wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
|
|
|
|
|
|
|
|
return wxDateTime(m_time + diff.GetValue());
|
|
|
|
}
|
|
|
|
|
1999-11-30 21:55:00 +00:00
|
|
|
wxDateTime& wxDateTime::Add(const wxTimeSpan& diff)
|
|
|
|
{
|
1999-12-21 01:44:45 +00:00
|
|
|
wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
|
1999-11-30 21:55:00 +00:00
|
|
|
|
|
|
|
m_time += diff.GetValue();
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
wxDateTime& wxDateTime::operator+=(const wxTimeSpan& diff)
|
|
|
|
{
|
|
|
|
return Add(diff);
|
|
|
|
}
|
|
|
|
|
1999-12-21 01:44:45 +00:00
|
|
|
wxDateTime wxDateTime::Substract(const wxTimeSpan& diff) const
|
|
|
|
{
|
|
|
|
wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
|
|
|
|
|
|
|
|
return wxDateTime(m_time - diff.GetValue());
|
|
|
|
}
|
|
|
|
|
1999-11-30 21:55:00 +00:00
|
|
|
wxDateTime& wxDateTime::Substract(const wxTimeSpan& diff)
|
|
|
|
{
|
1999-12-21 01:44:45 +00:00
|
|
|
wxASSERT_MSG( IsValid(), _T("invalid wxDateTime"));
|
1999-11-30 21:55:00 +00:00
|
|
|
|
|
|
|
m_time -= diff.GetValue();
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
wxDateTime& wxDateTime::operator-=(const wxTimeSpan& diff)
|
|
|
|
{
|
|
|
|
return Substract(diff);
|
|
|
|
}
|
|
|
|
|
|
|
|
wxTimeSpan wxDateTime::Substract(const wxDateTime& datetime) const
|
|
|
|
{
|
1999-12-21 01:44:45 +00:00
|
|
|
wxASSERT_MSG( IsValid() && datetime.IsValid(), _T("invalid wxDateTime"));
|
1999-11-30 21:55:00 +00:00
|
|
|
|
|
|
|
return wxTimeSpan(datetime.GetValue() - GetValue());
|
|
|
|
}
|
|
|
|
|
1999-12-21 15:40:13 +00:00
|
|
|
wxDateTime wxDateTime::Add(const wxDateSpan& diff) const
|
|
|
|
{
|
|
|
|
return wxDateTime(*this).Add(diff);
|
|
|
|
}
|
|
|
|
|
1999-11-30 21:55:00 +00:00
|
|
|
wxDateTime& wxDateTime::Substract(const wxDateSpan& diff)
|
|
|
|
{
|
|
|
|
return Add(diff.Negate());
|
|
|
|
}
|
|
|
|
|
1999-12-21 15:40:13 +00:00
|
|
|
wxDateTime wxDateTime::Substract(const wxDateSpan& diff) const
|
|
|
|
{
|
|
|
|
return wxDateTime(*this).Substract(diff);
|
|
|
|
}
|
|
|
|
|
1999-11-30 21:55:00 +00:00
|
|
|
wxDateTime& wxDateTime::operator-=(const wxDateSpan& diff)
|
|
|
|
{
|
|
|
|
return Substract(diff);
|
|
|
|
}
|
|
|
|
|
|
|
|
wxDateTime& wxDateTime::operator+=(const wxDateSpan& diff)
|
|
|
|
{
|
|
|
|
return Add(diff);
|
|
|
|
}
|
|
|
|
|
1999-12-01 22:15:53 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// wxDateTime and timezones
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
wxDateTime wxDateTime::ToTimezone(const wxDateTime::TimeZone& tz) const
|
|
|
|
{
|
|
|
|
return wxDateTime(*this).MakeTimezone(tz);
|
|
|
|
}
|
|
|
|
|
1999-11-30 21:55:00 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
1999-12-02 01:01:26 +00:00
|
|
|
// wxTimeSpan construction
|
1999-11-30 21:55:00 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
1999-12-01 22:15:53 +00:00
|
|
|
wxTimeSpan::wxTimeSpan(int hours, int minutes, int seconds, int milliseconds)
|
|
|
|
{
|
|
|
|
// assign first to avoid precision loss
|
1999-12-21 01:44:45 +00:00
|
|
|
m_diff = (long)hours;
|
|
|
|
m_diff *= 60l;
|
1999-12-01 22:15:53 +00:00
|
|
|
m_diff += minutes;
|
1999-12-21 01:44:45 +00:00
|
|
|
m_diff *= 60l;
|
1999-12-01 22:15:53 +00:00
|
|
|
m_diff += seconds;
|
1999-12-21 01:44:45 +00:00
|
|
|
m_diff *= 1000l;
|
1999-12-01 22:15:53 +00:00
|
|
|
m_diff += milliseconds;
|
|
|
|
}
|
|
|
|
|
1999-12-02 01:01:26 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// wxTimeSpan accessors
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
wxLongLong wxTimeSpan::GetSeconds() const
|
|
|
|
{
|
|
|
|
return m_diff / 1000l;
|
|
|
|
}
|
|
|
|
|
|
|
|
int wxTimeSpan::GetMinutes() const
|
|
|
|
{
|
|
|
|
return (GetSeconds() / 60l).GetLo();
|
|
|
|
}
|
|
|
|
|
|
|
|
int wxTimeSpan::GetHours() const
|
|
|
|
{
|
|
|
|
return GetMinutes() / 60;
|
|
|
|
}
|
|
|
|
|
|
|
|
int wxTimeSpan::GetDays() const
|
|
|
|
{
|
|
|
|
return GetHours() / 24;
|
|
|
|
}
|
|
|
|
|
|
|
|
int wxTimeSpan::GetWeeks() const
|
|
|
|
{
|
|
|
|
return GetDays() / 7;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// wxTimeSpan arithmetics
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
1999-12-21 01:44:45 +00:00
|
|
|
wxTimeSpan wxTimeSpan::Add(const wxTimeSpan& diff) const
|
|
|
|
{
|
|
|
|
return wxTimeSpan(m_diff + diff.GetValue());
|
|
|
|
}
|
|
|
|
|
1999-11-30 21:55:00 +00:00
|
|
|
wxTimeSpan& wxTimeSpan::Add(const wxTimeSpan& diff)
|
|
|
|
{
|
|
|
|
m_diff += diff.GetValue();
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
1999-12-21 01:44:45 +00:00
|
|
|
wxTimeSpan wxTimeSpan::Substract(const wxTimeSpan& diff) const
|
|
|
|
{
|
|
|
|
return wxTimeSpan(m_diff - diff.GetValue());
|
|
|
|
}
|
|
|
|
|
1999-11-30 21:55:00 +00:00
|
|
|
wxTimeSpan& wxTimeSpan::Substract(const wxTimeSpan& diff)
|
|
|
|
{
|
|
|
|
m_diff -= diff.GetValue();
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
wxTimeSpan& wxTimeSpan::Multiply(int n)
|
|
|
|
{
|
1999-12-21 01:44:45 +00:00
|
|
|
m_diff *= (long)n;
|
1999-11-30 21:55:00 +00:00
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
1999-12-21 01:44:45 +00:00
|
|
|
wxTimeSpan wxTimeSpan::Multiply(int n) const
|
1999-11-30 21:55:00 +00:00
|
|
|
{
|
1999-12-21 01:44:45 +00:00
|
|
|
return wxTimeSpan(m_diff * (long)n);
|
1999-11-30 21:55:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
wxTimeSpan wxTimeSpan::Abs() const
|
|
|
|
{
|
|
|
|
return wxTimeSpan(GetValue().Abs());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool wxTimeSpan::IsEqualTo(const wxTimeSpan& ts) const
|
|
|
|
{
|
|
|
|
return GetValue() == ts.GetValue();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool wxTimeSpan::IsLongerThan(const wxTimeSpan& ts) const
|
|
|
|
{
|
|
|
|
return GetValue().Abs() > ts.GetValue().Abs();
|
|
|
|
}
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// wxDateSpan
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
wxDateSpan&
|
|
|
|
wxDateSpan::operator+=(const wxDateSpan& other)
|
|
|
|
{
|
|
|
|
m_years += other.m_years;
|
|
|
|
m_months += other.m_months;
|
|
|
|
m_weeks += other.m_weeks;
|
|
|
|
m_days += other.m_days;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
1999-12-21 01:44:45 +00:00
|
|
|
wxDateSpan& wxDateSpan::Multiply(int factor)
|
1999-11-30 21:55:00 +00:00
|
|
|
{
|
|
|
|
m_years *= m_years;
|
|
|
|
m_months *= m_months;
|
|
|
|
m_weeks *= m_weeks;
|
|
|
|
m_days *= m_days;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
1999-12-21 01:44:45 +00:00
|
|
|
wxDateSpan wxDateSpan::Multiply(int factor) const
|
|
|
|
{
|
|
|
|
return wxDateSpan(*this).Multiply(factor);
|
|
|
|
}
|
|
|
|
|
1999-11-30 21:55:00 +00:00
|
|
|
wxDateSpan wxDateSpan::Negate() const
|
|
|
|
{
|
|
|
|
return wxDateSpan(-m_years, -m_months, -m_weeks, -m_days);
|
|
|
|
}
|
|
|
|
|
|
|
|
wxDateSpan& wxDateSpan::Neg()
|
|
|
|
{
|
|
|
|
m_years = -m_years;
|
|
|
|
m_months = -m_months;
|
|
|
|
m_weeks = -m_weeks;
|
|
|
|
m_days = -m_days;
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|