Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7591001
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T20:32:52+00:00 2026-05-30T20:32:52+00:00

I read in two strings with a Year, the Julian Day (year day), hour,

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T20:32:53+00:00Added an answer on May 30, 2026 at 8:32 pm

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two strings that I need to pull data out of but can't
I am trying to read in two strings separated by a space. cin>>a; cin>>b;
I'm trying to find out if two strings I have are the same, for
I'm writing a program which is supposed to read two strings that can contain
I need a way to read and delete text between two different strings found
The following two functions are extremely similar. They read from a [String] n elements,
I just read two articles over this topic which provide infomration inconsistent, so I
I have read two recent posts that discuss Depends and Imports Upcoming NAMESPACE, Depends,
I have a bash script as below, and I want it to read two
During the last few weeks, I had the opportunity to read two documents: The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.