Bonjour,
je cherche à faire une fonction qui prend comme argument une date de la forme "Dec 18 00:00:20" et qui renvoie le time_t correspondant. Ma fonction renvoie 1.252.299.098 alors qu'il semblerait que la bonne réponse soit 1.261.094.420.
Pouvez-vous m'aider à voir ce qui ne va pas?
Merci d'avance.

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
#include <stdio.h>
#include <string.h>
#include <time.h>
 
time_t string_to_time_t(char *s)
{
        time_t rawtime;
        struct tm * timeinfo;
        char month_s[3],day_s[2],hour_s[2],minute_s[2],second_s[2];
        int month,day,hour,minute,second;
        char *p=s;
 
        strncpy(month_s,p,3); p+=4;
        strncpy(day_s,p,2); p+=3;
        strncpy(hour_s,p,2); p+=3;
        strncpy(minute_s,p,2); p+=3;
        strncpy(second_s,p,2);
 
        char * month_names[12]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
        int i=0;
        while( !(strcmp(month_s,month_names[i])) )
                i++;
        month=i;
       day=atoi(day_s);
        hour=atoi(hour_s);
        minute=atoi(minute_s);
        second=atoi(second_s);
 
        time (&rawtime);
        timeinfo=localtime(&rawtime);
        timeinfo->tm_mon=month;
        timeinfo->tm_mday=day;
        timeinfo->tm_hour=hour;
        timeinfo->tm_min=minute;
        timeinfo->tm_sec=second;
        return mktime(timeinfo);
}
 
int main(int argc, char **argv)
{
        printf("temps %lu\n",(unsigned long)string_to_time_t("Dec 18 00:00:20"));
        return 0;
}