question about time in c++

Pages: 12
How do I find the current time (24 hour time) within my program code and set it as a variable without it printing the time so i can add that to an equation.
same thing with date but i don't want it to say something weird like Tue and then the date.
after this my program is complete
Last edited on
I'm not entirely sure how this function works so you'll have to check in the references but I believe you can find what you are looking for in <ctime>.
i looked and i could make the output a variable but it has the month and weekday in letters and that wouldn't work
looks like you did not look hard enoough ;)...
there are some functions to format the output...
struct tm?
No it's just because I don't use them so I'm not versed with them. And I was too lazy to look first.
what?
incubbus or any one what about setting the current time and date into a variable
i am really confused
Last edited on
I believe <ctime> defines a class for that purpose.
I looked and as far as I can understand it doesn't.
What does this mean?

"You can use the SYSTEMTIME structure, together with the GetSystemTime function to retrieve the current system time in UTC format."
I think this is Win 32 API. How would I use this.
Last edited on
It means that you aren't looking at ctime. I think you dismissed it a little prematurely.

Here's an example that changes the format:
http://www.cplusplus.com/reference/clibrary/ctime/strftime/
use the tm-class to get ur info!...

stolen from the ref:
1
2
3
4
5
6
7
8
9
10
int main ()
{
  time_t rawtime;
  struct tm * timeinfo;

  time ( &rawtime );
  timeinfo = localtime ( &rawtime );
  
  return 0;
}



now all you need is to access waht u wanted from the tm via e.g. timeinfo->tm_hour ,tm_sec or tm_minute <- all cute ints <3
Last edited on
maybe i wasn't clear... I want to set the time and date as integers. As in seconds would be an int, minutes would be an int, and hours would be an int.
Same thing with date, the day, year, and month would be different ints.
I am guessing using struct tm and/or local time.
1
2
3
4
5
6
7
8
9
int tm_sec;
int tm_min;
int tm_hour;
int tm_mday;
int tm_mon;
int tm_year;
int tm_wday;
int tm_yday;
int tm_isdst;


1
2
3
4
5
6
7
8
9
10
11
int main ()
{
  time_t rawtime;
  struct tm * timeinfo;

  time ( &rawtime );
  timeinfo = localtime ( &rawtime );
  
  return 0;
}

I don't know how to implement the two together

now all you need is to access waht u wanted from the tm via e.g. timeinfo->tm_hour ,tm_sec or tm_minute <- all cute ints <3

does that mean I put the tms after time info after localtime and in the parentheses i put my tms???

all i see is the tm ints and I don't know what to do with them....
incubbus: i know, Struc tm, but i dont know what function to use much less how to use it
Last edited on
localtime fills tm's members, what are you asking?
I think you need to read up on structures and how to use them. Read this page:
http://cplusplus.com/doc/tutorial/structures/
1
2
3
4
5
time_t rawtime;
  struct tm * timeinfo;

  time ( &rawtime );
  timeinfo = localtime ( &rawtime );


this fills ur timeinfo pointer with the address to an tm-structure matching the current date/time on your machine...

now u can easily access its members u dont need any more functions to access those... if u dont know how to access structures´ members, read the link Raskell posted, above...
Ok i think i nearly got this right or not right at all:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <ctime>
#include <iostream>
using namespace std;
int month,year,day;
int main ()
{
  time_t rawtime;
  struct tm * timeinfo;
  time ( &rawtime );
  timeinfo = localtime ( &rawtime );
  timeinfo->tm_mon = month;
  timeinfo->tm_year = year;
  timeinfo->tm_mday = day;
  cout << month <<": "<<year<< ": " << day << endl;
  system ("PAUSE");
  return 0;
}
  


It displays this output which i dont know how its possible
0: 0: 0



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <ctime>
#include <iostream>
using namespace std;
int month,year,day;
int main ()
{
  time_t rawtime;
  struct tm * timeinfo;
  time ( &rawtime );
  timeinfo = localtime ( &rawtime );
  timeinfo->tm_mon = month;
  timeinfo->tm_year = year;
  timeinfo->tm_mday = day;
  cout << month <<": "<<year<< ": " << day << endl;
  system ("PAUSE");
  return 0;
}
What the program does:


declare and initialize month, year, day to 0






assign variables initialized with 0 to struct members which hold the correct values


display the still zeroed variables

Pages: 12