QxTimeNeutral.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_TIME_NEUTRAL_H_
  32. #define _QX_TIME_NEUTRAL_H_
  33. #ifdef _MSC_VER
  34. #pragma once
  35. #endif
  36. /*!
  37. * \file QxTimeNeutral.h
  38. * \author Lionel Marty
  39. * \ingroup QxDao
  40. * \brief Helper class to store a time value into database under neutral format (HHMMSS) => 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 QxTimeNeutral;
  52. } // namespace qx
  53. QX_DLL_EXPORT QDataStream & operator<< (QDataStream & stream, const qx::QxTimeNeutral & t) QX_USED;
  54. QX_DLL_EXPORT QDataStream & operator>> (QDataStream & stream, qx::QxTimeNeutral & t) QX_USED;
  55. namespace qx {
  56. /*!
  57. * \ingroup QxDao
  58. * \brief qx::QxTimeNeutral : helper class to store a time value into database under neutral format (HHMMSS) => cross database compatibility
  59. */
  60. class QxTimeNeutral
  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::QxTimeNeutral & t);
  66. friend QX_DLL_EXPORT QDataStream & ::operator>> (QDataStream & stream, qx::QxTimeNeutral & t);
  67. private:
  68. QTime m_time; //!< Data value under QTime format from Qt library
  69. QString m_neutral; //!< Data value under neutral format 'hhmmss'
  70. public:
  71. QxTimeNeutral() { ; }
  72. explicit QxTimeNeutral(const QTime & time) : m_time(time) { update(); }
  73. explicit QxTimeNeutral(const QString & neutral) : m_neutral(neutral) { update(); }
  74. virtual ~QxTimeNeutral() { ; }
  75. inline QTime toTime() const { return m_time; }
  76. inline QString toNeutral() const { return m_neutral; }
  77. inline bool isValid() const { return m_time.isValid(); }
  78. inline void setTime(const QTime & time) { m_neutral = ""; m_time = time; update(); }
  79. inline void setNeutral(const QString & neutral) { m_time = QTime(); m_neutral = neutral; update(); }
  80. static QxTimeNeutral fromTime(const QTime & time) { return QxTimeNeutral(time); }
  81. static QxTimeNeutral fromNeutral(const QString & neutral) { return QxTimeNeutral(neutral); }
  82. private:
  83. static inline const char * format() { return "hhmmss"; }
  84. void update()
  85. {
  86. if (m_neutral.isEmpty() && ! m_time.isValid()) { return; }
  87. else if (m_time.isValid()) { m_neutral = m_time.toString(format()); }
  88. else { qAssert(m_neutral.size() == QString(format()).size()); m_time = QTime::fromString(m_neutral, format()); qAssert(m_time.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("time_neutral", m_neutral);
  96. if (Archive::is_loading::value) { m_time = QTime(); update(); }
  97. }
  98. #endif // _QX_ENABLE_BOOST_SERIALIZATION
  99. };
  100. } // namespace qx
  101. QX_REGISTER_CLASS_NAME(qx::QxTimeNeutral)
  102. #endif // _QX_TIME_NEUTRAL_H_