In Linux, get the C code implementation of the number of seconds in a year from the current time to before January 1, 1970. The current time is January 1, 1970.

Source: Internet
Author: User

In Linux, get the C code implementation of the number of seconds in a year from the current time to before January 1, 1970. The current time is January 1, 1970.

I. Problem Description
Write a C program in Linux to obtain the number of seconds from the current time to a year before January 1, 1970.

II. C code implementation

/*************************************** * ****************************** All Rights Reserved (C) 2015, Zhou Zhaoxiong. ** File name: GetSecNumBetweenTwoYear. c * File ID: none * Content Abstract: obtain the number of seconds from the current time to a certain time before January 1, 1970 * Other Description: none * Current version: V1.0 * OPERATOR: Zhou Zhaoxiong * completion date: 20150211 *************************************** * ******************************/# include <stdio. h> # include <stdlib. h> # include <string. h> typedef unsigned short int UINT16; typedef signed int INT32; typedef unsigned int UINT32; UINT32 GetSecNumBetweenTwoYear (UINT16 iFirstYear, UINT16 iSecondY Ear); INT32 main (); /*************************************** * ****************************** function description: main function * input parameter: none * output parameter: none * return value: none * Other description: no * modified date version number modifier modified content * found * 20150211 V1.0 Zhou Zhaoxiong create *********************** **************************************** * *****/INT32 main () {UINT32 iYearBefore1970 = 0; UINT32 iSecondsTo1970 = 0; UINT32 iT OtalSeconds = 0; // enter the year printf before January 1, 1970 ("Please input a year before 1970: \ n"); scanf ("% d", & iYearBefore1970 ); if (iYearBefore1970> = 1970) // make sure that The input time is less than 1970 {printf ("The year % d isn' t less than 1970, please check! \ N ", iYearBefore1970); return-1 ;}// calculate the number of seconds from the input year to January 1, 1970 iSecondsTo1970 = GetSecNumBetweenTwoYear (iYearBefore1970, 1970 ); printf ("The total seconds from % d to 1970 is: % u \ n", iYearBefore1970, iSecondsTo1970); // calculate The total number of seconds iTotalSeconds = (UINT32) (time (0) + (time_t) iSecondsTo1970); printf ("The total seconds from % d to now is: % u \ n", iYearBefore1970, iTotalSeconds); return 0; // The main function returns 0 }/*************** **************************************** * **************** Function description: calculate the number of seconds between any two years * input parameter: iFirstYear- iSecondYear- * output parameter: none * return value: Second * Other description: no * modified date version number modifier modified content * found * 20150211 V1.0 Zhou Zhaoxiong create *********************** **************************************** * *****/UINT32 GetSecNumBetweenTwoYear (UINT16 iFirstYear, UINT16 iSecondYear) {UINT32 iTotalDays = 0; // The total number of days between two years UINT16 iTmpYear = 0; // used for temporary storage of intermediate data // enter the parameter check, make sure that the subtraction is less than the subtrahend if (iFirstYear> iSecondYear) {iTmpYear = iFirstYear; iFirstYear = iSecondYear; iSecondYear = iTmpYear;} // calculate the total number of days iTotalDays = 0;; iTmpYear <iSecondYear; iTmpYear ++) {// determines the number of days of the year: 1-a leap year can be divisible by 4, but cannot be divisible by 100; 2-what can be divisible by 400 is the leap year if (0 = (iTmpYear % 4) & 0! = (ITmpYear % 100) | 0 = (iTmpYear % 400) {iTotalDays + = 366; // leap year} else {iTotalDays + = 365; // annually} return iTotalDays * 86400; // The total number of seconds}

Iii. makefile content

GetSecNumBetweenTwoYear : GetSecNumBetweenTwoYear.c    gcc -c -g GetSecNumBetweenTwoYear.c    gcc -g -o release/GetSecNumBetweenTwoYear GetSecNumBetweenTwoYear.o    rm *.o

Iv. program description
(1) in Linux, there is a function time that the user obtains, but the start point of the time is from January 1, 1970. Therefore, the time obtained by this program is divided into two sections: enter the time from January 1, to the current time. Use the self-developed function to obtain the input time from January 1, 1970, and use time (0) to obtain the input time from January 1, 1970 to the current time.
(2) When printing the number of seconds obtained, it must be in the "% u" format. If it is in the "% d" format, the integer value overflows and a negative value is printed.

5. program running result
After running the "make" command, go to the "release" directory and run "GetSecNumBetweenTwoYear". The result is as follows:
(1) normal use case

release> GetSecNumBetweenTwoYearPlease input a year before 1970: 1900The total seconds from 1900 to 1970 is: 2208988800The total seconds from 1900 to now is: 3632613008release> GetSecNumBetweenTwoYearPlease input a year before 1970: 1910The total seconds from 1910 to 1970 is: 1893456000The total seconds from 1910 to now is: 3317080221

(2) exception cases

release> GetSecNumBetweenTwoYearPlease input a year before 1970: 1990The year 1990 isn't less than 1970, please check!

My public account: zhouzxi. Please scan the following QR code:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.