C Tutorial – Number of Seconds in Decade and Visa-Versa

In a previous C language tutorial post the user “nikko manlangit” asked the following question: “How to convert seconds to decade or vice-versa?”. This C tutorial make use of time.h functions to answer his question.

Short Answer

It all depends on how accurate you want to calculate the numbers?

You can calculate with the following numbers:
A day has 24 hours x 60 minutes x 60 seconds = 86,400 seconds per day.
So a year has about 365 x 86,400 = 31,536,000 seconds
Decade has 10 years, so previous number times ten.

Or you take into account leap years and leap seconds (more accurate):
An easy way to do this is using the tm structure (time.h).

Convert Decade to Seconds

Because we want a real world example we use the tm structure of time.h. Take a look at the following example:


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

int main(void) {
	
	time_t start_time;
	time_t end_time;
	time_t convert_back_example1;
	time_t convert_back_example2;
	struct tm start_str_time;
	struct tm end_str_time;
	
	// Fill our starting structure
	start_str_time.tm_sec = 0;
	start_str_time.tm_min = 0;
	start_str_time.tm_hour = 0;
	start_str_time.tm_mday = 1;
	start_str_time.tm_mon = 0;
	start_str_time.tm_year = 2010-1900;
	start_str_time.tm_isdst = 0;

        // Fill our end structure
	end_str_time.tm_sec = 61;
	end_str_time published here.tm_min = 59;
	end_str_time.tm_hour = 23;
	end_str_time.tm_mday = 31;
	end_str_time.tm_mon = 11;
	end_str_time.tm_year = 2019-1900;
	end_str_time.tm_isdst = 0;
	
	start_time = mktime(&start_str_time);
	end_time = mktime(&end_str_time);
	
	printf("The difference was %f seconds.\n", difftime(end_time, start_time));
	
	// - Seconds to date/time examples
        // Example one
	start_str_time.tm_sec += difftime(end_time, start_time);
	convert_back_example1 = mktime(&start_str_time);
	printf("%s", asctime(localtime(&convert_back_example1)));

	// Example two shows string to date / time conversion, since epoch time.
	const char diff_time[] = "315532801";
	// Ignore milliseconds
	convert_back_example2 = atoi(diff_time);
	printf("Since epoch time: %s", asctime(localtime(&convert_back_example2)));

	return 0;
}

The program starts with some variable declaration of time_t and structures tm types. Then the first tm structure is filled with a date in the beginning of a year. The second tm structure is filled with a date that is a decade later and is at the end of that year. Then function mktime() used used to fix the broken-down time values, so we get a time_t type. The last call of the first part is the function difftime(), that returns the difference between two time_t values in seconds. So that’s the first part done. We have now broken down a decade into seconds. (And leap years and leap seconds are taken into account.)

Seconds to Date/Time Example

The second part of the program convert the seconds back into a readable date/time string. In example one we use the start time tm structure and add the seconds to .tm_sec. Then the mktime() function is used again to create a time_t type. We then use the asctime() and localtime() functions to print the date/time of newly calculated end time (a decade later).

Seconds to Date/Time Example since Epoch

In the second example we input a string representing the number of seconds in a decade. But this time we want to calculate using the epoch time. So we use atoi() function to convert the into an integer. Then again we use asctime() and localtime() functions to print the date/time of newly calculated end time (a decade later) from epoch.

That’s all for this tutorial. As you can see you can use the time_t and tm structure to get a more accurate example when you want to do conversions.

This entry was posted in C Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed. Tweet This! Tweet This! or use to share this post with others.

Comments are closed.