QxOrm 1.4.9
C++ Object Relational Mapping library
Loading...
Searching...
No Matches
QxDateTimeNeutral.h
Go to the documentation of this file.
1/****************************************************************************
2**
3** https://www.qxorm.com/
4** Copyright (C) 2013 Lionel Marty (contact@qxorm.com)
5**
6** This file is part of the QxOrm library
7**
8** This software is provided 'as-is', without any express or implied
9** warranty. In no event will the authors be held liable for any
10** damages arising from the use of this software
11**
12** Commercial Usage
13** Licensees holding valid commercial QxOrm licenses may use this file in
14** accordance with the commercial license agreement provided with the
15** Software or, alternatively, in accordance with the terms contained in
16** a written agreement between you and Lionel Marty
17**
18** GNU General Public License Usage
19** Alternatively, this file may be used under the terms of the GNU
20** General Public License version 3.0 as published by the Free Software
21** Foundation and appearing in the file 'license.gpl3.txt' included in the
22** packaging of this file. Please review the following information to
23** ensure the GNU General Public License version 3.0 requirements will be
24** met : http://www.gnu.org/copyleft/gpl.html
25**
26** If you are unsure which license is appropriate for your use, or
27** if you have questions regarding the use of this file, please contact :
28** contact@qxorm.com
29**
30****************************************************************************/
31
32#ifndef _QX_DATE_TIME_NEUTRAL_H_
33#define _QX_DATE_TIME_NEUTRAL_H_
34
35#ifdef _MSC_VER
36#pragma once
37#endif
38
46#ifdef _QX_ENABLE_BOOST_SERIALIZATION
47#include <boost/serialization/serialization.hpp>
48#include <boost/serialization/nvp.hpp>
49#endif // _QX_ENABLE_BOOST_SERIALIZATION
50
51#include <QtCore/qdatetime.h>
52#include <QtCore/qdatastream.h>
53
55
57
58namespace qx {
59class QxDateTimeNeutral;
60} // namespace qx
61
62QX_DLL_EXPORT QDataStream & operator<< (QDataStream & stream, const qx::QxDateTimeNeutral & t) QX_USED;
63QX_DLL_EXPORT QDataStream & operator>> (QDataStream & stream, qx::QxDateTimeNeutral & t) QX_USED;
64
65namespace qx {
66
72{
73
74#ifdef _QX_ENABLE_BOOST_SERIALIZATION
76#endif // _QX_ENABLE_BOOST_SERIALIZATION
77
78 friend QX_DLL_EXPORT QDataStream & ::operator<< (QDataStream & stream, const qx::QxDateTimeNeutral & t);
79 friend QX_DLL_EXPORT QDataStream & ::operator>> (QDataStream & stream, qx::QxDateTimeNeutral & t);
80
81private:
82
83 QDateTime m_dt;
84 QString m_neutral;
85
86public:
87
89 explicit QxDateTimeNeutral(const QDateTime & dt) : m_dt(dt) { update(); }
90 explicit QxDateTimeNeutral(const QString & neutral) : m_neutral(neutral) { update(); }
91 virtual ~QxDateTimeNeutral() { ; }
92
93 inline QDateTime toDateTime() const { return m_dt; }
94 inline QString toNeutral() const { return m_neutral; }
95 inline bool isValid() const { return m_dt.isValid(); }
96
97 inline void setDateTime(const QDateTime & dt) { m_neutral = ""; m_dt = dt; update(); }
98 inline void setNeutral(const QString & neutral) { m_dt = QDateTime(); m_neutral = neutral; update(); }
99
100 static QxDateTimeNeutral fromDateTime(const QDateTime & dt) { return QxDateTimeNeutral(dt); }
101 static QxDateTimeNeutral fromNeutral(const QString & neutral) { return QxDateTimeNeutral(neutral); }
102
103private:
104
105 static inline const char * format() { return "yyyyMMddhhmmss"; }
106
107 void update()
108 {
109 if (m_neutral.isEmpty() && ! m_dt.isValid()) { return; }
110 else if (m_dt.isValid()) { m_neutral = m_dt.toString(format()); }
111 else { qAssert(m_neutral.size() == QString(format()).size()); m_dt = QDateTime::fromString(m_neutral, format()); qAssert(m_dt.isValid()); }
112 }
113
114#ifdef _QX_ENABLE_BOOST_SERIALIZATION
115 template <class Archive>
116 void serialize(Archive & ar, const unsigned int file_version)
117 {
118 Q_UNUSED(file_version);
119 ar & boost::serialization::make_nvp("dt_neutral", m_neutral);
120 if (Archive::is_loading::value) { m_dt = QDateTime(); update(); }
121 }
122#endif // _QX_ENABLE_BOOST_SERIALIZATION
123
124};
125
126} // namespace qx
127
129
130#endif // _QX_DATE_TIME_NEUTRAL_H_
QX_DLL_EXPORT QDataStream & operator<<(QDataStream &stream, const qx::QxDateTimeNeutral &t) QX_USED
QX_DLL_EXPORT QDataStream & operator>>(QDataStream &stream, qx::QxDateTimeNeutral &t) QX_USED
#define QX_USED
Definition QxMacro.h:244
#define qAssert(x)
Definition QxMacro.h:52
#define QX_DLL_EXPORT
Definition QxMacro.h:182
qx::QxDateTimeNeutral : helper class to store a date-time value into database under neutral format (Y...
static const char * format()
void setDateTime(const QDateTime &dt)
QString toNeutral() const
void setNeutral(const QString &neutral)
QDateTime m_dt
Data value under QDateTime format from Qt library.
QDateTime toDateTime() const
QxDateTimeNeutral(const QDateTime &dt)
friend class boost::serialization::access
static QxDateTimeNeutral fromDateTime(const QDateTime &dt)
QxDateTimeNeutral(const QString &neutral)
void serialize(Archive &ar, const unsigned int file_version)
QString m_neutral
Data value under neutral format 'yyyyMMddhhmmss'.
static QxDateTimeNeutral fromNeutral(const QString &neutral)
qx::trait::get_class_name<T>::get() : return class name of type T under const char * format,...
#define QX_REGISTER_CLASS_NAME(className)
Root namespace for all QxOrm library features.