Pejman Moghadam / C-programming

C - Date and Time

Public Domain


strftime - format date and time

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

int main()
{
    time_t t;
    struct tm *tm;
    char str[200];

    t = time(NULL);

    tm = localtime(&t);
        if (tm == NULL) {
                perror("localtime");
                exit(EXIT_FAILURE);
        }

    if (strftime(str, sizeof(str), "%a %b %e %T %Z %Y", tm) == 0) {
            fprintf(stderr, "strftime returned 0");
            exit(EXIT_FAILURE);
    }
    printf("strftime is %s\n", str);

    tm = localtime(&t);
    printf("\n--Local Time--\n");
    printf("tm->tm_sec: %d\n", tm->tm_sec);
    printf("tm->tm_min: %d\n", tm->tm_min);
    printf("tm->tm_hour: %d\n", tm->tm_hour);
    printf("tm->tm_mday: %d\n", tm->tm_mday);
    printf("tm->tm_mon: %d\n", tm->tm_mon);
    printf("tm->tm_year: %d\n", tm->tm_year);
    printf("tm->tm_wday: %d\n", tm->tm_wday);
    printf("tm->tm_yday: %d\n", tm->tm_yday);
    printf("tm->tm_isdst: %d\n", tm->tm_isdst);

    tm = gmtime(&t);
    printf("\n--Greenwich Time--\n");
    printf("tm->tm_sec: %d\n", tm->tm_sec);
    printf("tm->tm_min: %d\n", tm->tm_min);
    printf("tm->tm_hour: %d\n", tm->tm_hour);
    printf("tm->tm_mday: %d\n", tm->tm_mday);
    printf("tm->tm_mon: %d\n", tm->tm_mon);
    printf("tm->tm_year: %d\n", tm->tm_year);
    printf("tm->tm_wday: %d\n", tm->tm_wday);
    printf("tm->tm_yday: %d\n", tm->tm_yday);
    printf("tm->tm_isdst: %d\n", tm->tm_isdst);

    return 0;
}

Output

    strftime is Thu Jan 16 00:40:36 IRST 2014

    --Local Time--
    tm->tm_sec: 36
    tm->tm_min: 40
    tm->tm_hour: 0
    tm->tm_mday: 16
    tm->tm_mon: 0
    tm->tm_year: 114
    tm->tm_wday: 4
    tm->tm_yday: 15
    tm->tm_isdst: 0

    --Greenwich Time--
    tm->tm_sec: 36
    tm->tm_min: 10
    tm->tm_hour: 21
    tm->tm_mday: 15
    tm->tm_mon: 0
    tm->tm_year: 114
    tm->tm_wday: 3
    tm->tm_yday: 14
    tm->tm_isdst: 0

strptime - convert a string representation of time to a time tm structure

#define _XOPEN_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

int main()
{
    struct tm tm;
    char str[255];

    memset(&tm, 0, sizeof(struct tm));
    strptime("2014-01-16 00:01:54", "%Y-%m-%d %H:%M:%S", &tm);
    strftime(str, sizeof(str), "%d %b %Y %H:%M", &tm);
    puts(str);
    exit(EXIT_SUCCESS);
}

Output

16 Jan 2014 00:01

mktime - converts a broken-down time structure, expressed as local time, to calendar time representation

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

int main()
{
    struct tm *tm = malloc(sizeof(struct tm));
    time_t t;
    char str[200];

    memset((void *)tm, 0, sizeof(struct tm));

    tm->tm_sec   = 13;
    tm->tm_min   = 13;
    tm->tm_hour  = 13;
    tm->tm_mday  = 10;
    tm->tm_mon   = 0;
    tm->tm_year  = 100;
    tm->tm_isdst = 0; 

    t = mktime(tm);

    tm = localtime(&t);
    strftime(str, sizeof(str), "%F %T", tm);
    printf("localtime is %s\n", str);

    tm = gmtime(&t);
    strftime(str, sizeof(str), "%F %T", tm);
    printf("greenwich is %s\n", str);

    return 0;
}

Output

localtime is 2000-01-10 13:13:13
greenwich is 2000-01-10 09:43:13

gettimeofday

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

int main()
{
    struct timeval tv;
    struct tm *tm;
    char str[200];

    if(gettimeofday(&tv, NULL) == -1 ) {
            perror("gettimeofday");
            exit(EXIT_FAILURE);
    }
    printf("tv->tv_sec: %d\n", tv.tv_sec);
    printf("tv->tv_usec: %d\n", tv.tv_usec);

    strftime(str, sizeof(str), "%a %b %e %T %Z %Y", localtime(&tv.tv_sec));
    printf("strftime is %s\n", str);

    exit(EXIT_SUCCESS);
}

Output

tv->tv_sec: 1389879829
tv->tv_usec: 314898
strftime is Thu Jan 16 17:13:49 IRST 2014

BY: Pejman Moghadam
TAG: c, date, time, strftime
DATE: 2014-01-16 17:14:22


Pejman Moghadam / C-programming [ TXT ]