1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
| #include <stdio.h>
#include <time.h>
time_t mktimeGMT(struct tm * tmGmt);
int main(int argc, char** argv)
{
time_t tDate, tDateMirror;
int iDate;
struct tm* tmLoc;
struct tm* tmGmt;
printf("----------------------------------------\n");
printf("Saisie du timestamp\n");
printf("----------------------------------------\n");
printf("Timestamp (<0 pour date courante) = ");
scanf("%d", &iDate);
tDate = (time_t) iDate;
if(tDate<0)
{
time(&tDate);
printf("Timestamp de la date courante = %d\n", (int)tDate);
}
printf("----------------------------------------\n");
printf("Elaboration de la structure tm en locale\n");
printf("----------------------------------------\n");
tmLoc = localtime(&tDate);
printf("Localtime = %02d/%02d/%04d %02d:%02d:%02d\n",
tmLoc->tm_mday, tmLoc->tm_mon+1, tmLoc->tm_year+1900,
tmLoc->tm_hour, tmLoc->tm_min, tmLoc->tm_sec);
printf("----------------------------------------\n");
printf("Recuperation du timestamp avec \"mktime\"\n");
printf("a partir de la structure tm en locale\n");
printf("----------------------------------------\n");
tDateMirror = mktime(tmLoc);
printf("Timestamp = %d", (int)tDateMirror);
if(tDateMirror == tDate) printf(" --> OK\n");
else printf(" --> NOK\n");
printf("----------------------------------------\n");
printf("Elaboration de la structure tm en GMT\n");
printf("----------------------------------------\n");
tmGmt = gmtime(&tDate);
printf("GMTtime = %02d/%02d/%04d %02d:%02d:%02d\n",
tmGmt->tm_mday, tmGmt->tm_mon+1, tmGmt->tm_year+1900,
tmGmt->tm_hour, tmGmt->tm_min, tmGmt->tm_sec);
printf("----------------------------------------\n");
printf("Recuperation du timestamp avec \"mktimeGMT\"\n");
printf("a partir de la structure tm en GMT\n");
printf("----------------------------------------\n");
tDateMirror = mktimeGMT(tmGmt);
printf("Timestamp = %d", (int)tDateMirror);
if(tDateMirror == tDate) printf(" --> OK\n");
else printf(" --> NOK\n");
return 0;
}
time_t mktimeGMT(struct tm * tmGmt)
{
time_t tCurrentDate, tLoc, tGmt, tDiff, tResult;
struct tm tmCurrentLoc, tmCurrentGmt;
struct tm tmGmtSav;
/* Recuperation de la structure GMT */
tmGmtSav = *tmGmt;
/* Recuperation du timestamp courant */
time(&tCurrentDate);
/* Elaboration des structures tm en local et GMT */
tmCurrentLoc = *localtime(&tCurrentDate);
tmCurrentGmt = *gmtime(&tCurrentDate);
/* Recuperation des timestamp comme si il s'agissait */
/* de deux structures tm en local */
tLoc = mktime(&tmCurrentLoc);
tGmt = mktime(&tmCurrentGmt);
/* Entre les deux timestamp recuperes, on a une difference */
/* egale au decalage horaire */
tDiff = tLoc - tGmt;
/* Recuperation du timestamp de la structure qui nous */
/* interesse avec la fonction mktime */
tResult = mktime(&tmGmtSav);
/* Ajout de la difference de timestamp */
if(tResult != -1) tResult += tDiff;
return tResult;
} |
Partager