w3resource logo

:

C Programming Exercises

C Exercises: Convert a time_t object to calendar time expressed as Coordinated Universal Time

C Date Time: Exercise-6 with Solution

Write a program in C to convert a time_t object to calendar time expressed as Coordinated Universal Time.

Sample Solution:

C Code:

#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
#include <time.h>

int main(void)
{
    time_t t = time(NULL);
    printf("\nThe calendar time expressed as Coordinated Universal Time is :");
    printf("\nUTC:   %s", asctime(gmtime(&t)));
    printf("local: %s\n", asctime(localtime(&t)));
 
#ifdef __STDC_LIB_EXT1__
    struct tm buf;
    char str[26];
    asctime_s(str,sizeof str,gmtime_s(&t, &buf));
    printf("UTC:   %s", str);
    asctime_s(str,sizeof str,localtime_s(&t, &buf)));
    printf("local: %s", str);
#endif
}

Sample Output:

The calendar time expressed as Coordinated Universal Time is :
UTC:   Thu Aug 03 10:53:03 2017
local: Thu Aug 03 16:23:03 2017

N.B.: The result may varry for your current system date and time.

Flowchart:

Flowchart: Convert a time_t object to calendar time expressed as Coordinated Universal Time

C Programming Code Editor:

Improve this sample solution and post your code through Disqus