I read in two strings with a Year, the Julian Day (year day), hour, minute, and an observation.
I pull the relevant variables out using sscanf:
sscanf(tide_str1.c_str(), "%d %d %d %d %Lf", &y1, &j1, &h1, &m1, &obs1);
sscanf(tide_str2.c_str(), "%d %d %d %d %Lf", &y2, &j2, &h2, &m2, &obs2);
For this particular data set, the values are 2011 083 23 22 1.1
I then create and populate a tm structure, and run mktime, with cout calls on the day in between and it changes from 083 to 364.
int y1=2011, j1=83, h1=23, m1=22;
struct tm time_struct = {0, 0, 0, 0, 0, 0, 0, 0, 0}, *time_ptr = &time_struct;
time_t tv_min;
time_struct.tm_year = y1 - 1900;
time_struct.tm_yday = j1;
cout << time_struct.tm_yday << endl;
time_struct.tm_hour = h1;
time_struct.tm_min = m1;
time_struct.tm_isdst = -1;
cout << time_struct.tm_yday << endl;
tv_min = mktime(time_ptr);
cout << time_struct.tm_yday << endl;
Why is that? Is it because the tm_mday and and tm_mon are set to 0? I initially tried not initializing it all to zero, but then mktime returned -1. What should I be doing differently if I only know the year day and not the month and month day?
mktime()
is doing what it's supposed to do.Quoting the C standard:
mktime()
can compute the values oftm_mday
andtm_yday
from other members; it isn't designed to compute the values of other members from those fields.What you can do, though, is initialize a
struct tm
with out-of-range values. For example, if you wanttm_yday
to be 200 (the 200th day of the year), you can initialize astruct tm
representing the 200th day of January.mktime()
will then normalize it to the correct date, yielding atime_t
value that you can then feed togmtime()
orlocaltime()
.Here's a simple example:
The output is: