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 9226307
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:47:52+00:00 2026-06-18T04:47:52+00:00

I am trying to write a function that takes in a parameter which is

  • 0

I am trying to write a function that takes in a parameter which is the offset days, and returns the date those many offset days from now. I can easily get the current date from below

#include <ctime>
#include <iostream>
using namespace std;

int main() {
time_t t = time(0);   // get time now
struct tm * now = localtime( & t );
cout << (now->tm_year + 1900) << '-' 
     << (now->tm_mon + 1) << '-'
     <<  now->tm_mday
     << endl;
}

My question is if I change the now->tm_mday to now->tm_mday – offset, is it smart enough to do the month change or the year change as they may change.

  • 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-18T04:47:54+00:00Added an answer on June 18, 2026 at 4:47 am

    No — (now->tm_year + 1900), (now->tm_mon + 1) and now->tm_mday are separate expressions and adding a new arithmetic operation to one will not affect the others.

    Apply the offset to t instead, which is an integral value representing seconds since UNIX epoch. Then the change will carry through to the tm structure and, ultimately, each of your output expressions:

    time_t t0 = time(0);               // now
    time_t t1 = time(0) - 5;           // five seconds ago
    time_t t2 = time(0) - 60*60*2;     // two hours ago
    time_t t3 = time(0) - 60*60*24*5;  // five days ago
    
    // (do try to avoid "magic numbers", though)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a function that takes multiple arguments, which can come either
I am trying to write a function that takes File object, offset and byte
I'm trying to write a function that takes two string arguments and returns the
I'm trying to write a java function that takes as a parameter an enum
I'm trying to write a PS function that takes a -Computername parameter. Here is
I am trying to write a function that takes two containers of the same
I am trying to write a function that inputs takes an array i keep
I am trying to write a higher-order Racket function that takes a first-order function
I'm trying to write a simple Vim function that takes the name of a
I'm trying to write a merge-sort function that takes a list and a comparison

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.