Linux下擷取目前時間到1970年之前某年份的秒數的C代碼實現

來源:互聯網
上載者:User

標籤:

一、問題描述
在Linux下編寫一C程式,用於擷取目前時間到1970年之前某年份的秒數。

二、C代碼實現

/********************************************************************** 著作權 (C)2015, Zhou Zhaoxiong。** 檔案名稱:GetSecNumBetweenTwoYear.c* 檔案標識:無* 內容摘要:擷取目前時間到1970年之前某時間的秒數* 其它說明:無* 目前的版本:V1.0* 作    者:Zhou Zhaoxiong* 完成日期: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();/********************************************************************** 功能描述:主函數* 輸入參數:無* 輸出參數:無* 返 回 值:無* 其它說明:無* 修改日期         版本號碼       修改人            修改內容* -------------------------------------------------------------------* 20150211        V1.0     Zhou Zhaoxiong        建立*********************************************************************/INT32 main(){    UINT32 iYearBefore1970 = 0;    UINT32 iSecondsTo1970  = 0;    UINT32 iTotalSeconds   = 0;    // 先輸入一個1970年之前的年份    printf("Please input a year before 1970: \n");    scanf("%d", &iYearBefore1970);    if (iYearBefore1970 >= 1970)    // 確保輸入時間小於1970    {        printf("The year %d isn‘t less than 1970, please check!\n", iYearBefore1970);        return -1;    }    // 計算輸入年份到1970年的秒數    iSecondsTo1970 = GetSecNumBetweenTwoYear(iYearBefore1970, 1970);    printf("The total seconds from %d to 1970 is: %u\n", iYearBefore1970, iSecondsTo1970);    // 計算總的秒數    iTotalSeconds = (UINT32)(time(0) + (time_t)iSecondsTo1970);    printf("The total seconds from %d to now is: %u\n", iYearBefore1970, iTotalSeconds);    return 0;                  // main函數返回0}/*********************************************************************** 功能描述: 計算任意兩年之間的秒數* 輸入參數: iFirstYear-減數            iSecondYear-被減數* 輸出參數: 無* 返 回 值: 秒數* 其它說明: 無* 修改日期         版本號碼          修改人          修改內容* --------------------------------------------------------------------* 20150211         V1.0      Zhou Zhaoxiong        建立*********************************************************************/UINT32 GetSecNumBetweenTwoYear(UINT16 iFirstYear, UINT16 iSecondYear){    UINT32 iTotalDays = 0;      // 兩年之間的總天數    UINT16 iTmpYear   = 0;      // 用作臨時存放中間資料    // 輸入參數檢查, 保證減數小於被減數    if (iFirstYear > iSecondYear)     {        iTmpYear    = iFirstYear;        iFirstYear  = iSecondYear;        iSecondYear = iTmpYear;    }    // 計算總天數    iTotalDays = 0;    for (iTmpYear = iFirstYear; iTmpYear < iSecondYear; iTmpYear ++)    {        // 判斷該年天數:1-能被4整除,但不能被100整除的是閏年; 2-能被400整除的是閏年        if ((0 == (iTmpYear%4) && 0 != (iTmpYear%100)) ||              0 == (iTmpYear%400))        {            iTotalDays += 366;    // 閏年        }        else        {            iTotalDays += 365;    // 平年        }    }    return iTotalDays * 86400;    // 總的秒數}

三、makefile檔案內容

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

四、程式說明
(1) 在Linux下,有一個函數time使用者擷取時間,但時間的起點是從1970年開始的,因此本程式擷取的時間分為兩段:輸入時間到1970年的時間和1970年到當前的時間。輸入時間到1970年的時間用自編的函數擷取,1970年到當前的時間用time(0)擷取。
(2) 列印擷取到的秒數的時候,要用“%u”格式;如果採用“%d”格式,則會出現整型值溢出的情況,列印出負值。

五、程式運行結果
運行“make”命令之後,轉到“release”目錄下,執行“GetSecNumBetweenTwoYear”,結果如下:
(1) 正常用例

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) 異常用例

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

本人公眾號:zhouzxi,請掃描以下二維碼:

Linux下擷取目前時間到1970年之前某年份的秒數的C代碼實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.