date_time.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Date and time utils
  2. //
  3. // Platform: ISO C++ 98/11
  4. // $Id$
  5. //
  6. // (c) __vic 2011
  7. #ifndef __VIC_DATE_TIME_H
  8. #define __VIC_DATE_TIME_H
  9. #include<__vic/defs.h>
  10. #include<__vic/error.h>
  11. namespace __vic {
  12. //----------------------------------------------------------------------------
  13. __VIC_CONSTEXPR_FUNC bool is_leap_year(int year)
  14. {
  15. return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
  16. }
  17. //----------------------------------------------------------------------------
  18. // 1st arg: month 1..12 (0 will be returned otherwise)
  19. // 2nd arg: year is used only if mon == 2
  20. int days_in_month(int , int ) noexcept;
  21. // Returns the difference in days between the beginning
  22. // of the 2nd year and the beginning of the 1st year
  23. // Year can't be 0!!!
  24. long days_between_years(unsigned , unsigned );
  25. //////////////////////////////////////////////////////////////////////////////
  26. struct invalid_date : public exception
  27. {
  28. explicit invalid_date(const char *msg) : exception(msg) {}
  29. };
  30. //////////////////////////////////////////////////////////////////////////////
  31. // yy != 0, mm - 1..12, dd - 1..{28..31}, hh - 0..23, mi - 0..59, ss - 0..59
  32. void validate_date(int yy, int mm, int dd);
  33. void validate_time(int hh, int mi, int ss);
  34. inline void validate_date_time(int yy, int mm, int dd, int hh, int mi, int ss)
  35. {
  36. validate_date(yy, mm, dd);
  37. validate_time(hh, mi, ss);
  38. }
  39. } // namespace
  40. #endif // header guard