w3resource logo


C Programming Exercises

C Exercises: Convert a time_t object to calendar time expressed as local time

Secondary Nav

C Date Time: Exercise-7 with Solution

Write a program in C to convert a time_t object to calendar time expressed as local time.

Expected Output :

The calendar time expressed as a local Time is :                        
UTC:   Tue Sep  6 08:35:25 2016                                         
local: Tue Sep  6 14:05:25 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 a local 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 local 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 a local 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.

Help the community:

Python: Fizzbuzz

C++: Decimal to binary conversion

JavaScript: Need Help in JavaScript

Python: Help me with this program