程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> C說話完成時光戳轉日期的算法(推舉)

C說話完成時光戳轉日期的算法(推舉)

編輯:關於C++

C說話完成時光戳轉日期的算法(推舉)。本站提示廣大學習愛好者:(C說話完成時光戳轉日期的算法(推舉))文章只能為提供參考,不一定能成為您想要的結果。以下是C說話完成時光戳轉日期的算法(推舉)正文


1、算法

時光是有周期紀律的,4年一個周期(閏年、閏年、閏年、閏年)合計1461天。Windows上C庫函數time(NULL)前往的是從1970年1月1日以來的毫秒數,我們最初算出來的年數必定要加上這個基數1970。總的天數除以1461便可以曉得閱歷了若干個周期;總的天數對1461取余數便可以曉得殘剩的缺乏一個周期的天數,對這個余數停止斷定也便可以獲得月份和日了。

固然了,C說話庫函數:localtime便可以取得一個時光戳對應的詳細日期了,這裡 重要說的是完成的一種算法。

2、C說話代碼完成

int nTime = time(NULL);//獲得以後體系時光
int nDays = nTime/DAYMS + 1;//time函數獲得的是從1970年以來的毫秒數,是以須要先獲得天數
int nYear4 = nDays/FOURYEARS;//獲得從1970年以來的周期(4年)的次數
int nRemain = nDays%FOURYEARS;//獲得缺乏一個周期的天數
int nDesYear = 1970 + nYear4*4;
int nDesMonth = 0, nDesDay = 0;
bool bLeapYear = false;
if ( nRemain<365 )//一個周期內,第一年
{//閏年

}
else if ( nRemain<(365+365) )//一個周期內,第二年
{//閏年
nDesYear += 1;
nRemain -= 365;
}
else if ( nRemain<(365+365+365) )//一個周期內,第三年
{//閏年
nDesYear += 2;
nRemain -= (365+365);
}
else//一個周期內,第四年,這一年是閏年
{//潤年
nDesYear += 3;
nRemain -= (365+365+365);
bLeapYear = true;
}
GetMonthAndDay(nRemain, nDesMonth, nDesDay, bLeapYear);

盤算月份和日期的函數:

static const int MON1[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};	//閏年
static const int MON2[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};	//閏年
static const int FOURYEARS = (366 + 365 +365 +365);	//每一個四年的總天數
static const int DAYMS = 24*3600;	//天天的毫秒數

void GetMonthAndDay(int nDays, int& nMonth, int& nDay, bool IsLeapYear)
{
	int *pMonths = IsLeapYear?MON2:MON1;
	//輪回減去12個月中每一個月的天數,直到殘剩天數小於等於0,就找到了對應的月份
	for ( int i=0; i<12; ++i )
	{
		int nTemp = nDays - pMonths[i];
		if ( nTemp<=0 )
		{
			nMonth = i+1;
			if ( nTemp == 0 )//表現恰好是這個月的最初一天,那末天數就是這個月的總天數了
				nDay = pMonths[i];
			else
				nDay = nDays;
			break;
		}
		nDays = nTemp;
	}
}

3、附上C說話庫函數的完成

<pre name="code" class="cpp">/***
*errno_t _gmtime32_s(ptm, timp) - convert *timp to a structure (UTC)
*
*Purpose:
*    Converts the calendar time value, in 32 bit internal format, to
*    broken-down time (tm structure) with the corresponding UTC time.
*
*Entry:
*    const time_t *timp - pointer to time_t value to convert
*
*Exit:
*    errno_t = 0 success
* tm members filled-in
*    errno_t = non zero
* tm members initialized to -1 if ptm != NULL
*
*Exceptions:
*
*******************************************************************************/

errno_t __cdecl _gmtime32_s (
struct tm *ptm,
const __time32_t *timp
)
{
__time32_t caltim;/* = *timp; *//* calendar time to convert */
int islpyr = 0; /* is-current-year-a-leap-year flag */
REG1 int tmptim;
REG3 int *mdays;/* pointer to days or lpdays */
struct tm *ptb = ptm;

_VALIDATE_RETURN_ERRCODE( ( ptm != NULL ), EINVAL )
memset( ptm, 0xff, sizeof( struct tm ) );

_VALIDATE_RETURN_ERRCODE( ( timp != NULL ), EINVAL )

caltim = *timp;
_VALIDATE_RETURN_ERRCODE_NOEXC( ( caltim >= _MIN_LOCAL_TIME ), EINVAL )

/*
 * Determine years since 1970. First, identify the four-year interval
 * since this makes handling leap-years easy (note that 2000 IS a
 * leap year and 2100 is out-of-range).
 */
tmptim = (int)(caltim / _FOUR_YEAR_SEC);
caltim -= ((__time32_t)tmptim * _FOUR_YEAR_SEC);

/*
 * Determine which year of the interval
 */
tmptim = (tmptim * 4) + 70; /* 1970, 1974, 1978,...,etc. */

if ( caltim >= _YEAR_SEC ) {

  tmptim++;    /* 1971, 1975, 1979,...,etc. */
  caltim -= _YEAR_SEC;

  if ( caltim >= _YEAR_SEC ) {

tmptim++;  /* 1972, 1976, 1980,...,etc. */
caltim -= _YEAR_SEC;

/*
 * Note, it takes 366 days-worth of seconds to get past a leap
 * year.
 */
if ( caltim >= (_YEAR_SEC + _DAY_SEC) ) {

tmptim++;  /* 1973, 1977, 1981,...,etc. */
caltim -= (_YEAR_SEC + _DAY_SEC);
}
else {
/*
 * In a leap year after all, set the flag.
 */
islpyr++;
}
  }
}

/*
 * tmptim now holds the value for tm_year. caltim now holds the
 * number of elapsed seconds since the beginning of that year.
 */
ptb->tm_year = tmptim;

/*
 * Determine days since January 1 (0 - 365). This is the tm_yday value.
 * Leave caltim with number of elapsed seconds in that day.
 */
ptb->tm_yday = (int)(caltim / _DAY_SEC);
caltim -= (__time32_t)(ptb->tm_yday) * _DAY_SEC;

/*
 * Determine months since January (0 - 11) and day of month (1 - 31)
 */
if ( islpyr )
  mdays = _lpdays;
else
  mdays = _days;


for ( tmptim = 1 ; mdays[tmptim] < ptb->tm_yday ; tmptim++ ) ;

ptb->tm_mon = --tmptim;

ptb->tm_mday = ptb->tm_yday - mdays[tmptim];

/*
 * Determine days since Sunday (0 - 6)
 */
ptb->tm_wday = ((int)(*timp / _DAY_SEC) + _BASE_DOW) % 7;

/*
 * Determine hours since midnight (0 - 23), minutes after the hour
 * (0 - 59), and seconds after the minute (0 - 59).
 */
ptb->tm_hour = (int)(caltim / 3600);
caltim -= (__time32_t)ptb->tm_hour * 3600L;

ptb->tm_min = (int)(caltim / 60);
ptb->tm_sec = (int)(caltim - (ptb->tm_min) * 60);

ptb->tm_isdst = 0;
return 0;

}

以上這篇C說話完成時光戳轉日期的算法(推舉)就是小編分享給年夜家的全體內容了,願望能給年夜家一個參考,也願望年夜家多多支撐。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved