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

  • SEARCH
  • Home
  • 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 8040139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T03:47:55+00:00 2026-06-05T03:47:55+00:00

I’m making a program that needs the duration (in time_t ) of a year.

  • 0

I’m making a program that needs the duration (in time_t) of a year.

In other ways, time_t of DD/MM/YYYY + duration = time_t of DD/MM/YYYY+1

So it may not always be 365 days (and 29/02/2012 will become 28/02/2013)

Here’s the algorithm I came with :

if YEAR is leap than
    if we are before the 29th feb' than return 365+1 days
    else if we are the 29th feb' than return 365-1 days
    else return 365 days
else if YEAR+1 is leap than
    if we are before or the 28th feb' than return 365 days
    else return 365+1 days
else return 365 days

Here, a day is 60 * 60 * 24 seconds

This algorithm seems to work. But I was wondering if there were another way to do this without all theses conditions and only 2 possible return values, or just some “trick” to optimize the thing.

I tried to increment tm_year from the struct tm like this :

// t is the input time_t
struct tm Tm (*localtime(&t));
if (Tm.tm_mon == 2 && Tm.tm_mday == 29) --Tm.tm_mday;
++Tm.tm_year;
return mktime(&Tm) - t;

But the result isn’t what I want, I got -1 hour, or -25…

I guess it’s because a year is not exactly 365 * 24 * 60 * 60.

  • 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-06-05T03:47:56+00:00Added an answer on June 5, 2026 at 3:47 am

    I would use Boost for this, since it already implements what you are looking for:

    #include <iostream>
    #include <boost/date_time/gregorian/gregorian_types.hpp>
    namespace date = boost::gregorian;
    
    int main() {
       date::date_period dp(date::date(2012, 6, 4), date::date(2013, 6, 4));
       long days = dp.length().days();
       std::cout << "Days between dates: " << days << std::endl;
    

    }

    If you wanted more precision, then you could use posix_time from Boost too:

    namespace ptime = boost::posix_time;
    
    ...
    
    ptime::ptime t1(date::date(2012, 6, 4), ptime::hours(0));
    ptime::ptime t2(date::date(2013, 6, 4), ptime::hours(0));
    
    ptime::time_duration td = t2 - t1;
    std::cout << "Milliseconds: " << td.total_milliseconds() << std::endl;
    

    Typically time_t is measured in seconds. Therefore, you would just need to call to td.total_seconds() to obtain the value that you are looking for.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm trying to create an if statement in PHP that prevents a single post
I'm making a simple page using Google Maps API 3. My first. One marker

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.