关闭

时间转化

标签: datestruct
554人阅读 评论(0) 收藏 举报

typedef struct
{
    uint8   sec;        // secondsafter the minute - [0,59]
    uint8   min;        // minutesafter the hour - [0,59]
    uint8   hour;       // hours since midnight - [0,23]
}SCI_TIME_T;

typedef struct
{
    uint8   mday;       // day of the month - [1,31]
    uint8   mon;        // months  - [1,12]
    uint16  year;       // years [2000,2049]
    uint8   wday;       // days since Sunday - [0,6], (Sunday : 0, Monday : 1)
}SCI_DATE_T;

PUBLIC BOOLEAN TM_IsLeapYear(   // Return SCI_TRUE, if it is leap year;
                                // else, return SCI_FALSE.
    uint16  year                // The year to be judge
    )
{
 if (year % 400 == 0)
 {
  return SCI_TRUE;
 }
 else if (year % 100 == 0)
 {
  return SCI_FALSE;
 }
 else if (year % 4 == 0)
 {
  return SCI_TRUE;
 }
 else
 {
  return SCI_FALSE;
 }
}

PUBLIC void TM_GetTime(SCI_TM_T *realtime)
{
 SCI_TIME_T tmp_time;
 SCI_DATE_T tmp_date;
 
 TM_GetSysDate(&tmp_date);
 TM_GetSysTime(&tmp_time); 
 
 realtime->tm_sec    = tmp_time.sec;
 realtime->tm_min    = tmp_time.min;
 realtime->tm_hour   = tmp_time.hour;
 
 realtime->tm_mday   = tmp_date.mday;
 realtime->tm_mon    = tmp_date.mon;
 realtime->tm_year   = tmp_date.year;
 realtime->tm_wday   = tmp_date.wday;

    {
        uint32  i;
        uint16  *days_ptr;
       
        i = ((TM_IsLeapYear(tmp_date.year)) == SCI_TRUE) ? 1 : 0;
        days_ptr = (uint16  *)&(days_from_jan1[i][0]);
       
        realtime->tm_yday = *(days_ptr + tmp_date.mon - 1) + tmp_date.mday - 1;
    }
       
 realtime->tm_isdst  = 0;
}

U32 betweenNowAnd2000App(void)
{
 U32         numSecond;
 SCI_TM_T    sysTime;

 //得到当前系统日期时间
 TM_GetTime(&sysTime);

 numSecond =  (sysTime.tm_year-2000)*365;
 numSecond += (sysTime.tm_yday+(sysTime.tm_year-2000)/4);
 if (sysTime.tm_year > 2000)
   numSecond++;
 numSecond = (numSecond*24+sysTime.tm_hour)*60;
 numSecond = ((numSecond+sysTime.tm_min)*60+sysTime.tm_sec);

 return numSecond;
}


void recoverDateBySeconds(U32 seconds, GUI_TIME *time, GUI_DATE *date)
{
  const U8 dayOfMonth[12]= {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
 
  SCI_ASSERT(time&&date);
  time->mSecond  = (U8)(seconds%60);
  seconds /= 60;
  time->mMinute = (U8)(seconds%60);
  seconds = seconds/60;
  time->mHour = (U8)(seconds%24);
  seconds /= 24;
  date->mYear = 0;
  if (seconds > 365 )
  {
    for (; seconds>365; date->mYear++)
   seconds -=(365+(0x0==date->mYear%4));
  }
  if (seconds>58 && 0x0==date->mYear%4)
   seconds--;
  date->mYear = (U16)(date->mYear+2000);
  for (date->mMonth=0; seconds > dayOfMonth[date->mMonth]; date->mMonth++)
   seconds -= dayOfMonth[date->mMonth];
  date->mMonth++;
  date->mDay = (U8)(seconds+1);

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:15481次
    • 积分:309
    • 等级:
    • 排名:千里之外
    • 原创:14篇
    • 转载:2篇
    • 译文:0篇
    • 评论:0条
    文章分类