Solved

date variable

Posted on 2004-08-11
8
207 Views
Last Modified: 2010-04-01
Hi,

I have a series of structs in a program.  One of the structs listed here
//Date struct
struct Date{
      int day;
      int month;
      int year;
};
requires 2 functions
fileInputDate- to read a date in the format dd/mm/yyyy from a text file and return a variable of type Date.
outputDate- to display a Date variable in an acceptable format.
I need to know what type of data is best to use for the date variable.
Thanks

Kaz


0
Comment
Question by:KazIT
8 Comments
 
LVL 11

Expert Comment

by:avizit
Comment Utility
guess your structure is good enough for date

though you can make them as unsigned ints.

as in
struct date{
     unsigned int day;
    unsigned int month;
     unsigned int year;
};


depending on your needs you may also decide to use enum data types for those






0
 
LVL 11

Expert Comment

by:Jase-Coder
Comment Utility
to save some memory you could set your variables to:

struct date{
   short unsigned int day;
   short unsigned int month;
   short  unsigned int year;
};
0
 
LVL 16

Expert Comment

by:nonubik
Comment Utility
or even

struct date{
   unsigned char day;
   unsigned char month;
   unsigned short year;
};
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
I prefer to have an integer date yyyymmdd as data member like that:

class Date
{
     int date;
public:
     Date(const string& strDate) : date(0)
     {
          int d, m, y;
          istringstream istr(strDate);
          istr >> d; if (istr.fail()) return;
          istr >> m; if (istr.fail()) return;    
          istr >> y; if (istr.fail()) return;  
          y = (y < 50)? 2000+y : (y < 100)? 1900 + y : y;
          if ((y < 1000 || y > 2200) ||
              (m < 1    || m > 12  ) ||
              (d < 1    || d > 31  ) ||
              (d == 31 &&
                (m == 4 || m == 6 || m == 9 || m == 11)
              )                      ||
              (m == 2 &&
                 (d > 28 + leap(y) )
              )
             )
          {
               return;
          }
          date = y*10000 + m*100 + d;
     }
     int leap(int y) { return ((y % 4) == 0 && ((y % 100) != 0 || (y % 400) == 0))? 1 : 0; }
     bool isValidDate() { return date != 0; }
     string getDateAsString()
     {   stringstream ss;
          ss << date % 100 << '/' << (date%10000) / 100 << '/' << date/10000;
          return ss.str();
     }
};

Regards, Alex

0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 86

Expert Comment

by:jkr
Comment Utility
>>I need to know what type of data is best to use for the date variable.

Use what the C/C++ runtime libraries use - a 'time_t', which is nothing but a 'long int'.
0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
Comment Utility
>> Use what the C/C++ runtime libraries use - a 'time_t'

time_t is the number of seconds elapsed since midnight (00:00:00), January 1, 1970, so i wouldn't recommend it for a Date class.

Regards, Alex
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Why not? The date is included. And, it only uses 4 bytes of space. Since all POSIX conformant systems rely on that...
0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 500 total points
Comment Utility
>> The date is included

But my birthday couldn't be stored with that ... ;-)

>> Since all POSIX conformant systems

All younger than me ...

Regards, Alex




0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now