QxDateNeutral.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. #ifndef _QX_DATE_NEUTRAL_H_
  32. #define _QX_DATE_NEUTRAL_H_
  33. #ifdef _MSC_VER
  34. #pragma once
  35. #endif
  36. /*!
  37. * \file QxDateNeutral.h
  38. * \author Lionel Marty
  39. * \ingroup QxDao
  40. * \brief Helper class to store a date value into database under neutral format (YYYYMMDD) => cross database compatibility
  41. */
  42. #ifdef _QX_ENABLE_BOOST_SERIALIZATION
  43. #include <boost/serialization/serialization.hpp>
  44. #include <boost/serialization/nvp.hpp>
  45. #endif // _QX_ENABLE_BOOST_SERIALIZATION
  46. #include <QtCore/qdatetime.h>
  47. #include <QtCore/qdatastream.h>
  48. #include <QxSerialize/Qt/QxSerialize_QString.h>
  49. #include <QxTraits/get_class_name.h>
  50. namespace qx {
  51. class QxDateNeutral;
  52. } // namespace qx
  53. QX_DLL_EXPORT QDataStream & operator<< (QDataStream & stream, const qx::QxDateNeutral & t) QX_USED;
  54. QX_DLL_EXPORT QDataStream & operator>> (QDataStream & stream, qx::QxDateNeutral & t) QX_USED;
  55. namespace qx {
  56. /*!
  57. * \ingroup QxDao
  58. * \brief qx::QxDateNeutral : helper class to store a date value into database under neutral format (YYYYMMDD) => cross database compatibility
  59. */
  60. class QxDateNeutral
  61. {
  62. #ifdef _QX_ENABLE_BOOST_SERIALIZATION
  63. friend class boost::serialization::access;
  64. #endif // _QX_ENABLE_BOOST_SERIALIZATION
  65. friend QX_DLL_EXPORT QDataStream & ::operator<< (QDataStream & stream, const qx::QxDateNeutral & t);
  66. friend QX_DLL_EXPORT QDataStream & ::operator>> (QDataStream & stream, qx::QxDateNeutral & t);
  67. private:
  68. QDate m_date; //!< Data value under QDate format from Qt library
  69. QString m_neutral; //!< Data value under neutral format 'yyyyMMdd'
  70. public:
  71. QxDateNeutral() { ; }
  72. explicit QxDateNeutral(const QDate & date) : m_date(date) { update(); }
  73. explicit QxDateNeutral(const QString & neutral) : m_neutral(neutral) { update(); }
  74. virtual ~QxDateNeutral() { ; }
  75. inline QDate toDate() const { return m_date; }
  76. inline QString toNeutral() const { return m_neutral; }
  77. inline bool isValid() const { return m_date.isValid(); }
  78. inline void setDate(const QDate & date) { m_neutral = ""; m_date = date; update(); }
  79. inline void setNeutral(const QString & neutral) { m_date = QDate(); m_neutral = neutral; update(); }
  80. static QxDateNeutral fromDate(const QDate & date) { return QxDateNeutral(date); }
  81. static QxDateNeutral fromNeutral(const QString & neutral) { return QxDateNeutral(neutral); }
  82. private:
  83. static inline const char * format() { return "yyyyMMdd"; }
  84. void update()
  85. {
  86. if (m_neutral.isEmpty() && ! m_date.isValid()) { return; }
  87. else if (m_date.isValid()) { m_neutral = m_date.toString(format()); }
  88. else { qAssert(m_neutral.size() == QString(format()).size()); m_date = QDate::fromString(m_neutral, format()); qAssert(m_date.isValid()); }
  89. }
  90. #ifdef _QX_ENABLE_BOOST_SERIALIZATION
  91. template <class Archive>
  92. void serialize(Archive & ar, const unsigned int file_version)
  93. {
  94. Q_UNUSED(file_version);
  95. ar & boost::serialization::make_nvp("date_neutral", m_neutral);
  96. if (Archive::is_loading::value) { m_date = QDate(); update(); }
  97. }
  98. #endif // _QX_ENABLE_BOOST_SERIALIZATION
  99. };
  100. } // namespace qx
  101. QX_REGISTER_CLASS_NAME(qx::QxDateNeutral)
  102. #endif // _QX_DATE_NEUTRAL_H_