'int air_date' '%'?

int air_date='20100103'; //2010 - Jan - 03

/* My goal here is to subtract a day. */

int day = air_date % 100; //?????? Is this right?

//Are there any functions time/date for this type of date format?

:cool:

Which language?

C++, g++ compiler for Linux.

Is there a reason you are encoding the date as the integer 20,100,103 instead of the number of seconds past the epoch or as a julian date? The reason I ask is that there are oodles of Date, Date/Time, and Julian Date classes out there for you to steal, er, utilize. But if you need the date to be in the aforementioned integer, you will have to manage how you add and subtract dates. Subtracting 1 will not work as intended:
20010102
20010101
20010099
20010098
Nor will adding 1:
20010227
20010228
20010229
20010230

---------- Post updated at 12:51 PM ---------- Previous update was at 12:48 PM ----------

If you want to extract the parts of the date, then:

int day   = air_date % 100;
int month = ( air_date / 100 ) % 100;
int year  = air_date / 10000;

The reason for me using that style of date is because that is what exists in my database. For the time being I wrote some code which I will post below. I wouldn't mind converting to a Julian style date or some other style if that will help me deal with things I just have not got there yet. Thanks for your reply:

string sub1day(string date)
{
  string day = date.substr(6,2);
  string month = date.substr(4,2);
  string year = date.substr(0,4);
  int iday = atoi(day.c_str());
  int imonth = atoi(month.c_str());
  int iyear = atoi(year.c_str());
  iday = iday -1;
  if(iday==0) {
    imonth = imonth -1;
    if(imonth == 0) {
      int iyear = iyear - 1;
      int imonth = 12;
      int iday = 31;
    }
    else if(imonth == 1 || imonth == 3 || imonth == 5 || imonth == 7 || imonth == 8 || imonth == 10 || imonth == 12)
    {
      iday = 31;
    }
    else if(imonth == 4 || imonth == 6 || imonth == 9 || imonth == 9 || imonth == 11)
    {
      iday = 30;
    }
    else if(imonth == 2)
    {
      iday = 28;
    }
  }
  stringstream sreturn;
  if(iday < 10 && imonth < 10) { sreturn << iyear << "0" << imonth << "0" << iday; }
  else if(imonth < 10) { sreturn << iyear << "0" << imonth << iday; }
  else if(iday < 10) { sreturn << iyear << imonth << "0" << iday; }
  return sreturn.str();
}

I do see one problem:

You need to account for leap-years:

    else if(imonth == 2) {
      if (iyear % 4) {
        iday = 28;
      }
       else {
        iday = 29;
      }
    }

Please keep in mind that this will only work properly if your dates occurs between the years 1901 and 2099. A more accurate implemention would be:

     else if(imonth == 2) {
      if (iyear % 400) {
        if (iyear % 100) {
          if (iyear % 4) {
            iday= 28;
          }
          else {
            iday = 29;
          }
        }
        else {
          iday= 28;
        }
      }
       else {
        iday = 29;
      }
    }
1 Like

Thanks a lot! I was wondering what a good implementation of leap year would be. You code is a huge help! :b:

Thinking about it for a moment, there are more non-leapyears, and more century non-leapyears, I would do:

  if (iyear % 4) {
    iday = 28;
  }
  else if (iyear % 100) {
    iday = 29;
  }
  else if (iyear % 400) {
    iday = 28;
  }
  else {
    iday = 29;
  }

This will limit the number of if statements executed for a given year.