Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Friday, January 1, 2016

Get the current time in C

Get the current time in C


I want to get the current time of my system. For that I'm using the following code in C:

time_t now;  struct tm *mytime = localtime(&now);   if ( strftime(buffer, sizeof buffer, "%X", mytime) )  {      printf("time1 = \"%s\"\n", buffer);  }  

The problem is that this code is giving some random time. Also, the random time is different everytime. I want the current time of my system.

Answer by Erik for Get the current time in C


Initialize your now variable.

time_t now = time(0); // Get the system time  

The localtime function is used to convert the time value in the passed time_t to a struct tm, it doesn't actually retrieve the system time.

Answer by mingos for Get the current time in C


Copy-pasted from here:

/* localtime example */  #include   #include     int main ()  {    time_t rawtime;    struct tm * timeinfo;      time ( &rawtime );    timeinfo = localtime ( &rawtime );    printf ( "Current local time and date: %s", asctime (timeinfo) );      return 0;  }  

(just add "void" to the main() arguments list in order for this to work in C)

Answer by zishan for Get the current time in C


easy way :

#include   #include     int main(void)  {      time_t mytime;      mytime = time(NULL);      printf(ctime(&mytime));        return 0;  }  

Answer by p2013 for Get the current time in C


guys i got a new way get system time. though its lengthy and is full of silly works but in this way you can get system time in integer format.

#include   #include     int main()  {      FILE *fp;      char hc1,hc2,mc1,mc2;      int hi1,hi2,mi1,mi2,hour,minute;      system("echo %time% >time.txt");      fp=fopen("time.txt","r");      if(fp==NULL)         exit(1) ;      hc1=fgetc(fp);      hc2=fgetc(fp);      fgetc(fp);      mc1=fgetc(fp);      mc2=fgetc(fp);      fclose(fp);      remove("time.txt");      hi1=hc1;      hi2=hc2;      mi1=mc1;      mi2=mc2;      hi1-=48;      hi2-=48;      mi1-=48;      mi2-=48;      hour=hi1*10+hi2;      minute=mi1*10+mi2;      printf("Current time is %d:%d\n",hour,minute);      return 0;  }  

Answer by user309026 for Get the current time in C


#include   #include     void main()  {      time_t t;      time(&t);      clrscr();        printf("Today's date and time : %s",ctime(&t));      getch();  }  

Answer by hexinpeter for Get the current time in C


To extend the answer from @mingos above, I wrote the below function to format my time to a specific format ([dd mm yyyy hh:mm:ss]).

// Store the formatted string of time in the output  void format_time(char *output){      time_t rawtime;      struct tm * timeinfo;        time ( &rawtime );      timeinfo = localtime ( &rawtime );        sprintf(output, "[%d %d %d %d:%d:%d]",timeinfo->tm_mday, timeinfo->tm_mon + 1, timeinfo->tm_year + 1900, timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);  }  

More information about struct tm can be found here.

Answer by Shauket Sheikh for Get the current time in C


guys you can use this function to get current local time. if you want gmtime then use gmtime function instead of localtime. cheers

time_t time;  struct tm * timeinfo;   time (&time);  timeinfo = localtime (&time);  CCLog("year->%d",timeinfo->tm_year+1900);  CCLog("month->%d",timeinfo->tm_mon+1);  CCLog("date->%d",timeinfo->tm_mday);  CCLog("hour->%d",timeinfo->tm_hour);  CCLog("minutes->%d",timeinfo->tm_min);  CCLog("seconds->%d",timeinfo->tm_sec);  

Answer by guneet for Get the current time in C


#include  #include    void main()  {      time_t t;      time(&t);      printf("\n current time is : %s",ctime(&t));  }  

Answer by Ashish Ahuja for Get the current time in C


This link worked for me. 'Mingos' answer is good, but I added this answer for the link.


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.