Advertisement
Guest User

Untitled

a guest
Jul 20th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. // Some definitions for calculation
  4. #define SEC_PER_MIN 60UL
  5. #define SEC_PER_HOUR 3600UL
  6. #define SEC_PER_DAY 86400UL
  7. #define SEC_PER_YEAR (SEC_PER_DAY*365)
  8.  
  9. // extracts 1..4 characters from a string and interprets it as a decimal value
  10. #define CONV_STR2DEC_1(str, i) (str[i]>'0'?str[i]-'0':0)
  11. #define CONV_STR2DEC_2(str, i) (CONV_STR2DEC_1(str, i)*10 + str[i+1]-'0')
  12. #define CONV_STR2DEC_3(str, i) (CONV_STR2DEC_2(str, i)*10 + str[i+2]-'0')
  13. #define CONV_STR2DEC_4(str, i) (CONV_STR2DEC_3(str, i)*10 + str[i+3]-'0')
  14.  
  15. // Custom "glue logic" to convert the month name to a usable number
  16. #define GET_MONTH(str, i) (str[i]=='J' && str[i+1]=='a' && str[i+2]=='n' ? 1 : \
  17. str[i]=='F' && str[i+1]=='e' && str[i+2]=='b' ? 2 : \
  18. str[i]=='M' && str[i+1]=='a' && str[i+2]=='r' ? 3 : \
  19. str[i]=='A' && str[i+1]=='p' && str[i+2]=='r' ? 4 : \
  20. str[i]=='M' && str[i+1]=='a' && str[i+2]=='y' ? 5 : \
  21. str[i]=='J' && str[i+1]=='u' && str[i+2]=='n' ? 6 : \
  22. str[i]=='J' && str[i+1]=='u' && str[i+2]=='l' ? 7 : \
  23. str[i]=='A' && str[i+1]=='u' && str[i+2]=='g' ? 8 : \
  24. str[i]=='S' && str[i+1]=='e' && str[i+2]=='p' ? 9 : \
  25. str[i]=='O' && str[i+1]=='c' && str[i+2]=='t' ? 10 : \
  26. str[i]=='N' && str[i+1]=='o' && str[i+2]=='v' ? 11 : \
  27. str[i]=='D' && str[i+1]=='e' && str[i+2]=='c' ? 12 : 0)
  28.  
  29. // extract the information from the time string given by __TIME__ and __DATE__
  30. #define __TIME_SECONDS__ CONV_STR2DEC_2(__TIME__, 6)
  31. #define __TIME_MINUTES__ CONV_STR2DEC_2(__TIME__, 3)
  32. #define __TIME_HOURS__ CONV_STR2DEC_2(__TIME__, 0)
  33. #define __TIME_DAYS__ CONV_STR2DEC_2(__DATE__, 4)
  34. #define __TIME_MONTH__ GET_MONTH(__DATE__, 0)
  35. #define __TIME_YEARS__ CONV_STR2DEC_4(__DATE__, 7)
  36.  
  37. // Days in February
  38. #define _UNIX_TIMESTAMP_FDAY(year) \
  39. (((year) % 400) == 0UL ? 29UL : \
  40. (((year) % 100) == 0UL ? 28UL : \
  41. (((year) % 4) == 0UL ? 29UL : \
  42. 28UL)))
  43.  
  44. // Days in the year
  45. #define _UNIX_TIMESTAMP_YDAY(year, month, day) \
  46. ( \
  47. /* January */ day \
  48. /* February */ + (month >= 2 ? 31UL : 0UL) \
  49. /* March */ + (month >= 3 ? _UNIX_TIMESTAMP_FDAY(year) : 0UL) \
  50. /* April */ + (month >= 4 ? 31UL : 0UL) \
  51. /* May */ + (month >= 5 ? 30UL : 0UL) \
  52. /* June */ + (month >= 6 ? 31UL : 0UL) \
  53. /* July */ + (month >= 7 ? 30UL : 0UL) \
  54. /* August */ + (month >= 8 ? 31UL : 0UL) \
  55. /* September */+ (month >= 9 ? 31UL : 0UL) \
  56. /* October */ + (month >= 10 ? 30UL : 0UL) \
  57. /* November */ + (month >= 11 ? 31UL : 0UL) \
  58. /* December */ + (month >= 12 ? 30UL : 0UL) \
  59. )
  60.  
  61. // get the UNIX timestamp from a digits representation
  62. #define _UNIX_TIMESTAMP(year, month, day, hour, minute, second) \
  63. ( /* time */ second \
  64. + minute * SEC_PER_MIN \
  65. + hour * SEC_PER_HOUR \
  66. + /* year day (month + day) */ (_UNIX_TIMESTAMP_YDAY(year, month, day) - 1) * SEC_PER_DAY \
  67. + /* year */ (year - 1970UL) * SEC_PER_YEAR \
  68. + ((year - 1969UL) / 4UL) * SEC_PER_DAY \
  69. - ((year - 1901UL) / 100UL) * SEC_PER_DAY \
  70. + ((year - 1601UL) / 400UL) * SEC_PER_DAY \
  71. )
  72.  
  73. // the UNIX timestamp
  74. #define UNIX_TIMESTAMP (_UNIX_TIMESTAMP(__TIME_YEARS__, __TIME_MONTH__, __TIME_DAYS__, __TIME_HOURS__, __TIME_MINUTES__, __TIME_SECONDS__))
  75.  
  76. // Current UTC day of week--0 is Sunday
  77. #define DAY_OF_WEEK ((UNIX_TIMESTAMP / 86400) + 4) % 7
  78.  
  79. // Type your code here, or load an example.
  80. int main(int argc, char **argv) {
  81. static_assert(DAY_OF_WEEK != 1);
  82. printf("It is not Monday\n");
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement