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:
sscan
mktime()
is doing what it's supposed to do.
Quoting the C standard:
The mktime function converts the broken-down time, expressed as local time, in the structure pointed to by timeptr into a calendar time value with the same encoding as that of the values returned by the time function. The original values of the tm_wday and tm_yday components of the structure are ignored, and the original values of the other components are not restricted to the ranges indicated above. On successful completion, the values of the tm_wday and tm_yday components of the structure are set appropriately, and the other components are set to represent the specified calendar time, but with their values forced to the ranges indicated above; the final value of tm_mdayis not set until tm_mon and tm_year are determined.
mktime()
can compute the values of tm_mday
and tm_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 want tm_yday
to be 200 (the 200th day of the year), you can initialize a struct tm
representing the 200th day of January. mktime()
will then normalize it to the correct date, yielding a time_t
value that you can then feed to gmtime()
or localtime()
.
Here's a simple example:
#include <iostream>
#include <ctime>
int main()
{
struct tm t = { 0 };
t.tm_sec = t.tm_min = t.tm_hour = 0; // midnight
t.tm_mon = 0; // January
t.tm_year = 2012 - 1900;
t.tm_isdst = -1; // unknown
t.tm_mday = 200; // January 200th?
time_t when = mktime(&t);
const struct tm *norm = localtime(&when); // Normalized time
std::cout << "month=" << norm->tm_mon << ", day=" << norm->tm_mday << "\n";
std::cout << "The 200th day of 2012 starts " << asctime(norm);
}
The output is:
month=6, day=18
The 200th day of 2012 starts Wed Jul 18 00:00:00 2012