📄 datetime.h
字号:
wxString Format(const wxChar *format = _T("%c"),
const TimeZone& tz = Local) const;
// preferred date representation for the current locale
wxString FormatDate() const { return Format(_T("%x")); }
// preferred time representation for the current locale
wxString FormatTime() const { return Format(_T("%X")); }
// returns the string representing the date in ISO 8601 format
// (YYYY-MM-DD)
wxString FormatISODate() const { return Format(_T("%Y-%m-%d")); }
// returns the string representing the time in ISO 8601 format
// (HH:MM:SS)
wxString FormatISOTime() const { return Format(_T("%H:%M:%S")); }
// implementation
// ------------------------------------------------------------------------
// construct from internal representation
wxDateTime(const wxLongLong& time) { m_time = time; }
// get the internal representation
inline wxLongLong GetValue() const;
// a helper function to get the current time_t
static time_t GetTimeNow() { return time((time_t *)NULL); }
// another one to get the current time broken down
static struct tm *GetTmNow()
{
time_t t = GetTimeNow();
return localtime(&t);
}
private:
// the current country - as it's the same for all program objects (unless
// it runs on a _really_ big cluster system :-), this is a static member:
// see SetCountry() and GetCountry()
static Country ms_country;
// this constant is used to transform a time_t value to the internal
// representation, as time_t is in seconds and we use milliseconds it's
// fixed to 1000
static const long TIME_T_FACTOR;
// returns TRUE if we fall in range in which we can use standard ANSI C
// functions
inline bool IsInStdRange() const;
// the internal representation of the time is the amount of milliseconds
// elapsed since the origin which is set by convention to the UNIX/C epoch
// value: the midnight of January 1, 1970 (UTC)
wxLongLong m_time;
};
// ----------------------------------------------------------------------------
// This class contains a difference between 2 wxDateTime values, so it makes
// sense to add it to wxDateTime and it is the result of subtraction of 2
// objects of that class. See also wxDateSpan.
// ----------------------------------------------------------------------------
class WXDLLEXPORT wxTimeSpan
{
public:
// constructors
// ------------------------------------------------------------------------
// return the timespan for the given number of seconds
static wxTimeSpan Seconds(long sec) { return wxTimeSpan(0, 0, sec); }
static wxTimeSpan Second() { return Seconds(1); }
// return the timespan for the given number of minutes
static wxTimeSpan Minutes(long min) { return wxTimeSpan(0, min, 0 ); }
static wxTimeSpan Minute() { return Minutes(1); }
// return the timespan for the given number of hours
static wxTimeSpan Hours(long hours) { return wxTimeSpan(hours, 0, 0); }
static wxTimeSpan Hour() { return Hours(1); }
// return the timespan for the given number of days
static wxTimeSpan Days(long days) { return Hours(24 * days); }
static wxTimeSpan Day() { return Days(1); }
// return the timespan for the given number of weeks
static wxTimeSpan Weeks(long days) { return Days(7 * days); }
static wxTimeSpan Week() { return Weeks(1); }
// default ctor constructs the 0 time span
wxTimeSpan() { }
// from separate values for each component, date set to 0 (hours are
// not restricted to 0..24 range, neither are minutes, seconds or
// milliseconds)
inline wxTimeSpan(long hours,
long minutes = 0,
long seconds = 0,
long milliseconds = 0);
// default copy ctor is ok
// no dtor
// arithmetics with time spans (see also below for more operators)
// ------------------------------------------------------------------------
// return the sum of two timespans
inline wxTimeSpan Add(const wxTimeSpan& diff) const;
// add two timespans together
inline wxTimeSpan& Add(const wxTimeSpan& diff);
// add two timespans together
wxTimeSpan& operator+=(const wxTimeSpan& diff) { return Add(diff); }
// return the difference of two timespans
inline wxTimeSpan Subtract(const wxTimeSpan& diff) const;
// subtract another timespan
inline wxTimeSpan& Subtract(const wxTimeSpan& diff);
// subtract another timespan
wxTimeSpan& operator-=(const wxTimeSpan& diff) { return Subtract(diff); }
// multiply timespan by a scalar
inline wxTimeSpan Multiply(int n) const;
// multiply timespan by a scalar
inline wxTimeSpan& Multiply(int n);
// multiply timespan by a scalar
wxTimeSpan& operator*=(int n) { return Multiply(n); }
// return this timespan with inversed sign
wxTimeSpan Negate() const { return wxTimeSpan(-GetValue()); }
// negate the value of the timespan
wxTimeSpan& Neg() { m_diff = -GetValue(); return *this; }
// negate the value of the timespan
wxTimeSpan& operator-() { return Neg(); }
// return the absolute value of the timespan: does _not_ modify the
// object
inline wxTimeSpan Abs() const;
// there is intentionally no division because we don't want to
// introduce rounding errors in time calculations
// comparaison (see also operator versions below)
// ------------------------------------------------------------------------
// is the timespan null?
bool IsNull() const { return m_diff == 0l; }
// returns true if the timespan is null
bool operator!() const { return !IsNull(); }
// is the timespan positive?
bool IsPositive() const { return m_diff > 0l; }
// is the timespan negative?
bool IsNegative() const { return m_diff < 0l; }
// are two timespans equal?
inline bool IsEqualTo(const wxTimeSpan& ts) const;
// compare two timestamps: works with the absolute values, i.e. -2
// hours is longer than 1 hour. Also, it will return FALSE if the
// timespans are equal in absolute value.
inline bool IsLongerThan(const wxTimeSpan& ts) const;
// compare two timestamps: works with the absolute values, i.e. 1
// hour is shorter than -2 hours. Also, it will return FALSE if the
// timespans are equal in absolute value.
bool IsShorterThan(const wxTimeSpan& t) const { return !IsLongerThan(t); }
// breaking into days, hours, minutes and seconds
// ------------------------------------------------------------------------
// get the max number of weeks in this timespan
inline int GetWeeks() const;
// get the max number of days in this timespan
inline int GetDays() const;
// get the max number of hours in this timespan
inline int GetHours() const;
// get the max number of minutes in this timespan
inline int GetMinutes() const;
// get the max number of seconds in this timespan
inline wxLongLong GetSeconds() const;
// get the number of milliseconds in this timespan
wxLongLong GetMilliseconds() const { return m_diff; }
// conversion to text
// ------------------------------------------------------------------------
// this function accepts strftime()-like format string (default
// argument corresponds to the preferred date and time representation
// for the current locale) and returns the string containing the
// resulting text representation. Notice that only some of format
// specifiers valid for wxDateTime are valid for wxTimeSpan: hours,
// minutes and seconds make sense, but not "PM/AM" string for example.
wxString Format(const wxChar *format = _T("%H:%M:%S")) const;
// implementation
// ------------------------------------------------------------------------
// construct from internal representation
wxTimeSpan(const wxLongLong& diff) { m_diff = diff; }
// get the internal representation
wxLongLong GetValue() const { return m_diff; }
private:
// the (signed) time span in milliseconds
wxLongLong m_diff;
};
// ----------------------------------------------------------------------------
// This class is a "logical time span" and is useful for implementing program
// logic for such things as "add one month to the date" which, in general,
// doesn't mean to add 60*60*24*31 seconds to it, but to take the same date
// the next month (to understand that this is indeed different consider adding
// one month to Feb, 15 - we want to get Mar, 15, of course).
//
// When adding a month to the date, all lesser components (days, hours, ...)
// won't be changed unless the resulting date would be invalid: for example,
// Jan 31 + 1 month will be Feb 28, not (non existing) Feb 31.
//
// Because of this feature, adding and subtracting back again the same
// wxDateSpan will *not*, in general give back the original date: Feb 28 - 1
// month will be Jan 28, not Jan 31!
//
// wxDateSpan can be either positive or negative. They may be
// multiplied by scalars which multiply all deltas by the scalar: i.e. 2*(1
// month and 1 day) is 2 months and 2 days. They can be added together and
// with wxDateTime or wxTimeSpan, but the type of result is different for each
// case.
//
// Beware about weeks: if you specify both weeks and days, the total number of
// days added will be 7*weeks + days! See also GetTotalDays() function.
//
// Finally, notice that for adding hours, minutes &c you don't need this
// class: wxTimeSpan will do the job because there are no subtleties
// associated with those.
// ----------------------------------------------------------------------------
class WXDLLEXPORT wxDateSpan
{
public:
// constructors
// ------------------------------------------------------------------------
// this many years/months/weeks/days
wxDateSpan(int years = 0, int months = 0, int weeks = 0, int days = 0)
{
m_years = years;
m_months = months;
m_weeks = weeks;
m_days = days;
}
// get an object for the given number of days
static wxDateSpan Days(int days) { return wxDateSpan(0, 0, 0, days); }
static wxDateSpan Day() { return Days(1); }
// get an object for the given number of weeks
static wxDateSpan Weeks(int weeks) { return wxDateSpan(0, 0, weeks, 0); }
static wxDateSpan Week() { return Weeks(1); }
// get an object for the given number of months
static wxDateSpan Months(int mon) { return wxDateSpan(0, mon, 0, 0); }
static wxDateSpan Month() { return Months(1); }
// get an object for the given number of years
static wxDateSpan Years(int years) { return wxDateSpan(years, 0, 0, 0); }
static wxDateSpan Year() { return Years(1); }
// default copy ctor is ok
// no dtor
// accessors (all SetXXX() return the (modified) wxDateSpan object)
// ------------------------------------------------------------------------
// set number of years
wxDateSpan& SetYears(int n) { m_years = n; return *this; }
// set number of months
wxDateSpan& SetMonths(int n) { m_months = n; return *this; }
// set number of weeks
wxDateSpan& SetWeeks(int n) { m_weeks = n; return *this; }
// set number of days
wxDateSpan& SetDays(int n) { m_days = n; return *this; }
// get number of years
int GetYears() const { return m_years; }
// get number of months
int GetMonths() const { return m_months; }
// get number of weeks
int GetWeeks() const { return m_weeks; }
// get number of days
int GetDays() const { return m_days; }
// returns 7*GetWeeks() + GetDays()
int GetTotalDays() const { return 7*m_weeks + m_days; }
// arithmetics with date spans (see also below for more operators)
// ------------------------------------------------------------------------
// return sum of two date spans
inline wxDateSpan Add(const wxDateSpan& other) const;
// add another wxDateSpan to us
inline wxDateSpan& Add(const wxDateSpan& other);
// add another wxDateSpan to us
inline wxDateSpan& operator+=(const wxDateSpan& other);
// return difference of two date spans
inline wxDateSpan Subtract(const wxDateSpan& other) const;
// subtract another wxDateSpan from us
inline wxDateSpan& Subtract(const wxDateSpan& other);
// subtract another wxDateSpan from us
inline wxDateSpan& operator-=(const wxDateSpan& other);
// return a copy of this time span with changed sign
inline wxDateSpan Negate() const;
// inverse the sign of this timespan
inline wxDateSpan& Neg();
// inverse the sign of this timespan
wxDateSpan& operator-() { return Neg(); }
// return the date span proportional to this one with given factor
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -