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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:39:45+00:00 2026-06-15T19:39:45+00:00

given a standard ISO 8601 timestamp, is there an easy way to get a

  • 0

given a standard ISO 8601 timestamp, is there an easy way to get a time stamp from 20 minutes before and another time stamp of 20 minutes after the original timestamp?

For example given

 2010-01-25 18:02:00

I would want one or two functions to return,
2010-01-25 17:48:00 and 2010-01-25 18:22:00

The solution I gave up with is rather cumbersome, I use s.substr(14,2) to get the minutes and s.substr(12,2) to get the hours, convert the hour and minute strings to int and subtract or add the minute value with a condition if it goes above 60 or below 0. Is there a better/easier way to do this?

  • 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-15T19:39:46+00:00Added an answer on June 15, 2026 at 7:39 pm

    You could:

    1. Convert the string into a struct tm using std::get_time.
    2. Modify the struct tm (no need to consider the ranges).
    3. Normalize the struct tm using mktime.
    4. Print the struct tm using std::put_time

    #include <ctime>
    #include <string>
    #include <sstream>
    #include <iostream>
    #include <iomanip>
    
    std::string increase_minutes(const std::string& input, int delta) {
        struct tm tm;
        constexpr auto format = "%Y-%m-%d %T";
    
        std::istringstream iss (input);
        iss >> std::get_time(&tm, format);
        tm.tm_min += delta;
        mktime(&tm);
    
        std::ostringstream oss;
        oss << std::put_time(&tm, format);
        return oss.str();
    }
    
    
    int main() {
        auto s = "2012-12-31 23:59:00";
        std::cout << increase_minutes(s, 20) << std::endl;
        std::cout << increase_minutes(s, -20) << std::endl;
    }
    

    Note that std::get_time and std::put_time are absent in g++/libstdc++. You may need to parse the string using the C functions sscanf or strptime, and print the string using strftime. See also C++ date and time.


    Edit: If we want to change by a generic std::chrono::duration, it is better we manipulate on a std::chrono::time_point directly. We could create a time_point from a struct tm with a two-step conversion: time_point ↔ time_t ↔ struct tm, as illustrated below:

    #include <ctime>
    #include <string>
    #include <sstream>
    #include <iostream>
    #include <iomanip>
    #include <chrono>
    
    template <typename R, typename P>
    std::string increase_iso_time(const std::string& input, 
                                  const std::chrono::duration<R, P>& duration) {
        struct tm tm;
        constexpr auto format = "%Y-%m-%d %T";
    
        std::istringstream iss (input);
        iss >> std::get_time(&tm, format);
        auto time = mktime(&tm);
        auto tp = std::chrono::system_clock::from_time_t(time);
    
        tp += duration;
    
        time = std::chrono::system_clock::to_time_t(tp);
        tm = *localtime(&time);
        std::ostringstream oss;
        oss << std::put_time(&tm, format);
        return oss.str();
    }
    
    int main() {
        auto s = "2012-12-31 23:59:00";
        std::cout << increase_iso_time(s, std::chrono::minutes(20)) << std::endl;
        std::cout << increase_iso_time(s, - std::chrono::hours(4)
                                          - std::chrono::minutes(20)) << std::endl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a datetime.time value in Python, is there a standard way to add an
Given that I have a Foo model w/ the standard Rails timestamp columns what
Given DateTime start = startsomething and DateTime end = endSomething Is there a standard
Is there a standard/common way to give compiler-style error messages that point to a
This is the statement from ISO C++ Standard 14.6.2.1: Dependent types : A type
We are using the ISO 8601-standard to show week numbers here in Sweden. Most
we're looking for a standard way to encode a URL given a predefined base
Is there a standard method to repr the call that resulted in a given
Given an index and a size, is there a more efficient way to produce
This is the statement from ISO C++ Standard 14.6.2.4: Dependent template arguments : A

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.