_strtime

Hi guys I have a simple question. How will I add an hour to the current time using this codes?

1
2
3
4
5
  char timenow[9];
  _strtime(timenow);
//modify current time to
//insert additional hour here
cout<<timenow;
I am not exactly familar with the default format of _strtime though I would image offsets 0 and 1 are the hours so you could check if offset 1 is less than 9 and if so increment it by 1. Otherwise set it to 0 and increment index 0 by 1. You'd also have to make sure that it isn't 23 hours when you increment because 24 hours would be equal to 0.
If you could show me an example I would really appreciate it :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <ctime>

int main()
{
    // http://en.cppreference.com/w/cpp/chrono/c/time
    std::time_t now = std::time( 0 ) ; // current time (since epoch)
    
    // convert to calendar time 
    // http://en.cppreference.com/w/cpp/chrono/c/localtime
    std::tm* ptm = std::localtime( &now ) ; 
    char cstr[ 128 ] ;

    // format and print
    // http://en.cppreference.com/w/cpp/chrono/c/strftime
    std::strftime( cstr, sizeof( cstr ), "%c", ptm ) ;
    std::cout << "local time now: " << cstr << '\n' ;

    ptm->tm_hour += 100 ; // add 100 hours 

    // ptm->tm_hour would now be out of range; normalise 
    // http://en.cppreference.com/w/cpp/chrono/c/mktime
    std::time_t now_plus_100_hrs = std::mktime( ptm ) ;
    ptm = std::localtime( &now_plus_100_hrs ) ;

    // format and print
    std::strftime( cstr, sizeof( cstr ), "%c", ptm ) ;
    std::cout << "local time 100 hours from now: " << cstr << '\n' ;
}

http://coliru.stacked-crooked.com/a/6f789271f31fd46f
Topic archived. No new replies allowed.