setting device time...with c++

hi
i got a assignment in which i have to write codes for execution of certain commands.
one of the command is set_time. if user enters set_time 12:12:12
the time should get reset to 12:12:12 no matter what it is now.
can somebody share a code which does this????
Show some code. It's impossible to tell without knowing how the time is being stored.

Have a go. If you get stuck, post with what you have so far.
like to get system time i have used

time_t t = time(NULL);
tm* timePtr = localtime(&t);

cout << "seconds= " << timePtr->tm_sec << endl;
cout << "minutes = " << timePtr->tm_min << endl;
cout << "hours = " << timePtr->tm_hour << endl;

and to set the time i have come up with

time_t mytime = time(O);
struct tm* tm_ptr = localtime(&mytime);

if (tm_ptr)
{
tm_ptr->tm_mon = atoi(date.substr(5,2).c_str()) - 1;
tm_ptr->tm_mday = atoi(date.substr(8,2).c_str());
tm_ptr->tm_year = atoi(date.substr(0,4).c_str());
tm_ptr->tm_min = atoi(newtime.substr(3,2).c_str());
tm_ptr->tm_hour = atoi(newtime.substr(0,2).c_str());
printf("%d\n%d\n%d\n%d\n%d\n", tm_ptr->tm_mon,tm_ptr->tm_mday,tm_ptr->tm_year,tm_ptr->tm_min,tm_ptr->tm_hour);
const struct timeval tv = {mktime(tm_ptr), 0};
settimeofday(&tv, 0);
}
return 0;


but it is not working
So you want the time to be set to 12:12:12?

How about the following code?

1
2
3
4
5
6
void setTime( tm &my_tm, int hrs, int mins, int secs )
{
   my_tm.tm_hour = hrs;
   my_tm.tm_min  = mins;   
   my_tm.tm_sec  = secs;
}


Where the call would be...

1
2
struct tm my_time;
setTime( my_time, 12, 12, 12 );
Don't mix C and C++, you can't shouldn't use both cout and printf.
Last edited on
If we are using the default streams unchanged, we can.
http://en.cppreference.com/w/cpp/io/ios_base/sync_with_stdio
no no i want the code in c++ only...
code snippet which can reset the device tym to whatever i want and not just 12:12:12...it was an example
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <ctime>
#include <string>
#include <sstream>
#include <stdexcept>
// #include <sys/time.h>

// invariant: time_str contains today's local wall clock time in the form 'hh:mm:ss'
std::time_t str_to_time_t( const std::string& time_str )
{
    constexpr char COLON = ':' ;
    enum { UB_HR = 24, UB_MIN = 60, UB_SEC = 60 };

    char delimiter ;
    const std::time_t now = std::time(nullptr) ;
    std::tm tm = *std::localtime( &now );

    std::istringstream stm(time_str) ;
    if( ( stm >> tm.tm_hour && tm.tm_hour >= 0 && tm.tm_hour < UB_HR ) &&
        ( stm >> delimiter && delimiter == COLON ) &&
        ( stm >> tm.tm_min && tm.tm_min >= 0 && tm.tm_min < UB_MIN ) &&
        ( stm >> delimiter && delimiter == COLON ) &&
        ( stm >> tm.tm_sec && tm.tm_sec >= 0 && tm.tm_sec < UB_SEC ) &&
        ( stm >> std::ws && stm.eof() ) )
    {
        return std::mktime(&tm) ;
    }
    else throw std::invalid_argument( "invalid time string" ) ;
}

int main() // minimal test driver
{
    const std::string test[] = { "2:34:6", "19:74:55", "12x18y0", "23:59:59", "12:0:0" } ;
    for( const std::string& s : test )
    {
        try
        {
            const std::time_t time_to_set = str_to_time_t(s) ;
            const std::tm tm = *std::localtime( &time_to_set );
            char cstr[128] ;
            std::strftime( cstr, sizeof(cstr), "%A, %Y %B %d %H:%M:%S", &tm ) ;
            std::cout << cstr << " (" << time_to_set << ")\n" ;
            // const time_val tv { time_to_set, 0 } ;
            // settimeofday( &time_to_set, nullptr ) ;
        }
        catch( const std::exception& )
        {
            std::cerr << "badly formed time string '" << s << "'\n" ;
        }
    }
}

http://ideone.com/0prtOt
Last edited on
Topic archived. No new replies allowed.