date - Don't understand tm_struct (C++) calculations - is there an offset of some kind? -


i can't understand why tm struct in c++ behaves way. let me more specific - if current time, i'd this

time_t = time(0); tm *nowtm = gmtime(&now); 

and upon printing out date, expect 2015/06/13 (the current date of post)

cout << nowtm->tm_year << "/" << nowtm->tm_mon << "/" << nowtm->tm_mday; 

but instead, find out prints out 1150/5/13 instead. month value, added 1 set correct month, playing around year proved troublesome.

i came across post: algorithm add or subtract days date?, said subtract 1900 year correct year. tried no avail.

i tried adding on difference between current year , 1150, 2015 - 1150 = 865 correct year, gave me 9800 instead of 2015.

i experimented adding year, , found that

  1. if +1 year, goes in increments of 10 years.
  2. if +0.1 year, divide date 0 , add 0.1 (e.g. 1150 + 1 = 115.01).

i'm confused - why happen , how correct year in tm struct?

from documentation on tm can see that:

  • tm_year years since 1900, not current year number, i.e. should 115 year.
  • tm_mon months since january (range 0-11), not number of month.

so need is:

std::cout << 1900 + nowtm->tm_year    << "/" << 1 + nowtm->tm_mon    << "/" << nowtm->tm_mday; 

Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -