date variable

Posted on 2004-08-11
Last Modified: 2010-04-01

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.


Question by:KazIT
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 11

Expert Comment

ID: 11781448
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

LVL 11

Expert Comment

ID: 11781841
to save some memory you could set your variables to:

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

Expert Comment

ID: 11781862
or even

struct date{
   unsigned char day;
   unsigned char month;
   unsigned short year;
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 39

Expert Comment

ID: 11782700
I prefer to have an integer date yyyymmdd as data member like that:

class Date
     int date;
     Date(const string& strDate) : date(0)
          int d, m, y;
          istringstream istr(strDate);
          istr >> d; if ( return;
          istr >> m; if ( return;    
          istr >> y; if ( 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) )
          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

LVL 86

Expert Comment

ID: 11784236
>>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'.
LVL 39

Expert Comment

ID: 11784338
>> 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
LVL 86

Expert Comment

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

Accepted Solution

itsmeandnobodyelse earned 500 total points
ID: 11784422
>> The date is included

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

>> Since all POSIX conformant systems

All younger than me ...

Regards, Alex


Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

724 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