| QxOrm 1.4.3 C++ Object Relational Mapping library

- Main Page
- Modules
- Namespaces
- Classes
- Files
- Directories

- File List
- File Members

QxDateTimeNeutral.h
Go to the documentation of this file.

    00001 /****************************************************************************
    00002 **
    00003 ** http://www.qxorm.com/
    00004 ** Copyright (C) 2013 Lionel Marty (contact@qxorm.com)
    00005 **
    00006 ** This file is part of the QxOrm library
    00007 **
    00008 ** This software is provided 'as-is', without any express or implied
    00009 ** warranty. In no event will the authors be held liable for any
    00010 ** damages arising from the use of this software
    00011 **
    00012 ** Commercial Usage
    00013 ** Licensees holding valid commercial QxOrm licenses may use this file in
    00014 ** accordance with the commercial license agreement provided with the
    00015 ** Software or, alternatively, in accordance with the terms contained in
    00016 ** a written agreement between you and Lionel Marty
    00017 **
    00018 ** GNU General Public License Usage
    00019 ** Alternatively, this file may be used under the terms of the GNU
    00020 ** General Public License version 3.0 as published by the Free Software
    00021 ** Foundation and appearing in the file 'license.gpl3.txt' included in the
    00022 ** packaging of this file. Please review the following information to
    00023 ** ensure the GNU General Public License version 3.0 requirements will be
    00024 ** met : http://www.gnu.org/copyleft/gpl.html
    00025 **
    00026 ** If you are unsure which license is appropriate for your use, or
    00027 ** if you have questions regarding the use of this file, please contact :
    00028 ** contact@qxorm.com
    00029 **
    00030 ****************************************************************************/
    00031
    00032 #ifndef _QX_DATE_TIME_NEUTRAL_H_
    00033 #define _QX_DATE_TIME_NEUTRAL_H_
    00034
    00035 #ifdef _MSC_VER
    00036 #pragma once
    00037 #endif
    00038
    00046 #ifdef _QX_ENABLE_BOOST_SERIALIZATION
    00047 #include <boost/serialization/serialization.hpp>
    00048 #include <boost/serialization/nvp.hpp>
    00049 #endif // _QX_ENABLE_BOOST_SERIALIZATION
    00050
    00051 #include <QtCore/qdatetime.h>
    00052 #include <QtCore/qdatastream.h>
    00053
    00054 #include <QxSerialize/Qt/QxSerialize_QString.h>
    00055
    00056 #include <QxTraits/get_class_name.h>
    00057
    00058 namespace qx {
    00059 class QxDateTimeNeutral;
    00060 } // namespace qx
    00061
    00062 QX_DLL_EXPORT QDataStream & operator<< (QDataStream & stream, const qx::QxDateTimeNeutral & t) BOOST_USED;
    00063 QX_DLL_EXPORT QDataStream & operator>> (QDataStream & stream, qx::QxDateTimeNeutral & t) BOOST_USED;
    00064
    00065 namespace qx {
    00066
    00071 class QxDateTimeNeutral
    00072 {
    00073
    00074 #ifdef _QX_ENABLE_BOOST_SERIALIZATION
    00075    friend class boost::serialization::access;
    00076 #endif // _QX_ENABLE_BOOST_SERIALIZATION
    00077
    00078    friend QDataStream & ::operator<< (QDataStream & stream, const qx::QxDateTimeNeutral & t);
    00079    friend QDataStream & ::operator>> (QDataStream & stream, qx::QxDateTimeNeutral & t);
    00080
    00081 private:
    00082
    00083    QDateTime m_dt;
    00084    QString m_neutral;
    00085
    00086 public:
    00087
    00088    QxDateTimeNeutral() { ; }
    00089    explicit QxDateTimeNeutral(const QDateTime & dt) : m_dt(dt) { update(); }
    00090    explicit QxDateTimeNeutral(const QString & neutral) : m_neutral(neutral) { update(); }
    00091    virtual ~QxDateTimeNeutral() { ; }
    00092
    00093    inline QDateTime toDateTime() const    { return m_dt; }
    00094    inline QString toNeutral() const       { return m_neutral; }
    00095    inline bool isValid() const            { return m_dt.isValid(); }
    00096
    00097    inline void setDateTime(const QDateTime & dt)      { m_neutral = ""; m_dt = dt; update(); }
    00098    inline void setNeutral(const QString & neutral)    { m_dt = QDateTime(); m_neutral = neutral; update(); }
    00099
    00100    static QxDateTimeNeutral fromDateTime(const QDateTime & dt)    { return QxDateTimeNeutral(dt); }
    00101    static QxDateTimeNeutral fromNeutral(const QString & neutral)  { return QxDateTimeNeutral(neutral); }
    00102
    00103 private:
    00104
    00105    static inline const char * format() { return "yyyyMMddhhmmss"; }
    00106
    00107    void update()
    00108    {
    00109       if (m_neutral.isEmpty() && ! m_dt.isValid()) { return; }
    00110       else if (m_dt.isValid()) { m_neutral = m_dt.toString(format()); }
    00111       else { qAssert(m_neutral.size() == QString(format()).size()); m_dt = QDateTime::fromString(m_neutral, format()); qAssert(m_dt.isValid()); }
    00112    }
    00113
    00114 #ifdef _QX_ENABLE_BOOST_SERIALIZATION
    00115    template <class Archive>
    00116    void serialize(Archive & ar, const unsigned int file_version)
    00117    {
    00118       Q_UNUSED(file_version);
    00119       ar & boost::serialization::make_nvp("dt_neutral", m_neutral);
    00120       if (Archive::is_loading::value) { m_dt = QDateTime(); update(); }
    00121    }
    00122 #endif // _QX_ENABLE_BOOST_SERIALIZATION
    00123
    00124 };
    00125
    00126 } // namespace qx
    00127
    00128 QX_REGISTER_CLASS_NAME(qx::QxDateTimeNeutral)
    00129
    00130 #endif // _QX_DATE_TIME_NEUTRAL_H_