The following is an introduction to the difference between gmtime and localtime functions in Linux. As can be seen from the following code running results, there are some differences in the usage of gmtime and localtime. gmtime and localtime are two different functions, and many people tend to confuse the two when using them. The following editor will teach you how to distinguish the use of gmtime and localtime under Linux, so that you can use them correctly next time.
the difference:
#include "time.h"
#include "stdio.h"
int main(int argc, char **argv)
{
time_t tmpcal_ptr = {0};
struct tm *tmp_ptr = NULL;
tmpcal_ptr = time(NULL);
printf("tmpcal_ptr=%dn", tmpcal_ptr);
tmp_ptr = gmtime(&tmpcal_ptr);
printf ("after gmtime, the time is: n%d:%d:%d", tmp_ptr-》tm_hour, tmp_ptr-》tm_min, tmp_ptr-》tm_sec);
tmp_ptr = localtime(&tmpcal_ptr);
printf ("after localtime, the time is: n%d:%d:%d", tmp_ptr-》tm_hour, tmp_ptr-》tm_min, tmp_ptr-》tm_sec);
return 0;
}
The running results are as follows:
The basic meaning is that what gmtime converts is the standard time in the 0 time zone
localtime is the time in the current time zone that is transferred out, taking the time zone into account. However, please note that on some systems that have been cut down on embedded devices, the time zone has not been set properly, causing the time transferred out by both to be in time zone 0.