示例#1
0
文件: time.c 项目: Nirlendu/mona
struct tm *localtime(const time_t *timep) {
  static struct tm ret;
  time_t now = *timep;

  int year = 1970;
  unsigned long dayclock = (unsigned long) now % SECONDS_DAY;
  unsigned long dayno = (unsigned long) now / SECONDS_DAY;

  ret.tm_sec = dayclock % 60;
  ret.tm_min = (dayclock % 3600) / 60;
  ret.tm_hour = dayclock / 3600;
  ret.tm_wday = (dayno + 4) % 7;
  while (dayno >= (unsigned long) DAYS_YEAR(year))  {
    dayno -= DAYS_YEAR(year);
    year++;
  }
  ret.tm_year = year - 1900;
  ret.tm_yday = dayno;
  ret.tm_mon = 0;
  while (dayno >= (unsigned long) _num_days[IS_LEAPYEAR(year)][ret.tm_mon]) {
    dayno -= _num_days[IS_LEAPYEAR(year)][ret.tm_mon];
    ret.tm_mon++;
  }
  ret.tm_mday = dayno + 1;
  ret.tm_isdst = 0;
  return &ret;
}
示例#2
0
/*
 * LPLD_DateTimeToTimeStamp
 * 将时间结构体转换为秒
 * 以IBM时间标准为基础
 * 
 * 参数:
 *    TimeStamp_FormatTypeDef* realtime_init_struct
 *                        具体定义见TimeStamp_FormatTypeDef
 *
 * 输出:
 *    返回值为秒,可以做为RTC秒计数器的值
 *    返回零表示错误
 *
 */
uint32 LPLD_DateTimeToTimeStamp(TimeStamp_FormatTypeDef *realtime_init_struct)
{
  uint16 i; 
  uint32 current_second = 0;
  uint16 current_year = realtime_init_struct->year;
  uint8 current_month = realtime_init_struct->month;
  uint8 current_day = realtime_init_struct->day;
  uint8 current_hour = realtime_init_struct->hour;
  uint8 current_minute = realtime_init_struct->minute;
  //判断时间起点终端是否越界
  if(current_year < IBM_YEAR_START || current_year > IBM_YEAR_END)
  {
    return 0;
  }
  //将年转化为秒
  for (i = IBM_YEAR_START; i < current_year; i++)
  {
    if(IS_LEAPYEAR(i))
    {
        current_second += LEAPYEAR_CONVERTTO_SECONEDS;
    }
    else
    {
        current_second += AVERAGEYEAR_CONVERTTO_SECONEDS;
    }
  }
  //将月转化为秒
  for ( i = 0; i < (current_month - 1); i++)
  {
    current_second += (uint32)month_table[i] * DAY_CONVERTTO_SECONEDS;
    if(IS_LEAPYEAR(current_year) && i == FEBRUARY)
    {
        current_second += DAY_CONVERTTO_SECONEDS;
    }
  }
  //将天,小时,分转化为秒
  current_second += (uint32)(current_day - 1) * DAY_CONVERTTO_SECONEDS;
  current_second += (uint32)current_hour * HOUR_CONVERTTO_SECONEDS;
  current_second += (uint32)current_minute * MINUTE_CONVERTTO_SECONEDS;
  current_second += realtime_init_struct->second;   
  return current_second;   
}
示例#3
0
/*
 * LPLD_TimeStampToDateTime
 * 将秒转换为时间结构体
 * 以IBM时间标准为基础
 * 
 * 参数:
 *    uint32 -- rtc_second  RTC秒计数器的值
 *    TimeStamp_FormatTypeDef* realtime_init_struct
 *                        具体定义见TimeStamp_FormatTypeDef
 *
 * 输出:
 *    返回值为秒,可以做为RTC秒计数器的值
 *    返回1表示正确
 *
 */
uint8 LPLD_TimeStampToDateTime(uint32 rtc_second, TimeStamp_FormatTypeDef *realtime_init_struct)
{
  static uint32 day_counter = 0;
  uint32 days = 0;
  uint32 i = 0;
  days = rtc_second / DAY_CONVERTTO_SECONEDS;//second to days
  if(day_counter != days)
  {
    day_counter = days;
    i = IBM_YEAR_START;
    //days to years
    while(days >= AVERAGEYEAR_DAYS)
    {
      if(IS_LEAPYEAR(i)) 
      {
        if(days >= LEAPYEAR_DAYS)
        {
            days-= LEAPYEAR_DAYS;
        }   
        else 
        {
            i++;
            break;
        }   
      }
      else 
      {
         days -= AVERAGEYEAR_DAYS; 
      }
      i++;  
    }
    realtime_init_struct->year = i;
    i = 0;
    //days to month
    while(days >= AVERAGEYEAR_FEBRUARY_DAYS )
    {
      if(IS_LEAPYEAR(realtime_init_struct->year) && i == FEBRUARY )
      {
        if(days >= LEAPYEAR_FEBRUARY_DAYS) 
        {
          days -= LEAPYEAR_FEBRUARY_DAYS;
        }
        else break; 
      }
      else 
      {
        if(days >= month_table[i]) 
        {
          days -= month_table[i];
        }
        else break;
      }
      i++;  
    }
    realtime_init_struct->month = i+1;
    realtime_init_struct->day = days+1;
  }
  i = rtc_second % DAY_CONVERTTO_SECONEDS;     
  realtime_init_struct->week = LPLD_GetWeek(realtime_init_struct);         
  realtime_init_struct->hour = i / HOUR_CONVERTTO_SECONEDS;        //hours
  realtime_init_struct->minute = (i % HOUR_CONVERTTO_SECONEDS) / MINUTE_CONVERTTO_SECONEDS; //minutes  
  realtime_init_struct->second = (i % HOUR_CONVERTTO_SECONEDS) % MINUTE_CONVERTTO_SECONEDS; //seconds
  return 1;
}