C Language Implementation time stamp date algorithm (recommended) _c language

Source: Internet
Author: User

1, algorithm

Time is a periodic rule, a period of 4 years (excepting, excepting, excepting, leap year) amounted to 1461 days. The C library function time (NULL) on Windows returns the number of milliseconds since January 1, 1970, and the number of years that we finally calculate must be added to this cardinality of 1970. The total number of days divided by 1461 can know how many cycles have gone through, the total number of days to 1461 to the remainder can know the remaining less than a period of days, the remainder can be judged by the month and day.

Of course, C language library function: LocalTime can get a time stamp corresponding to the specific date, here is mainly to implement an algorithm.

2, C language code implementation

int ntime = time (NULL);//Get current system times
int ndays = ntime/dayms + 1;//time function Gets the number of milliseconds since 1970, so you need to get the number of days
int NYEAR4 = NDa First ys/fouryears;//gets the number of cycles (4 years) since 1970
int nremain = ndays%fouryears;//gets less than one cycle of days
int ndesyear = 1970 + nyear4*4 ;
int ndesmonth = 0, ndesday = 0;
BOOL Bleapyear = false;
if (nremain<365)//a period, the first year
{//excepting

}
else if (nremain< (365+365))//a cycle, the second year
{//excepting
Ndesyear + 1;
Nremain-= 365;
}
else if (nremain< (365+365+365))//a period, the third year
{//excepting
Ndesyear + + 2;
Nremain-= (365+365);
}
else//a period, the fourth year, this year is a leap year
{//Run years
Ndesyear + + 3;
Nremain-= (365+365+365);
Bleapyear = true;
}
Getmonthandday (Nremain, Ndesmonth, Ndesday, bleapyear);

Functions to calculate month and date:

static const int MON1[12] = {To, n, a, a, to, to, be;	excepting
static const int MON2[12] = {To, n, a, a, to, to, to;	Leap year
static const int Fouryears = (366 + 365 +365 +365);	Total days of each four years
static const int dayms = 24*3600;	Number of milliseconds per day

void getmonthandday (int ndays, int& nmonth, int& nday, bool isleapyear)
{
	int *pmonths = is Leapyear? Mon2:mon1;
	The loop subtracts the number of days per month in 12 months until the remaining number is less than or equal to 0, and the corresponding month for
	(int i=0; i<12; ++i)
	{
		int ntemp = Ndays-pmonths[i];
   if (ntemp<=0)
		{
			nmonth = i+1;
			if (ntemp = = 0)//means just the last day of this month, then the number of days is the total number of days this month
				nday = pmonths[i];
			else
				nday = ndays;
			break;
		Ndays = ntemp;
	}

3, attached C language library function implementation

<pre name= "code" class= "CPP" >/*** *errno_t _gmtime32_s (PTM, Timp)-convert *timp to a structure (UTC) * *purpose: * Converts the Calendar time value, in the bit internal format, to * broken-down time (tm structure) with the Corresp
Onding UTC time. * *entry: * Const time_t *timp-pointer to time_t value to convert * *exit: * errno_t = 0 Success * TM members fill ed-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 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 1970. Identify the Four-year interval * Since this makes handling easy ("Note" "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's 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 and 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 this 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.
* * 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-&GT;TM_ISDST = 0;

return 0; }

Above this C language to achieve timestamp date algorithm (recommended) is a small series to share all the content, hope to give you a reference, but also hope that we support the cloud habitat community.

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.