Is there any way to set a specific date and time on linux machine through C Code

0

**> I tried this code however it is not working without root privileges. Is there any way to use >settimeofday in C code

Please go through the code and do let me know what can be done in this case gettimeofday is working fine but settimeofday is giving error. I am stuck on this from days and not able to figure out what can be done here**

'''
    #include <stdio.h>
    #include <string.h>
    #include <time.h>
    #include <sys/time.h>
    #define ERROR -1
    #define OK 1
    typedef struct
    {
         int day;
         int mon;
         int year;
         int hr;
         int min;
         int sec;
    }tmfh_date;
    int main()
    {
         tmfh_date date;
         struct tm info;
         struct timezone tz;
         char buffer[1024];
         time_t ti;
         struct timeval T;
         int ret_Status = -1;
         //      time(&ti);
         //      info  = localtime(&ti);
         date.day = 12;
         date.mon = 12;
         date.year = 2020;
         date.hr = 0xFFFFFFFF;
         date.min = 0xFFFFFFFF;
         date.sec = 0xFFFFFFFF;
         FILE *cmd1 = NULL;
         if ((date.hr != 0xFFFFFFFF) && (date.min != 0xFFFFFFFF) && (date.sec != 0xFFFFFFFF))
         {       info->tm_hour = date.hr;
                 info->tm_min = date.min;
                 info->tm_sec = date.sec;*/
         }
         else  if ((date.year != 0xFFFFFFFF) && (date.mon != 0xFFFFFFFF) && (date.day != 0xFFFFFFFF))
         {        info.tm_year = date.year;
                  info.tm_mon = date.mon ;
                  info.tm_mday= date.day;
         }
         else
         {        info->tm_hour = date.hr;
                  info->tm_min = date.min;
                  info->tm_sec = date.sec;
                  info->tm_year = date.year;
                  info->tm_mon = date.mon;
                  info->tm_mday= date.day ;
          }
          ret_Status = gettimeofday (&T, &tz);
          printf("%d\n",ret_Status);
          T.tv_sec = mktime(&info);
          printf("%ld\n",T.tv_sec);
          if( T.tv_sec == -1 )
           {
                 return ERROR;
           }
           // T.tv_usec = 1000000*T.tv_sec ;
           ret_Status = settimeofday(&T, &tz);
           if (ret_Status != OK)
           {     //error position    
                 return ERROR;
           }
           return OK;
        }
'''
c
linux
date
root
asked on Stack Overflow Dec 3, 2020 by Jyoti Rawat

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0