question about time in c++

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


If the variables are assigned to the struct members with the correct values then shouldn't cout display the correct values...
hence assign???
Last edited on
You're parsing the sentence incorrectly.
assign [variables initialized with 0] to [struct members which hold the correct values]

In other words, you're overwriting the correct values with zeroes, and then you're printing the zeroes.
Last edited on
I followed the example almost exactly, So how is it wrong?
Edit: am i using this wrong? ->
Last edited on
You are using = wrong
1
2
3
timeinfo->tm_mon = month;
  timeinfo->tm_year = year;
  timeinfo->tm_mday = day;


by doing this u are replacing the prefilled values of tm_mon, tm_year and tm_mday with those in month, year and day (those u created urself... - as bazzy explained in his post)...

= : takes the right operand and assing its val to the left...

so u got to turn things to... year = timeinfo->tm_year; (understood?)

but: even if u do so the program would show
0 , 110 , [the current day]
...
this is because the values in your tm-structure are filled as in the following link (read the table carefully - i didnt do this the last time^^) http://cplusplus.com/reference/clibrary/ctime/tm/
Yes... I got it. I knew I had something wrong in the timeinfo->tm_???? = ????; lines
tm_year years since 1900

and
tm_mon months since January 0-11


So I am guessing a couple if statements would fix the month problem and I would need a new variable to
do var = year + 1990;
but then if i did elapsed time with that it would only go from 1900 to present how would i fix that?

also I just noticed another problem... If I was doing elapsed time, Then DST would get in the way every year and mess up the results. It could be off by more than a couple days. Is it possible to use tm_isdst to fix this?
Or does loosing an hour and then gaining back another cancel this out?
Last edited on
why a couple of if-statements... just add 1 to your month :D... if u want to write out the month´s name use an switch-control structure...

beginner at programming wrote:
also I just noticed another problem... If I was doing elapsed time, Then DST would get in the way every year and mess up the results. It could be off by more than a couple days

how do you mean that?
Whoever came up with DST deserves to burn in Hell for ever and ever.

The typical way to solve this is to keep all times in UTC internally, and only compute DST for display.
Whoever came up with DST deserves to burn in Hell for ever and ever.

I aggree. And Ok.

why a couple of if-statements... just add 1 to your month :D... if u want to write out the month´s name use an switch-control structure...

Maybe I didn't think that through.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <ctime>
#include <iostream>
using namespace std;
// GLOBAL VARIABLES
int month,year,day,year1,month1;
int main () //main
{
   time_t rawtime;
  struct tm * timeinfo;
  time ( &rawtime );
  timeinfo = localtime ( &rawtime );
  month = timeinfo->tm_mon;
  year = timeinfo->tm_year;
  day = timeinfo->tm_mday;
  year1 = year + 1990;
   // month fixed
   month1 = month + 1;
  
       /* Prints The correct date */     
   cout << month1 <<"/" << day << "/" <<year1  << endl;
    system ("PAUSE");
  return 0;
} // end of main
1/18/2100 


I dont know how that is possible?


Last edited on
oh i see it.. I knew it was years since 1900 but for some reason I messed up in my head...
Topic archived. No new replies allowed.
Pages: 12