CompileDateTime.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /**
  2. * Copyright (c) 2011-2022 Bill Greiman
  3. * This file is part of the SdFat library for SD memory cards.
  4. *
  5. * MIT License
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a
  8. * copy of this software and associated documentation files (the "Software"),
  9. * to deal in the Software without restriction, including without limitation
  10. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  11. * and/or sell copies of the Software, and to permit persons to whom the
  12. * Software is furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included
  15. * in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  18. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  22. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. #ifndef CompileDateTime_h
  26. #define CompileDateTime_h
  27. // Note - these functions will compile to a few bytes
  28. // since they are evaluated at compile time.
  29. /** \return year field of the __DATE__ macro. */
  30. constexpr uint16_t compileYear() {
  31. return 1000*(__DATE__[7] - '0')
  32. + 100*(__DATE__[8] - '0')
  33. + 10*(__DATE__[9] - '0')
  34. + (__DATE__[10] - '0');
  35. }
  36. /** \return true if str equals the month field of the __DATE__ macro. */
  37. constexpr bool compileMonthIs(const char* str) {
  38. return __DATE__[0] == str[0]
  39. && __DATE__[1] == str[1]
  40. && __DATE__[2] == str[2];
  41. }
  42. /** \return month field of the __DATE__ macro. */
  43. constexpr uint8_t compileMonth() {
  44. return compileMonthIs("Jan") ? 1 :
  45. compileMonthIs("Feb") ? 2 :
  46. compileMonthIs("Mar") ? 3 :
  47. compileMonthIs("Apr") ? 4 :
  48. compileMonthIs("May") ? 5 :
  49. compileMonthIs("Jun") ? 6 :
  50. compileMonthIs("Jul") ? 7 :
  51. compileMonthIs("Aug") ? 8 :
  52. compileMonthIs("Sep") ? 9 :
  53. compileMonthIs("Oct") ? 10 :
  54. compileMonthIs("Nov") ? 11 :
  55. compileMonthIs("Dec") ? 12 : 0;
  56. }
  57. /** \return day field of the __DATE__ macro. */
  58. constexpr uint8_t compileDay() {
  59. return 10*(__DATE__[4] == ' ' ? 0 : __DATE__[4] - '0') + (__DATE__[5] - '0');
  60. }
  61. /** \return hour field of the __TIME__ macro. */
  62. constexpr uint8_t compileHour() {
  63. return 10*(__TIME__[0] - '0') + __TIME__[1] - '0';
  64. }
  65. /** \return minute field of the __TIME__ macro. */
  66. constexpr uint8_t compileMinute() {
  67. return 10*(__TIME__[3] - '0') + __TIME__[4] - '0';
  68. }
  69. /** \return second field of the __TIME__ macro. */
  70. constexpr uint8_t compileSecond() {
  71. return 10*(__TIME__[6] - '0') + __TIME__[7] - '0';
  72. }
  73. #endif // CompileDateTime_h