0

I'm trying to make a program wherein the user will input his/her birthday and the program will compute how many days, months, years, hours and minutes they've lived.

I've searched Google and I see there's a way to split the date into three to four parts.

I've copied this code and it seems to be working. It's just that I don't understand it. All the forums I've read don't help much either. Can anyone explain it to me?

time_t t = time(NULL);
tm* timePtr = localtime(&t);

cout << "seconds= " << timePtr->tm_sec << endl;
cout << "minutes = " << timePtr->tm_min << endl;
cout << "hours = " << timePtr->tm_hour << endl;
cout << "day of month = " << timePtr->tm_mday << endl;
cout << "month of year = " << timePtr->tm_mon << endl;
cout << "year = " << timePtr->tm_year + 1900 << endl;
cout << "weekday = " << timePtr->tm_wday << endl;
cout << "day of year = " << timePtr->tm_yday << endl;
cout << "daylight savings = " << timePtr->tm_isdst << endl;
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
user985206
  • 11
  • 1
  • 4
  • 5
    Which part don't you understand? – Oliver Charlesworth Mar 18 '12 at 20:35
  • You'll need to add one to the month to get the conventional month numbering (January as 1, but `tm_mon` uses 0 for January, etc), but you've already taken care of the year plus 1900. What else needs explaining? `localtime()` works with your program's current timezone; you'd use `gmtime()` to determine a time value w.r.t UTC (aka GMT). And timezones generally make life more complex. – Jonathan Leffler Mar 18 '12 at 20:35
  • 1
    There is not so much to understand in that code, the **localtime()** function does all the dirty work for you and you simply access the **tm** fields to read the results (**time_t** keeps the date in a compact form while **tm** splits the date in its components (seconds, minutes, hours, day, month, year and other useful informations). – Adriano Repetti Mar 18 '12 at 20:37

2 Answers2

0

This is not standard C++ code. This is (also) POSIX code.

(The recent C++11 standard gives you <chrono> but few compilers implement it; on Linux you'll need GCC 4.6 or 4.7; there is also <ctime>)

See this answer to a question very related to yours.

As for time, gettimeofday, localtime, strftime they have well written reference documentation in the manual page. What don't you understand?

(follow the links I gave you and read the linked pages carefully).

Community
  • 1
  • 1
Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
0

In most computing environments, dates and times are a unified concept. The C runtime library (hence also C++) provides the type time_t which measures time (and dates) as the number of seconds since 1970-01-01T00:00:00 UTC.

The localtime() function takes a time_t and converts that into the calendar-like fields which humans are accustomed to, according to the local timezone (which is obtained from the computer—the timezone can also be specified specifically). There is another very similar call, gmtime() which does not consider the local timezone, but always uses the UTC timezone, formerly called GMT.

To do what you want, accept from the user their birth date, birth time, and timezone, and convert that into a time_t. Then subtract that from the current time() value. The difference is the number of seconds they have been alive. To be friendly, use gmtime() to convert that to years, months, days, hours, minutes, and seconds.

wallyk
  • 56,922
  • 16
  • 83
  • 148