-
Date Class
I found this code that pretty much does exactly what I want. (minus forcing the change of a date) but I don't understand it to be able to produce a similar one.
Code:
#include <iostream>
#include <ctime>
using namespace std;
//Date class
class Date
{
public:
Date(int DDD, int YYYY); //constructor for DDD YYYY overloaded Dateone()
Date(int MM, int DD, int YY); //constructor for MM/DD/YY overloaded Datetwo)
Date(char data[20]);
private:
int day;
int month;
int year;
char mon;
};
Date::Date(int DDD, int YYYY)
{
day = DDD;
year = YYYY;
cout << day << " " << year << "\n";
}
Date::Date(int MM, int DD, int YY)
{
day = DD;
month = MM;
year = YY;
cout << month << "/" << day << "/" << year << "\n";
}
Date::Date(char data[20])
{
cout << data << "\n";
}
int main(void)
{
struct tm *t;
int day, mon, year;
char *month[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
char date[100];
time_t rawtime;
time ( &rawtime );
cout << ( "Current date and time are: ", ctime (&rawtime) );
t = localtime(&rawtime);
day = t->tm_mday;
mon = t->tm_mon + 1;
year = t->tm_year + 1900;
sprintf(date, "%s %d, %d", mon[mon - 1], day, year);
Date One(day, year);
Date Two(mon, day, year);
Date Three(date);
return 0;
}
I don't understand the purpose of this 'data' function tho it doesnt work without it.
Also its my first time using ctime so I dont understand the main much.. please correct me if I'm wrong
so from
Code:
time_t rawtime;
time ( &rawtime );
cout << ( "Current date and time are: ", ctime (&rawtime) );
t = localtime(&rawtime);
day = t->tm_mday;
mon = t->tm_mon + 1;
year = t->tm_year + 1900;
sprintf(date, "%s %d, %d", mon[mon - 1], day, year);
is all necessary to get the pc time and display it? if I just want the pc to know I would delete after the t= part cus the rest is basically just setting it up to print right?
I dont understand the purpose of the struct in the main nore the char date either..
currently my code i'm developing is
Code:
#include <iostream>
#include <ctime>
using namespace std;
class Date {
private:
int day;
int month;
int year;
public:
Date();
Date( int,int,int);
void FormatTime();
void print();
};
Date::Date( int m, int d, int yr )
{
if ( m > 0 && m <= 12 ) // validate the month (Jan-Dec)
month = m;
else {
month = 1;
cout << "Month " << m << " invalid. Set to month 1.\n";
}
if ( d > 0 && d <= 31 ) // validate the day (0-31)
day = d;
else {
day = 1;
cout << "day " << d << " invalid. Set to day 1.\n";
}
year = yr;
}
void Date::FormatTime()
{
//in hear change number into an actual month (1-january)
}
void Date::print()
{
cout << month<<"/"<<day<<"/"<<year <<endl; // mm/dd/year
cout << day <<", "<< year << endl; //dd year
}
int main()
{
}
I was doing it so that the user puts in the date but then I realized the question says "read about <ctime> and implement it. Which would make my code all useless cus I'm suppose to be using the pc time...
Any tips/help is appreciated
Thanks!
-
Eh, where to start...
OK, first off, the Date(char data[20]) constructor is incomplete it seems. My guess it that it takes in a date in string form (similar to the constructor that takes year, month and date).
As for the main function, here's a breakdown:
Code:
int main(void)
{
struct tm *t; // Struct to hold date
int day, mon, year;
char *month[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
char date[100];
time_t rawtime;
time ( &rawtime ); // Gets the current time and stores it in rawtime.
cout << ( "Current date and time are: ", ctime (&rawtime) );
t = localtime(&rawtime); // Converts the raw time to local time (time zone thingys) and returns a tm struct with the date.
// Takes the date and converts it to a "real" date.
// The tm struct begins counting years from 1900 and days are relative to 0, so the first day in a month is 0, not 1.
// Lastly prints the date.
day = t->tm_mday;
mon = t->tm_mon + 1;
year = t->tm_year + 1900;
// The -1 is to adjust the month value to an index for the month array and print the month name in that index.
sprintf(date, "%s %d, %d", mon[mon - 1], day, year);
// Construct date classes from the year.
Date One(day, year);
Date Two(mon, day, year);
Date Three(date);
return 0;
}
Also, the date is printed twice. The first two lines should get the date.
The second is a little more verbose and allows you to format it a little better, not to mention it's in a much easier format.
And also, this:
Date( int,int,int);
Don't remove the names there!
http://sourceforge.net/apps/mediawik...arameter_names
Boost also has a date class you can look into.
-
There is a very nice Date (or maybe Date/Time) class available for free on snippets.org...it is a "kitchen sink" class so you may want to trim it down for your purposes but I have used it in the past and found it quite nice...
-
Boost is known quality and portable, so that's where I would look first for reusable code.