This blog is under construction

Sunday, 6 May 2012

strftime example in C

Header file:

     size_t strftime(char *str, size_t sz, const char *fmt,
                                        const struct tm *tp);

     Date and time information in *tp is formatted based on the format string in fmt and the result is placed inside the character array(pointer) str of size sz.

strftime function C example:

  int main() {
        time_t tp;
        struct tm *ts;
        int size;
        char str[100];
        tp = time(NULL);
        ts = localtime(&tp);
        size = strftime(str, 100, "%a %b %d %H:%M:%S %Y\n", ts);
        printf("%s", str);
        return 0;

  jp@jp-VirtualBox:~/cpgms/time$ ./a.out
  Sun May 06 00:01:55 2012

No comments:

Post a Comment