Sep 10, 2013 at 12:52pm UTC
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????
Sep 10, 2013 at 1:01pm UTC
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.
Sep 10, 2013 at 1:07pm UTC
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
Sep 10, 2013 at 1:44pm UTC
Don't mix C and C++, you can't shouldn't use both cout and printf.
Last edited on Sep 10, 2013 at 4:41pm UTC
Sep 11, 2013 at 7:23am UTC
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
Sep 11, 2013 at 8:30am UTC
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 Sep 11, 2013 at 8:31am UTC