The time stamp and date time are converted into C language and the date C Language

Source: Internet
Author: User

The time stamp and date time are converted into C language and the date C Language
/** Ctime. h ** Created on: May 19,201 6 * Author: root */# ifndef CTIME_H _ # define CTIME_H _ # include "common/micro_type.h" # define OFFSET_SECOND 946684800/* 1970/1/1/0/0/0/2000/1/1/0/0/0/bytes ������ * // # define OFFSET_SECOND 0/* �� 2000/1/1/0/0/0 �� 2000/1/1/0/0/0 �� �� */# define SECOND_OF_DAY 86400/* 1 �� */typedef struct date_time {uint16 iYear; uint16 iMon; uint16 iDay; uint16 iHour; uint16 iMin; uint16 iSec; uint16 iMsec;} DATE_TIME; void GetDateTimeFromSecond (unsigned long lSec, DATE_TIME * tTime ); # endif/* CTIME_H _*/

/** Ctime. c ** Created on: May 19,201 6 * Author: root */# include "common/micro_type.h" # include "ctime. h "uint8 DayOfMon [12] = {31,28, 31,30, 31,30, 31,31, 30,31, 30,31 }; /* ���� 1970/1/1/0/0/0/��ʱ */unsigned long GetSecondTime (DATE_TIME * date_time) {uint16 iYear, iMon, iDay, iHour, iMin, iSec; iYear = date_time-> iYear; iMon = date_time-> iMon; iDay = date_time-> iDay; iHour = date_time-> iHour; iMin = Date_time-> iMin; iSec = date_time-> iSec; uint16 I, Cyear = 0; unsigned long CountDay = 0; for (I = 1970; I <iYear; I ++) /* When 1970 then �������� then ���� */{if (I % 4 = 0) & (I % 100! = 0) | (I % 400 = 0) Cyear ++;} CountDay = Cyear * 366 + (iYear-1970-Cyear) * 365; for (I = 1; I <iMon; I ++) {if (I = 2) & (iYear % 4 = 0) & (iYear % 100! = 0) | (iYear % 400 = 0) CountDay + = 29; else CountDay + = DayOfMon [I-1];} CountDay + = (iDay-1 ); countDay = CountDay * SECOND_OF_DAY + (unsigned long) iHour * 3600 + (unsigned long) iMin * 60 + iSec; return CountDay ;} /* ������������ 1970/1/1/0/0/0 ���� */void GetDateTimeFromSecond (unsigned long lSec, DATE_TIME * tTime) {uint16 I, j, iDay; unsigned long lDay; lDay = lSec/SECOND_OF_DAY;/* Second second = LSec % SECOND_OF_DAY; I = 1970; while (lDay> 365) {if (I % 4 = 0) & (I % 100! = 0) | (I % 400 = 0)/* �� */lDay-= 366; else lDay-= 365; I ++ ;} if (lDay = 365 )&&! (I % 4 = 0) & (I % 100! = 0) | (I % 400 = 0)/* Other �� */{lDay-= 365; I ++ ;} tTime-> iYear = I;/* �� */for (j = 0; j <12; j ++) /* ���� · ��/{if (j = 1) & (I % 4 = 0) & (I % 100! = 0) | (I % 400 = 0) iDay = 29; else iDay = DayOfMon [j]; if (lDay> = iDay) lDay-= iDay; else break;} tTime-> iMon = j + 1; tTime-> iDay = lDay + 1; tTime-> iHour = (lSec/3600) + 8) % 24; // note that the time difference between the world time and Beijing time is 8, tTime-> iMin = (lSec % 3600)/60; tTime-> iSec = (lSec % 3600) % 60 ;}

Related Article

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.