w3resource logo


C Programming Exercises

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

Secondary Nav

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.

Expected Output :

The calendar time expressed as Coordinated Universal Time is :          
UTC:   Tue Sep  6 08:18:10 2016                                         
local: Tue Sep  6 13:48:10 2016

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
}

Flowchart :

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

C Practice online:

#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
}

Improve this sample solution and post your code through Disqus



Join our Question Answer community to learn and share your programming knowledge.

Solve these problems:

Python: How to check whether a file exists using Python?

C#: Loops in c#

PHP: For each

SQL: JOIN using more than 5 tables

JavaScript: Blank Slide in Javascript Slideshow