Advertisement
ripred

get_date.cpp

Dec 2nd, 2023
734
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.84 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <stdint.h>
  3. using namespace std;
  4.  
  5. char compileDate[16] = __DATE__;
  6.  
  7. uint16_t const monPattern[12] = {
  8. //ascii added together
  9.     281,    // J+a+n = 74+97+110
  10.     269,    // F+e+b = 70+101+98
  11.     288,    // M+a+r = 77+97+114
  12.     291,    // A+p+r = 65+112+114
  13.     295,    // M+a+y = 77+97+121
  14.     301,    // J+u+n = 74+117+110
  15.     299,    // J+u+l = 74+117+108
  16.     285,    // A+u+g = 65+117+103
  17.     296,    // S+e+p = 83+101+112
  18.     294,    // O+c+t = 79+99+116
  19.     307,    // N+o+v = 78+111+118
  20.     268,    // D+e+c = 68+101+99
  21. };
  22.  
  23. //name of months
  24. const char* months[12] {
  25.     "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  26. };
  27.  
  28. //get compile month in integers
  29. int decideMonth(){
  30.     //get monPattern by adding ascii
  31.     int add = 0;
  32.     for(int i=0; i<3;i++){
  33.         add += compileDate[i];
  34.     }
  35.  
  36.     //what month?
  37.     int mon = 0;
  38.     for(int j=0; j<12; j++){
  39.         if(add==monPattern[j]){
  40.             mon = j+1;
  41.         }
  42.     }
  43.     return mon;
  44. }
  45. int month = decideMonth();
  46.  
  47. //get compile date in integers
  48. int decideDate(){
  49.     //get date, converting from ascii to decimal
  50.     int dt=0;
  51.     for (int i=4; i<6; i++){
  52.         if (compileDate[i] != ' '){
  53.             dt = dt*10 + (compileDate[i]-48);
  54.         }
  55.     }
  56.     return dt;
  57. }
  58. int date = decideDate();
  59.  
  60. //get compile year in integers
  61. int decideYear(){
  62.     //get year, converting from ascii to decimal
  63.     int yr = 0;
  64.     for (int i=7; i<11; i++){
  65.         yr = yr*10 + (compileDate[i]-48);
  66.     }
  67.     return yr;
  68. }
  69. int year = decideYear();
  70.  
  71. // number of days in a month
  72. int get_maxDays(int mon){
  73.     //30 day months
  74.     if (mon==4 || mon==6 || mon==9 || mon==11){
  75.         return 30;
  76.     }
  77.  
  78.     //special case on Feb
  79.     else if (mon==2){
  80.         if (year%4==0){
  81.             return 29;
  82.         }
  83.         else {
  84.             return 28;
  85.         }
  86.     }
  87.  
  88.     //31 day months
  89.     else {
  90.         return 31;
  91.     }
  92. }
  93. int maxDays = get_maxDays(month);
  94. const int maxMonth = 12;
  95.  
  96. //next month
  97. void checkMonth(){
  98.     if (date > maxDays){
  99.         date -= maxDays;                //1st new month
  100.         month++;                        //new month
  101.         maxDays = get_maxDays(month);   //new month's maxDays
  102.     }
  103. }
  104.  
  105. //next year
  106. void checkYear(){
  107.     if (month > maxMonth){
  108.         month -= maxMonth;  //Jan new year
  109.         year++;             //new year
  110.     }
  111. }
  112.  
  113. //when hit 00:00 next day, date++
  114. //checkMonth() and checkYear() every date++
  115.  
  116. //today (idk how to do this)
  117. string today(){
  118.     return string(months[month-1]) + " " + to_string(date) + " " + to_string(year);
  119. }
  120.  
  121. int main(){
  122.     cout << compileDate << endl;
  123.     cout << months[month-1] << ' ' << date << ' ' << year << endl; //i want the today function to output this thing
  124.     cout << today() << endl;
  125.     return 0;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement