Un mémorandum sur certaines fonctions de CTIME (3)
Un type pour mettre unixtime. Il semble qu'il soit implémenté comme un type entier de 8 octets dans mon environnement.
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)
Reçoit une valeur de type time_t et renvoie la représentation sous forme de chaîne d'heure locale correspondante.
#include <stdio.h>
#include <time.h>
int main(int argc, char *argv[]) {
    time_t now = time(NULL);
    printf("%s", ctime(&now));
}
production
Sun May 19 20:23:47 2019
struct tm *gmtime(const time_t *timep)
Reçoit une valeur de type time_t et renvoie un pointeur vers la structure UTC tm correspondante.
#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
	);
}
production
2019-5-19 14:39:29
struct tm *localtime(const time_t *timep)
Reçoit une valeur de type time_t et renvoie un pointeur vers la structure tm d'heure locale correspondante.
#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
	);
}
production
2019-5-19 23:45:2
char *asctime(const struct tm *tm)
Prend un pointeur vers la structure tm et renvoie une représentation sous forme de chaîne de l'heure correspondante.
#include <stdio.h>
#include <time.h>
int main(int argc, char *argv[]) {
	time_t now = time(NULL);
	printf("%s", asctime(localtime(&now)));
}
production
Sun May 19 20:23:47 2019
Page de manuel CTIME (3)
Recommended Posts