A memorandum about some functions in CTIME (3)
A type to put unix time. It seems that it is implemented as an 8-byte integer type in my environment.
struct tm {
int tm_sec; /* Seconds (0-60) */
int tm_min; /* Minutes (0-59) */
int tm_hour; /* Hours (0-23) */
int tm_mday; /* Day of the month (1-31) */
int tm_mon; /* Month (0-11) */
int tm_year; /* Year - 1900 */
int tm_wday; /* Day of the week (0-6, Sunday = 0) */
int tm_yday; /* Day in the year (0-365, 1 Jan = 0) */
int tm_isdst; /* Daylight saving time */
};
char *ctime(const time_t *timep)
Receives a time_t type value and returns the corresponding local time string representation.
#include <stdio.h>
#include <time.h>
int main(int argc, char *argv[]) {
time_t now = time(NULL);
printf("%s", ctime(&now));
}
output
Sun May 19 20:23:47 2019
struct tm *gmtime(const time_t *timep)
Takes a time_t type value and returns a pointer to the corresponding UTC tm structure.
#include <stdio.h>
#include <time.h>
int main(int argc, char *argv[]) {
time_t now = time(NULL);
struct tm *hoge = gmtime(&now);
printf(
"%d-%d-%d %d:%d:%d\n",
hoge->tm_year+1900,
hoge->tm_mon+1,
hoge->tm_mday,
hoge->tm_hour,
hoge->tm_min,
hoge->tm_sec
);
}
output
2019-5-19 14:39:29
struct tm *localtime(const time_t *timep)
Takes a time_t type value and returns a pointer to the corresponding local time tm structure.
#include <stdio.h>
#include <time.h>
int main(int argc, char *argv[]) {
time_t now = time(NULL);
struct tm *hoge = localtime(&now);
printf(
"%d-%d-%d %d:%d:%d\n",
hoge->tm_year+1900,
hoge->tm_mon+1,
hoge->tm_mday,
hoge->tm_hour,
hoge->tm_min,
hoge->tm_sec
);
}
output
2019-5-19 23:45:2
char *asctime(const struct tm *tm)
Takes a pointer to a tm structure and returns a string representation of the corresponding time.
#include <stdio.h>
#include <time.h>
int main(int argc, char *argv[]) {
time_t now = time(NULL);
printf("%s", asctime(localtime(&now)));
}
output
Sun May 19 20:23:47 2019
CTIME (3) man page