| QxOrm 1.2.8 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 #include <boost/serialization/serialization.hpp>
    00047 #include <boost/serialization/nvp.hpp>
    00048
    00049 #include <QtCore/qdatetime.h>
    00050
    00051 #include <QxSerialize/Qt/QxSerialize_QString.h>
    00052
    00053 #include <QxTraits/get_class_name.h>
    00054
    00055 namespace qx {
    00056
    00061 class QxDateTimeNeutral
    00062 {
    00063
    00064    friend class boost::serialization::access;
    00065
    00066 private:
    00067
    00068    QDateTime m_dt;
    00069    QString m_neutral;
    00070
    00071 public:
    00072
    00073    QxDateTimeNeutral() { ; }
    00074    explicit QxDateTimeNeutral(const QDateTime & dt) : m_dt(dt) { update(); }
    00075    explicit QxDateTimeNeutral(const QString & neutral) : m_neutral(neutral) { update(); }
    00076    virtual ~QxDateTimeNeutral() { ; }
    00077
    00078    inline QDateTime toDateTime() const    { return m_dt; }
    00079    inline QString toNeutral() const       { return m_neutral; }
    00080    inline bool isValid() const            { return m_dt.isValid(); }
    00081
    00082    inline void setDateTime(const QDateTime & dt)      { m_neutral = ""; m_dt = dt; update(); }
    00083    inline void setNeutral(const QString & neutral)    { m_dt = QDateTime(); m_neutral = neutral; update(); }
    00084
    00085    static QxDateTimeNeutral fromDateTime(const QDateTime & dt)    { return QxDateTimeNeutral(dt); }
    00086    static QxDateTimeNeutral fromNeutral(const QString & neutral)  { return QxDateTimeNeutral(neutral); }
    00087
    00088 private:
    00089
    00090    static inline const char * format() { return "yyyyMMddhhmmss"; }
    00091
    00092    void update()
    00093    {
    00094       if (m_neutral.isEmpty() && ! m_dt.isValid()) { return; }
    00095       else if (m_dt.isValid()) { m_neutral = m_dt.toString(format()); }
    00096       else { qAssert(m_neutral.size() == QString(format()).size()); m_dt = QDateTime::fromString(m_neutral, format()); qAssert(m_dt.isValid()); }
    00097    }
    00098
    00099    template <class Archive>
    00100    void serialize(Archive & ar, const unsigned int file_version)
    00101    {
    00102       Q_UNUSED(file_version);
    00103       ar & boost::serialization::make_nvp("dt_neutral", m_neutral);
    00104       if (Archive::is_loading::value) { m_dt = QDateTime(); update(); }
    00105    }
    00106
    00107 };
    00108
    00109 } // namespace qx
    00110
    00111 QX_REGISTER_CLASS_NAME(qx::QxDateTimeNeutral)
    00112
    00113 #endif // _QX_DATE_TIME_NEUTRAL_H_