toytime.hpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*-----------------------------------------------------------------------------+
  2. Copyright (c) 2007-2009: Joachim Faulhaber
  3. +------------------------------------------------------------------------------+
  4. Copyright (c) 1999-2006: Cortex Software GmbH, Kantstrasse 57, Berlin
  5. +------------------------------------------------------------------------------+
  6. Distributed under the Boost Software License, Version 1.0.
  7. (See accompanying file LICENCE.txt or copy at
  8. http://www.boost.org/LICENSE_1_0.txt)
  9. +-----------------------------------------------------------------------------*/
  10. #include <boost/config.hpp>
  11. #ifdef BOOST_MSVC
  12. #pragma warning(push)
  13. #pragma warning(disable:4996) // This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
  14. #endif
  15. namespace boost{namespace icl
  16. {
  17. /** Time is a toy-class to demonstrate a class that conforms the requirements of
  18. a template parameter for class template icl::interval.
  19. In real world applications you may want to use the integer representation of a
  20. time variable. That way intervals and their containers are working most efficiently.
  21. */
  22. enum {sunday=0, monday, tuesday, wednesday, thursday, friday, saturday};
  23. static const char* daynames[] = {"sun", "mon", "tue", "wed", "thu", "fri", "sat"};
  24. class Time
  25. {
  26. public:
  27. Time(): m_time(0) {}
  28. Time(int hours, int minutes): m_time(60*hours+minutes) {}
  29. Time(int day, int hours, int minutes): m_time((24*60)*day+60*hours+minutes) {}
  30. int getDay()const { return m_time/(24*60); }
  31. int getHours()const { return (m_time%(24*60))/60; }
  32. int getMinutes()const { return (m_time%(24*60))%60; }
  33. int asInt()const { return m_time; }
  34. std::string getDayString()const { return daynames[getDay()]; }
  35. std::string as_string()const
  36. {
  37. const int MAX_TIMESTING_LEN = 256;
  38. char repr[MAX_TIMESTING_LEN];
  39. sprintf(repr, "%3s:%02d:%02d", getDayString().c_str(), getHours(), getMinutes());
  40. return std::string(repr);
  41. }
  42. Time& operator ++ () { m_time++; return *this; }
  43. Time& operator -- () { m_time--; return *this; }
  44. private:
  45. int m_time;
  46. };
  47. bool operator < (const Time& x1, const Time& x2) { return x1.asInt() < x2.asInt(); }
  48. bool operator == (const Time& x1, const Time& x2) { return x1.asInt() == x2.asInt(); }
  49. bool operator <= (const Time& x1, const Time& x2) { return x1.asInt() <= x2.asInt(); }
  50. template<class CharType, class CharTraits>
  51. std::basic_ostream<CharType, CharTraits> &operator<<
  52. (std::basic_ostream<CharType, CharTraits> &stream, Time const& value)
  53. {
  54. return stream << value.as_string();
  55. }
  56. }} // namespace icl boost
  57. #ifdef BOOST_MSVC
  58. #pragma warning(pop)
  59. #endif