Time stamp and date time are mutually transferred to C language

Source: Internet
Author: User

/* * ctime.h * *  created On:may, 2016 *      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??????????  *///#define Offset_second   0          /*?? 2000/1/1/0/0/0?? 2000/1/1/0/0/0??????????  */#define Second_of_day     86400      /* 1???????                          ,         &NB Sp */ typedef struct date_time{    uint16   iyear;    uint16   imon;    UInt16 & nbsp iday;     uint16   ihour;    uint16   imin;    uint16   isec;  &nbs P UInt16   Imsec;} Date_time; void Getdatetimefromsecond (unsigned long lsec, date_time *ttime); #endif/* Ctime_h_ */ 

/* * ctime.c * *  created On:may, 2016 *      author:root */#include "c Ommon/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;  &nbsp ; 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++)   & nbsp /* ??? 1970???????????????? */    {        if (((i%4==0) && (i%100!=0)) | | (i%400==0))  Cyear++;   }    countday = cyear * 366 + (iyear-1970-cyear) * 365;    for (I=1; i<i Mon; i++)     {        if ((i==2) && (((iyear%4==0) && (iyear%100!=0)) | | (iyear%400==0)))             Countday + = 29;        else        &NBSP ;   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?????? */void Getdatetimefromsecond (unsigned long lsec, date_time *ttime) {    uint16 i,j,iday;    unsigned l Ong lday;     lday = Lsec/second_of_day;        /*???????????? */    lsec = 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)))  /*??? */    {      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];  &nbsp ;   if (lday >= iday) Lday-= iday;      Else break;   }    Ttime->imon   = j+1;    Ttime->iday  = lday+1;    Ttime->ihour = ((lsec/3600) +8)%24;//Here Note that world time has been added to Beijing when Difference between 8,    ttime->imin  = (lsec% 3600)/60;    ttime->isec  = (lsec% 3600)% 60;}

Time stamp and datetime to C language

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.