String-TIMESTAMP-UnixTime.cmake 770 B

12345678910111213141516171819202122
  1. string(TIMESTAMP timestamp "[%Y-%m-%d %H:%M:%S] %s" UTC)
  2. string(TIMESTAMP unix_time "%s")
  3. string(TIMESTAMP year "%Y" UTC)
  4. string(TIMESTAMP days "%j" UTC)
  5. # Doing proper date calculations here to verify unix timestamps
  6. # could be error prone.
  7. # At the very least use some safe lower and upper bounds to
  8. # see if we are somewhere in the right region.
  9. math(EXPR years_since_epoch "${year} - 1970")
  10. math(EXPR lower_bound "((${years_since_epoch} * 365) + ${days} - 1) * 86400")
  11. math(EXPR upper_bound "((${years_since_epoch} * 366) + ${days}) * 86400")
  12. if(unix_time GREATER_EQUAL lower_bound AND unix_time LESS upper_bound)
  13. message("~${unix_time}~")
  14. else()
  15. message(FATAL_ERROR "${timestamp} unix time not in expected range [${lower_bound}, ${upper_bound})")
  16. endif()