datetime.h

来自「A*算法 A*算法 A*算法 A*算法A*算法A*算法」· C头文件 代码 · 共 1,563 行 · 第 1/5 页

H
1,563
字号

    inline bool operator!=(const wxDateTime& dt) const
    {
        wxASSERT_MSG( IsValid() && dt.IsValid(), _T("invalid wxDateTime") );
        return GetValue() != dt.GetValue();
    }

    // arithmetics with dates (see also below for more operators)
    // ------------------------------------------------------------------------

        // return the sum of the date with a time span (positive or negative)
    inline wxDateTime Add(const wxTimeSpan& diff) const;
        // add a time span (positive or negative)
    inline wxDateTime& Add(const wxTimeSpan& diff);
        // add a time span (positive or negative)
    inline wxDateTime& operator+=(const wxTimeSpan& diff);
    inline wxDateTime operator+(const wxTimeSpan& ts) const
    {
        wxDateTime dt(*this);
        dt.Add(ts);
        return dt;
    }

        // return the difference of the date with a time span
    inline wxDateTime Subtract(const wxTimeSpan& diff) const;
        // subtract a time span (positive or negative)
    inline wxDateTime& Subtract(const wxTimeSpan& diff);
        // subtract a time span (positive or negative)
    inline wxDateTime& operator-=(const wxTimeSpan& diff);
    inline wxDateTime operator-(const wxTimeSpan& ts) const
    {
        wxDateTime dt(*this);
        dt.Subtract(ts);
        return dt;
    }

        // return the sum of the date with a date span
    inline wxDateTime Add(const wxDateSpan& diff) const;
        // add a date span (positive or negative)
    wxDateTime& Add(const wxDateSpan& diff);
        // add a date span (positive or negative)
    inline wxDateTime& operator+=(const wxDateSpan& diff);
    inline wxDateTime operator+(const wxDateSpan& ds) const
    {
        wxDateTime dt(*this);
        dt.Add(ds);
        return dt;
    }

        // return the difference of the date with a date span
    inline wxDateTime Subtract(const wxDateSpan& diff) const;
        // subtract a date span (positive or negative)
    inline wxDateTime& Subtract(const wxDateSpan& diff);
        // subtract a date span (positive or negative)
    inline wxDateTime& operator-=(const wxDateSpan& diff);
    inline wxDateTime operator-(const wxDateSpan& ds) const
    {
        wxDateTime dt(*this);
        dt.Subtract(ds);
        return dt;
    }

        // return the difference between two dates
    inline wxTimeSpan Subtract(const wxDateTime& dt) const;
    inline wxTimeSpan operator-(const wxDateTime& dt2) const;

    // conversion to/from text: all conversions from text return the pointer to
    // the next character following the date specification (i.e. the one where
    // the scan had to stop) or NULL on failure.
    // ------------------------------------------------------------------------

        // parse a string in RFC 822 format (found e.g. in mail headers and
        // having the form "Wed, 10 Feb 1999 19:07:07 +0100")
    const wxChar *ParseRfc822Date(const wxChar* date);
        // parse a date/time in the given format (see strptime(3)), fill in
        // the missing (in the string) fields with the values of dateDef (by
        // default, they will not change if they had valid values or will
        // default to Today() otherwise)
    const wxChar *ParseFormat(const wxChar *date,
                              const wxChar *format = wxDefaultDateTimeFormat,
                              const wxDateTime& dateDef = wxDefaultDateTime);
        // parse a string containing the date/time in "free" format, this
        // function will try to make an educated guess at the string contents
    const wxChar *ParseDateTime(const wxChar *datetime);
        // parse a string containing the date only in "free" format (less
        // flexible than ParseDateTime)
    const wxChar *ParseDate(const wxChar *date);
        // parse a string containing the time only in "free" format
    const wxChar *ParseTime(const wxChar *time);

        // 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
    wxString Format(const wxChar *format = wxDefaultDateTimeFormat,
                    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 WXDLLIMPEXP_BASE 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); }
    inline wxTimeSpan operator+(const wxTimeSpan& ts) const
    {
        return wxTimeSpan(GetValue() + ts.GetValue());
    }

        // 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); }
    inline wxTimeSpan operator-(const wxTimeSpan& ts)
    {
        return wxTimeSpan(GetValue() - ts.GetValue());
    }

        // 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); }
    inline wxTimeSpan operator*(int n) const
    {
        return wxTimeSpan(*this).Multiply(n);
    }

        // return this timespan with opposite 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); }

    inline bool operator<(const wxTimeSpan &ts) const
    {
        return GetValue() < ts.GetValue();
    }

    inline bool operator<=(const wxTimeSpan &ts) const
    {
        return GetValue() <= ts.GetValue();
    }

    inline bool operator>(const wxTimeSpan &ts) const
    {
        return GetValue() > ts.GetValue();
    }

    inline bool operator>=(const wxTimeSpan &ts) const
    {
        return GetValue() >= ts.GetValue();
    }

    inline bool operator==(const wxTimeSpan &ts) const
    {
        return GetValue() == ts.GetValue();
    }

    inline bool operator!=(const wxTimeSpan &ts) const
    {
        return GetValue() != ts.GetValue();
    }

    // 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

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?