7

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?

RuQu
  • 243
  • 3
  • 4
  • 9
  • The day of the year is sometimes *incorrectly* referred to as a "Julian Day". In fact, a [Julian Day](http://en.wikipedia.org/wiki/Julian_day) number is the number of days since January 1, 4713 BC; it's used in astronomy. – Keith Thompson Mar 05 '12 at 22:52
  • Yes, but it is so widely used (incorrectly) within certain professions as to, in practice, be correct. So long as everyone within a field uses the same jargin...communication happens. – RuQu Mar 05 '12 at 23:47

1 Answers1

20

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_mday is 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
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • I tried your advice and initialized like this: struct tm time_struct = {0, 0, 0, 200, 200, 0, 200, 0, 0} and the result when I called it back was day 77 when it should be 83, which is closer than the 364 from before but still off. – RuQu Mar 05 '12 at 23:02
  • 1
    The standard doesn't guarantee the order of the members of `struct tm`, so initializing it like that won't necessarily work. You should initialize or assign all the members by name. But if they're declared in the usual order, you're initializing `tm_mday` and `tm_mon` to 200. I don't think you want the 200th, or rather 201st, month of the year. (And don't forget that `struct tm` numbers the months 0 to 11, not 1 to 12.) – Keith Thompson Mar 05 '12 at 23:08
  • To be clear, if I want year day 83, I should be setting the month to 0, and the month day to 83? – RuQu Mar 05 '12 at 23:10
  • 1
    @RuQu: Yes. Month 0 is January, so setting `tm_mon` to 0 and `tm_mday` to 83 specifies the 83rd of January, which will then be normalized to March 24 in a leap year, March 25 otherwise. – Keith Thompson Mar 05 '12 at 23:12
  • Yup, that worked, with the obvious 1 day offset from using 0 instead of 1 for the start. Thanks. And kind of new here...how do I vote up your answer since it is in the comments? – RuQu Mar 05 '12 at 23:20
  • @RuQu: You can upvote an answer (meaning you like it) by clicking the up arrow to the left of it. You can *accept* an answer (meaning it's the one that solved your problem) by clicking the check mark. Do either, both, or neither as you choose (this is me *not* trolling for reputation points). You can also upvote (but not downvote) comments if you like, though that's not as meaningful; hovering your cursor to the left of the comment should reveal an up arrow. In all cases, hovering your cursor over an icon should show pop-up text briefly explaining what it does. – Keith Thompson Mar 05 '12 at 23:32