time convert

#include <stdio.h>
#include <math.h>
#include <stdlib.h>




#define LEAPS_THRU_END_OF(y) ((y)/4 - (y)/100 + (y)/400)


struct rtc_time {                                                                                                                                     
    int tm_sec;
    int tm_min;
    int tm_hour;
    int tm_mday;
    int tm_mon;
    int tm_year;
    int tm_wday;
    int tm_yday;
    int tm_isdst;
};
static const unsigned char rtc_days_in_month[] = {
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
};
    
 static const unsigned short rtc_ydays[2][13] = {
/* Normal years */
     { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },
        /* Leap years */
     { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }
  };


  static int is_leap_year(unsigned int year)                                                                                                 
  { return (!(year % 4) && (year % 100)) || !(year % 400);
   }
int rtc_year_days(unsigned int day, unsigned int month, unsigned int year)
 {
       return rtc_ydays[is_leap_year(year)][month] + day-1;
 }
 int rtc_month_days(unsigned int month, unsigned int year)
   {
      return rtc_days_in_month[month] + (is_leap_year(year) && month == 1);
   }


void rtc_time_to_tm(unsigned long time, struct rtc_time *tm)
{
unsigned int month, year;
int days;


days = time / 86400;
time -= (unsigned int) days * 86400;


/* day of the week, 1970-01-01 was a Thursday */
tm->tm_wday = (days + 4) % 7;


year = 1970 + days / 365;
days -= (year - 1970) * 365
+ LEAPS_THRU_END_OF(year - 1)
- LEAPS_THRU_END_OF(1970 - 1);
if (days < 0) {
year -= 1;
days += 365 + is_leap_year(year);
}
tm->tm_year = year - 1900;
tm->tm_yday = days + 1;


for (month = 0; month < 11; month++) {
int newdays;


newdays = days - rtc_month_days(month, year);
if (newdays < 0)
break;
days = newdays;
}
tm->tm_mon = month;
tm->tm_mday = days + 1;


tm->tm_hour = time / 3600;
time -= tm->tm_hour * 3600;
tm->tm_min = time / 60;
tm->tm_sec = time - tm->tm_min * 60;


tm->tm_isdst = 0;
}
unsigned long
mktime(const unsigned int year0, const unsigned int mon0,
       const unsigned int day, const unsigned int hour,
       const unsigned int min, const unsigned int sec)
{
    unsigned int mon = mon0, year = year0;                                                                                                            


    /* 1..12 -> 11,12,1..10 */
    if (0 >= (int) (mon -= 2)) {
        mon += 12;  /* Puts Feb last since it has leap day */
        year -= 1;
    }


    return ((((unsigned long)
          (year/4 - year/100 + year/400 + 367*mon/12 + day) +
          year*365 - 719499
        )*24 + hour /* now have hours */
      )*60 + min /* now have minutes */
    )*60 + sec; /* finally seconds */
}
int main(void)
{
unsigned long time = 1325462722;
struct rtc_time tm;
printf("enter %ld\n",time);
// rtc_time_to_tm(time, &tm);


tm.tm_year = 138;
tm.tm_mon = 0;
tm.tm_mday = 18;
tm.tm_hour = 10;
tm.tm_min = 14;
tm.tm_sec = 8;
printf("second\n");
printf("%d,%d,%d,%d,%d,%d\n", tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec);
printf("%ld\n", mktime(tm.tm_year+1900,tm.tm_mon+1,tm.tm_mday,tm.tm_hour,tm.tm_min,tm.tm_sec));
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值