First, the problem description
Write a C program under Linux that gets the number of seconds in a year before the current time until 1970.
Second, C code implementation
/********************************************************************** All rights reserved (C), Zhou Zhaoxiong.** File name: getsecnumbetweentwoyear.c* File ID: None* Content Summary: Gets the number of seconds before the current time until 1970* Other instructions: None* Current version: V1.0* Author: 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 isecondyear); INT32 main ();********************************************************************** Function Description: main function* Input parameters: None* Output parameters: None* return value: None* Other instructions: None* Modified Date version number modify the content of the person* -------------------------------------------------------------------* 20150211 V1.0 Zhou Zhaoxiong created*********************************************************************/int32 Main () {UINT32 iYearBefore1970 = 0;UINT32 iSecondsTo1970 = 0;UINT32 itotalseconds = 0;//Enter a year prior to 1970 yearsprintf ("Please input a year before 1970: \ n");scanf ("%d", &iyearbefore1970);if (iYearBefore1970 >= 1970)//Ensure 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 to enter the year to 1970iSecondsTo1970 = Getsecnumbetweentwoyear (iYearBefore1970, 1970);printf ("The total seconds from%d to 1970 are:%u\n", iYearBefore1970, iSecondsTo1970);//Calculate the total number of secondsitotalseconds = (UINT32) (Time (0) + (time_t) iSecondsTo1970);printf ("The total seconds from%d to today is:%u\n", iYearBefore1970, itotalseconds);return 0; The main function returns 0}/*********************************************************************** Function Description: Calculates the number of seconds between any two years* Input parameters: ifirstyear-isecondyear-is being subtracted* Output parameters: None* Return value: Number of seconds* Other instructions: None* Modified Date version number modify the content of the person* --------------------------------------------------------------------* 20150211 V1.0 Zhou Zhaoxiong created*********************************************************************/uint32 getsecnumbetweentwoyear (UINT16 ifirstyear, UINT16 isecondyear) {UINT32 itotaldays = 0; Total number of days between two yearsUINT16 itmpyear = 0; Used to temporarily store intermediate data//input parameter check to ensure that the meiosis is less than theif (Ifirstyear > Isecondyear) {itmpyear = ifirstyear;ifirstyear = isecondyear;isecondyear = itmpyear; }//Calculate total number of daysitotaldays = 0;For (itmpyear = ifirstyear; itmpyear < isecondyear; Itmpyear + +) {//Judge the number of days: 1-can be divisible by 4, but not divisible by 100 is a leap year, 2-can be divisible by 400 is a leap yearif ((0 = = (itmpyear%4) && 0! = (itmpyear%100)) | |0 = = (itmpyear%400)) {itotaldays + = 366; Leap Year }Else {itotaldays + = 365; Common year } }return itotaldays * 86400; The total number of seconds}
Iii. contents of makefile File
GetSecNumBetweenTwoYear : GetSecNumBetweenTwoYear.c gcc -c -g GetSecNumBetweenTwoYear.c gcc -g -o release/GetSecNumBetweenTwoYear GetSecNumBetweenTwoYear.o rm *.o
iv. Description of the procedure
(1) Under Linux, there is a function time the user gets the times, but the starting point of time is starting from 1970, so the time that the program obtains is divided into two parts: the time to enter the time to 1970 and 1970 to the current time. Input time to 1970 is obtained by using a self-compiled function, and 1970 to the current time is obtained by using times (0).
(2) When the number of seconds to print, to use the "%u" format, if the "%d" format, there will be an integer value overflow, the printing of negative values.
v. Results of program Operation
After running the "make" command, go to the "release" Directory and Execute "getsecnumbetweentwoyear" with the following results:
(1) Normal use cases
release> getsecnumbetweentwoyearplease InputaYearbefore 1970:1900The totalseconds from 1900 to 1970is:2208988800The totalseconds from 1900 toNow is:3632613008release> getsecnumbetweentwoyearplease InputaYearbefore 1970:1910The totalseconds from 1910 to 1970is:1893456000The totalseconds from 1910 toNow is:3317080221
(2) Exception use cases
yearbefore19701990year1990isn‘tless than1970, please check!
My public number: ZHOUZXI, please scan the following two-dimensional code:
C code implementation that gets the number of seconds in a year before the current time until 1970 under Linux