time_t ts;
struct tm tm;
strptime("2001-11-12 18:31:01", "%Y-%m-%d %H:%M:%S", &tm);
strftime(&ts, 50, "%s", &tm);
printf( "TS : %d\n", &ts );
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
char date_str[] = "2023-07-05 11:00:33.855172883";
struct tm tm_struct;
double timestamp;
// Utilisation de strptime pour analyser la chaîne en une structure tm
if (strptime(date_str, "%Y-%m-%d %H:%M:%S", &tm_struct) == NULL) {
printf("Erreur lors de l'analyse de la date.\n");
return 1;
}
// Ajout des fractions de seconde à la structure tm
char *frac_sec_str = date_str + 20; // pointe vers ".855172883"
double frac_sec = strtod(frac_sec_str, NULL);
tm_struct.tm_sec = (int)frac_sec;
frac_sec -= tm_struct.tm_sec;
tm_struct.tm_gmtoff = frac_sec * 1e9; // stocke les nanosecondes
// Utilisation de mktime pour convertir la structure tm en timestamp
timestamp = (double)mktime(&tm_struct) + frac_sec;
printf("Timestamp : %.9f\n", timestamp);
return 0;
}
➡️ Offre MyRankingMetrics ⬅️
pré-audit SEO gratuit avec RM Tech (+ avis d'expert)
coaching offert aux clients (avec Olivier Duffez ou Fabien Faceries)
Voir les détails ici