mog KUPO ! | Je cherche à écrire 2 fonction de conversion de date:
L'une qui me transforme un timestamp "SQL-compliant" en timestamp en seconde (utc):
Code :
- char* DAT_SecondsToTimestamp(unsigned long seconds) {
- struct tm* time = NULL;
- static char timestamp[DAT_TIMESTAMP_SIZE] = "";
- //---
- time = gmtime(&seconds);
- sprintf(timestamp, "%02d-%02d-%02d %02d:%02d:%02d", time->tm_mday, time->tm_mon+1, (1900+time->tm_year)%1000, time->tm_hour, time->tm_min, time->tm_sec);
- return timestamp;
- }
|
L'autre fait la conversion inverse
Code :
- unsigned long DAT_TimestampToSeconds(char* timestamp) {
- char tmp[3];
- struct tm* time = NULL;
- //---
- time = (struct tm*)malloc(sizeof(struct tm));
- strncpy(tmp, ×tamp[DAT_TIMESTAMP_SS_POS], 2);
- time->tm_sec = atoi(tmp);
- strncpy(tmp, ×tamp[DAT_TIMESTAMP_MI_POS], 2);
- time->tm_min = atoi(tmp);
- strncpy(tmp, ×tamp[DAT_TIMESTAMP_HH_POS], 2);
- time->tm_hour = atoi(tmp);
- strncpy(tmp, ×tamp[DAT_TIMESTAMP_DD_POS], 2);
- time->tm_mday = atoi(tmp);
- strncpy(tmp, ×tamp[DAT_TIMESTAMP_MM_POS], 2);
- time->tm_mon = atoi(tmp) - 1;
- strncpy(tmp, ×tamp[DAT_TIMESTAMP_YY_POS], 2);
- time->tm_year = atoi(tmp) + 100;
- time->tm_wday = 0;
- time->tm_yday = 0;
- time->tm_isdst = 1;
- return mktime(time);
- }
|
Mais cette dernière ne fonctionne pas correctement. Je "perd" 2h00 ... comme par hasard c'est le décalage horaire GMT.
1) Est-ce que je cherche compliqué et il existe une autre manière de faire ça ?
2) Sinon ... ben aidez moi !
Petit exemple:
Code :
- printf("1065705799 = %s\n", DAT_SecondsToTimestamp(1065705799));
- printf("09-10-03 13:23:19 = %d\n", toto = DAT_TimestampToSeconds("09-10-03 13:23:19" ));
- printf("Time = %s\n", DAT_SecondsToTimestamp(toto));
|
M'affiche ça:
1065705799 = 09-10-03 15:23:19
09-10-03 13:23:19 = 1065698599
Time = 09-10-03 13:23:19
PS: les constante:
#define DAT_TIMESTAMP_SIZE 17+1 // Format 'DD-MM-YY HH:MI:SS'
#define DAT_TIMESTAMP_DD_POS 0
#define DAT_TIMESTAMP_MM_POS 3
#define DAT_TIMESTAMP_YY_POS 6
#define DAT_TIMESTAMP_HH_POS 9
#define DAT_TIMESTAMP_MI_POS 12
#define DAT_TIMESTAMP_SS_POS 15
Message édité par mog le 09-10-2003 à 16:39:46
|