c++ date non standard format issues

Alright this is driving me nuts. I am trying to use the system date of users' computer to create a free trial version. After 7 days from the initial run, they will get a message saying their trial version has expired.

I have been using several methods to get system date. But all seem to give results in inconsistant formats. June will be spelled out in full for instance, but April will be abreviated as Apr. What gives?

Does that make any sense to any of you?

To make matters worse I can find no specs online that gives me the lowdown on what to expect for a certain date, and since I'm using a shared computer at a library I can not change the system date on the computer in order to test various dates.

Today after having compiled my program anew, right away I get a message that the installer has expired, presumably because today is the first of the month and whatever method it is I'm using to get system date has supplied the date in a different format than it did for june (for instance sometimes the formatting differes by having 1 or 2 spaces between month & year, or in numeric format giving january as 01, but february as 2 (without the 0) etc, and this is for the very same method!!!)

I need to be assured that everything is in order, and I can not go on rewriting code so that it can accept date in any format. What comes next? French?

Please help.

Last edited on
P. S. I am also using preprocessor macros __DATE__ etc.
To be honest with you, I don't know which one is causing me the greater headache.
Can you show any code? How about the time function.

http://cplusplus.com/reference/clibrary/ctime/time/
wtf wrote:
What comes next? French?

Yes, followed by ancient Egyptian hieroglyphics.

Getting serious, binarybob's answer is probably your best bet.
date.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#ifndef DATE_H
 #define DATE_H
 #include <iostream>
 #include <string>
 #include <cstdlib>
 using std::ostream;

 enum months {January = 1, February, March, April, May, June, July, August, September, October, November, December};

 class Date
{
   friend ostream &operator<<( ostream &, const Date & );
   public:
   Date(const Date & d);
   Date();
   //Date( int m = 1, int d = 1, int y = 1900 ); // default constructor
   Date(int m, int d, int y);
   Date(string s, bool post);
   //Date( string m = "January", int d = 1, int y = 1900);
   void setDate( int, int, int ); // set month, day, year
   void setMonth(int);
   void setDay(int);
   void setYear(int);
   int getdate(int & m, int & d, int & y);
   Date & operator--();
   Date operator--( int );
   Date &operator++(); // prefix increment operator
   Date operator++( int ); // postfix increment operator
   const Date &operator+=( int ); // add days, modify object
   const bool operator == (const Date & rhs);
   const bool operator != (const Date & rhs);
   const bool operator < (const Date & rhs);
   const bool operator > (const Date & rhs);
   const bool operator <= (const Date & rhs);
   const bool operator >= (const Date & rhs);

//   Date krypted(const string & k);
   //string krypted(const string & k);


   bool leapYear( int ) const; // is date in a leap year?
   bool endOfMonth( int ) const; // is date at the end of month?
   void addyear(), addmonth();

   int getyear(), getmonth(), getday();

   private:
   int month;
   int day;
   int year;

   static const int days[]; // array of days per month
   void helpIncrement(); // utility function for incrementing date
   void helpdencrement();
   int lastdayofmonth();

}; // end class Date



bool convert(char * c, int & m, int & d, int & y)
{
    char dateStr [9];
    //_strdate( dateStr);
    //dateStr = c;
    //char tempdate[7] = {dateStr[0], dateStr[1], dateStr[3], dateStr[4], dateStr[6], dateStr[7] };
    char tempdate[7] = {c[0], c[1], c[2], c[3], c[4], c[5], c[6] };
    int DD[6]; int D = atoi(tempdate);
    for (int i = 0; i < 6; ++i)
    DD[i] = int(tempdate[i]) - 48;
    y = 10 * DD[4] + DD[5];
    m = 10 * DD[0] + DD[1];
    d = 10 * DD[2] + DD[3];
    y += 2000;
    return D;
}

#endif
#include "DATE.cpp" 
date.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <string>
#ifndef DATE2_CPP
#define DATE2_CPP
#include <iostream>
#include "Date.h"
// initialize static member at file scope; one classwide copy
const int Date::days[] =
    { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
Date::Date(const Date & d)
{
    month = d.month;
    day = d.day;
    year = d.year;
}
Date::Date(){
    }
Date::Date( int m, int d, int y )
   {
      setDate( m, d, y );
   } // end Date constructor

Date::Date(string s, bool post)
{
    month = 1; year = 1900; day = 1;
    static int count;
    ++count;

    int d = 0, y = 0;
    string ms = "", ds = "", ys = "";
    char null = ' ';
    char t = '2';
    int c = 0;
    do{
        t = s[c];
        ms += (t != '\n') ? t : null;
      }while(!isspace(t) && ++c); c++; if (post) c++; //debugging

    do{
        t = s[c];
        ds += (t != '\n') ? t : null;
      }while(!isspace(t) && ++c); c++;

    do{
        t = s[c];
        ys += (t != '\n') ? t : null;
      }while(!isspace(t) && !cin.fail() && ++c);

    string temp;
    for (int i = 0; i < ms.length() - 1; ++i)
        temp += ms[i];
    ms = temp;
    temp = "" ;
    for (int j = 0; j < ds.length() - 1; ++j)
        {if (isdigit(ds[j]))
         temp += ds[j];
        }
    ds = temp;
    temp = ""; //
    for (int k = 0; k < 4; ++k)
        temp += ys[k];
    ys = temp;
    temp = "";

    if (ms == "January" || ms == "1" || ms == "JAN" || ms == "Jan" || ms == "JANUARY") setMonth(January); else if (ms == "February" || ms == "Feb" || ms == "FEB" || ms == "FEBRUARY" || ms == "2") setMonth(February); else if (ms == "March" || ms == "3" || ms == "Mar" || ms == "MAR" || ms == "MARCH") setMonth(March); else if (ms == "April" || ms == "Apr" || ms == "4") setMonth(April);
    else if (ms == "May" || ms == "5" || ms == "MAY") setMonth(May); else if (ms == "June" || ms == "6" || ms == "JUN" || ms == "Jun" || ms == "JUNE") setMonth(June); else if (ms == "July" || ms == "7" || ms == "JULY" || ms == "JUL" || ms == "Jul") setMonth(July);
    else if (ms == "August" || ms == "8" || ms == "AUGUST" || ms == "AUG" || ms == "Aug") setMonth(August); else if (ms == "September" || ms == "9" || ms == "SEPTEMBER" || ms == "Sept" || ms == "Sept" || ms == "Sep" || ms == "SEP") setMonth(September);
    else if (ms == "October" || ms == "10" || ms == "OCTOBER" || ms == "OCT" || ms == "Oct" ) setMonth(October); else if (ms == "November" || ms == "11" || ms == "Nov" || ms == "NOVEMBER" || ms == "NOV") setMonth(November); else if (ms == "December" || ms == "12" || ms == "Dec" || ms == "DEC" || ms == "DECEMBER") setMonth(December);

    if (ds == "1") setDay(1); else if (ds == "2") setDay(2); else if (ds == "3") setDay(3); else if (ds == "4") setDay(4); else if (ds == "5") setDay(5); else if (ds == "6") setDay(6);
    else if (ds == "7") setDay(7); else if (ds == "8") setDay(8); else if (ds == "9") setDay(9); else if (ds == "10") setDay(10); else if (ds == "11") setDay(11); else if (ds == "12") setDay(12);
    else if (ds == "13") setDay(13); else if (ds == "14") setDay(14); else if (ds == "15") setDay(15); else if (ds == "16") setDay(16); else if (ds == "17") setDay(17); else if (ds == "18") setDay(18);
    else if (ds == "19") setDay(19); else if (ds == "20") setDay(20); else if (ds == "21") setDay(21); else if (ds == "22") setDay(22); else if (ds == "23") setDay(23); else if (ds == "24") setDay(24); else if (ds == "25") setDay(25); else if (ds == "26") setDay(26);
    else if (ds == "27") setDay(27); else if (ds == "28") setDay(28); else if (ds == "29") setDay(29); else if (ds == "30") setDay(30); else if (ds == "31") setDay(31);
    else setDay(1);

    for (int l = 0, m = 1000; l < ys.length(); ++l, m /= 10)
        {
            y += m * (ys[l] - 48);
        }
    setYear(y);
}

void Date::setDate( int mm, int dd, int yy )
   {

      month = ( mm >= 1 && mm <= 12 ) ? mm : 1;
      year = ( yy >= 1900 && yy <= 2100 ) ? yy : 1900;

      // test for a leap year
      if ( month == 2 && leapYear( year ) )
         day = ( dd >= 1 && dd <= 29 ) ? dd : 1;
      else
         day = ( dd >= 1 && dd <= days[ month ] ) ? dd : 1;
   } // end function setDate

void Date::setMonth(int m)
    {
        month = (m >= 1 && m <= 12 ) ? m : 1;
    }
date.cpp continued
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
void Date::setDay(int d)
    {
        if ( month == 2 && leapYear( year ) )
            day = ( d >= 1 && d <= 29 ) ? d : 1;
        else day = ( d >= 1 && d <= days[ month ] ) ? d : 1;
        return;
    }

void Date::setYear(int y)
    {
        year = ( y >= 1900 && y <= 2100 ) ? y : 1900;
    }

Date & Date::operator--(){ helpdencrement(); return *this;}
Date Date::operator--(int){Date temp = *this; helpdencrement(); return temp; }

   // overloaded prefix increment operator
Date &Date::operator++()
   {
      helpIncrement(); // increment date
      return *this; // reference return to create an lvalue
   } // end function operator++

   // overloaded postfix increment operator; note that the
   // dummy integer parameter does not have a parameter name
Date Date::operator++( int )
   {
      Date temp = *this; // hold current state of object
      helpIncrement();

      // return unincremented, saved, temporary object
      return temp; // value return; not a reference return
   } // end function operator++

   // add specified number of days to date
const Date &Date::operator+=( int additionalDays )
   {
      for ( int i = 0; i < additionalDays; i++ )
         helpIncrement();

      return *this; // enables cascading
   } // end function operator+=

   // if the year is a leap year, return true; otherwise, return false
bool Date::leapYear( int testYear ) const
   {
      if ( testYear % 400 == 0 ||
         ( testYear % 100 != 0 && testYear % 4 == 0 ) )
        return true; // a leap year
      else
        return false; // not a leap year
   } // end function leapYear

   // determine whether the day is the last day of the month
bool Date::endOfMonth( int testDay ) const
   {
      if ( month == 2 && leapYear( year ) )
         return testDay == 29; // last day of Feb. in leap year
      else
         return testDay == days[ month ];
   } // end function endOfMonth

   // function to help increment the date
void Date::helpIncrement()
   {
      // day is not end of month
      if ( !endOfMonth( day ) )
         day++; // increment day
      else
         if ( month < 12 ) // day is end of month and month < 12
         {
            month++; // increment month
            day = 1; // first day of new month
         } // end if
         else // last day of year
         {
            year++; // increment year
            month = 1; // first month of new year
            day = 1; // first day of new month
         } // end else
   } // end function helpIncrement

void Date::helpdencrement()
{
    //if ( !beginofMonth( day ) )
    if (day != 1)
        day--;
    else
        if (month > 1)
            {
                month--;
                day = lastdayofmonth();
            }
        else
            {
                year--;
                month = 12;
                day = lastdayofmonth();
            }
}
date.cpp part 3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
int Date::lastdayofmonth()
{
    switch(month)
        {
            case January: return 31; break; case February: if (leapYear(year)) return 29; else return 28; break; case March: return 31; break; case April: return 30; break;
            case May: return 31; break; case June: return 30; break; case July: return 31; break; case August: return 31; break; case September: return 30; break; case October: return 31; break;
            case November: return 30; break; case December: return 31; break; default: return 0; break;
        }
}

void Date::addmonth()
{
    ++month;
}

void Date::addyear()
{
    ++year;
}
   // overloaded output operator
ostream &operator<<( ostream &output, const Date &d )
   {

       char msg[] = "", msg1[] = "January", msg2[] = "February", msg3[] = "March", msg4[] = "April", msg5[] = "May", msg6[] = "June",
       msg7[] = "July", msg8[] = "August", msg9[] = "September", msg10[] = "October", msg11[] = "November", msg12[] = "December";

       //static char *monthName[13] = {msg, msg1, msg2, msg3, msg4, msg5, msg6, msg7, msg8, msg9, msg10, msg11, msg12};
       string monthName[13] = {msg, msg1, msg2, msg3, msg4, msg5, msg6, msg7, msg8, msg9, msg10, msg11, msg12};

     output << monthName[ d.month ] << ' ' << d.day << ", " << d.year;
     return output; // enables cascading
  } // end function operator<<


const bool Date::operator == (const Date & rhs)
{
    if (month == rhs.month && day == rhs.day && year == rhs.year)
        return true;
    else return false;
}

const bool Date::operator !=(const Date & rhs)
{
    if ((*this == rhs))
        return false;
    else return true;
}

const bool Date::operator < (const Date & rhs)
{
    if (year >= rhs.year)
        return false;
    if (month >= rhs.month)
        return false;
    if (day < rhs.day)
        return true;
}

const bool Date::operator > (const Date & rhs)
{
    if (year > rhs.year)
        return true;
    if (year < rhs.year)
        return false;
    if (month > rhs.month)
        return true;
    if (month < rhs.month)
        return false;
    if (day > rhs.day)
        return true;
    else return false;
}

const bool Date::operator <= (const Date & rhs)
{
    if (year >= rhs.year)
        return false;
    if (month >= rhs.month)
        return false;
    if (day <= rhs.day)
        return true;
}

const bool Date::operator >= (const Date & rhs)
{
    if (year <= rhs.year)
        return false;
    if (month <= rhs.month)
        return false;
    if (day >= rhs.day)
        return true;

}

int Date::getdate(int & m, int & d, int & y)
{
    char dateStr [9];
    _strdate( dateStr); char tempdate[7] = {dateStr[0], dateStr[1], dateStr[3], dateStr[4], dateStr[6], dateStr[7] };
    int DD[6]; int D = atoi(tempdate);
    for (int i = 0; i < 6; ++i)
    DD[i] = int(tempdate[i]) - 48;
    year = 10 * DD[4] + DD[5];
    month = 10 * DD[0] + DD[1];
    day = 10 * DD[2] + DD[3];
    year += 2000;
    m = month; d = day; y = year;
    return D;
}

int Date::getday()
{
    return day;
}

int Date::getmonth()
{
    return month;
}

int Date::getyear()
{
    return year;
}
#endif 
I think the date class is fine, the problem however is the methods that I'm using to get the system date and compiler date.

for instance:
 
Date compiledate(__DATE__, false);


__DATE__ is consistently providing the date in different formats, so nearly every month I find myself needing to rewrite the date class to be able to accept the new format.

This can not possibly be acceptable to anyone.

Here is more relevant code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
SYSTEMTIME st;
bool setsystemtime(){ GetSystemTime(&st); return 1; } // cout << st.wYear << "\n" << st.wMonth << "\n" << st.wDay << endl; return 1; }
bool setedthesystemtime = setsystemtime();

char dateStr [8], timeStr[8], DateStr [9];
//int x(){exit(11);}; int X = x();
bool datesetter(){ _strdate(DateStr); //cout << "!!!DateStr123 = " << DateStr << endl;
                return true;}
bool setdate = datesetter();
int cc(){cout << "DateStr = " << DateStr << endl; }
//int CC = cc();
string DateStrString = makestring(DateStr, true);
//int doubt(){ cout << "DateStr = " << DateStr <<  endl; cout << "DateStrString = " << DateStrString << endl; exit(36); }
//bool whatthefuck = doubt(); 


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
string makestring(char a[], bool alph)
{
    if (isdigit(a[0]))
        {
            string ts = "";
            char space = ' ', two = '2', zero = '0';
            ts += a[0]; ts += a[1]; ts += space; ts += a[3]; ts += a[4];
               ts += space; ts += two; ts += zero; ts += a[6]; ts += a[7];
            return ts;
        }

    {
        char t = a[0], space = ' ', two = '2', zero = '0';
        int i = 0;
        string ts = "";
        while (isspace(a[i]))
            {
                ++i;
            }

        while (isalpha(a[i]))
            {
                ts += a[i];
                ++i;
            }
        ts += space;
        while (isspace(a[i]))
            {
                ++i;
            }
        while(isdigit(a[i]))
            {
                ts += a[i];
                ++i;
            }
        while(i < 1000)
            {
                if (isdigit(a[i])) ts += a[i];
                if (a[i] == ',') {ts += ','; ts += space; }
                ++i;
            }
        return ts;
    }
}
bumb
I just happened to think, could the problem be occurring in the library function atoi()?

Is the atoi function standard?

Thanks again.
Is the atoi function standard?
Yes, it is.

You can use ctime to get current date:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <ctime>
#include <iostream>
using namespace std;
int main()
{
   time_t t=time(NULL);
   tm *timeinfo;

   timeinfo=localtime(&t);

   cout <<"Year: "<<timeinfo->tm_year+1900<<endl
        <<"Month:  "<<timeinfo->tm_mon+1<<endl
        <<"Day of the month:  "<<timeinfo->tm_mday<<endl;
}


Regarding __DATE__, the standard format is "Mmm dd yyyy", so this code should work:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include <sstream>
using namespace std;

int month, day, year;
const char *months[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug","Sep", "Oct", "Nov", "Dec"};

void getdate(void)
{
  stringstream ss(__DATE__);

  string str_month;
  ss >> str_month;
  ss >>day;
  ss >>year;

  for (int i = 0; i < 12; i++)
    {
      if (str_month==months[i])
        {
          month = i + 1;
          return;
        }
    }
}

int main()
{
  getdate();
  cout <<"Compilation date: "<<endl;
  
  cout  <<"Year:  "<<year<<endl
        <<"Month: "<<month<<endl
        <<"Day:   "<<day<<endl;

}
Topic archived. No new replies allowed.