mardi 30 juin 2015

How to convert character string in microseconds to struct tm in C?


I have a string that contains microseconds since the epoch. How could I convert it to a time structure?

#include <time.h>
#include <stdio.h>
#include <stdlib.h>

int main ()
{
    struct tm tm; 
    char buffer [80];
    char *str ="1435687921000000";
    if(strptime (str, "%s", &tm) == NULL)
        exit(EXIT_FAILURE); 
    if(strftime (buffer,80,"%Y-%m-%d",&tm) == 0)
        exit(EXIT_FAILURE);

    printf("%s\n", buffer);

    return 0;
}


Aucun commentaire:

Enregistrer un commentaire