如何在 Perl 中将十进制转换为 MK 时间。
我得到这个文件:
codes below are time decoding and encoding, for your reference
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 }
};
#define LEAPS_THRU_END_OF(y) ((y)/4 - (y)/100 + (y)/400)
#define LEAP_YEAR(year) ((!(year % 4) && (year % 100)) || !(year % 400))
u32 Mktime (u32 year, u32 mon, u32 day, u32 hour,u32 min, u32 sec)
{
if (0 >= (int) (mon -= 2)){ /**//* 1..12 -> 11,12,1..10 */
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
)*60 + min
)*60 + sec;
}
/*
* The number of days in the month.
*/
s16 rtc_month_days(u16 month, u16 year)
{
return rtc_days_in_month[month] + (LEAP_YEAR(year) && month == 1);
}
/*
* The number of days since January 1. (0 to 365)
*/
s16 rtc_year_days(u16 day, u16 month, u16 year)
{
return rtc_ydays[LEAP_YEAR(year)][month] + day-1;
}
void rtc_time_to_tm(u32 time, rtc_time *tm)
{
s16 days, month, year;
s16 newdays;
days = time / 86400;
time -= 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 + LEAP_YEAR(year);
}
tm->tm_year = year - 1900;
tm->tm_yday = days + 1;
for (month = 0; month < 11; month++) {
newdays = days - rtc_month_days(month, year);
if (newdays < 0)
break;
days = newdays;
}
tm->tm_mon = month + 1;
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_year += 1900;
}
u32 Pro_Mktime (u32 year, u32 mon, u32 day, u32 hour,u32 min, u32 sec)
{
u32 baseMk=0;
u32 mk=0;
u32 retMk=0;
baseMk=Mktime(2000,1,1,0,0,0);
mk=Mktime(year,mon,day, hour,min,sec);
retMk=mk-baseMk;
return retMk;
}
void Pro_Localtime (u32 mktime, rtc_time *tm)
{
struct tm *tblock;
u32 baseMk=0;
u32 mk=0;
baseMk=Mktime(2000,1,1,0,0,0);
mk=mktime+baseMk;
rtc_time_to_tm(mk,tm);
}
我需要在 perl 而不是 java 中继续它。
十进制 430208245 如何转换为 2000 年、第 1 个月、第 1 天、0 小时、1 天、0 小时、0 分钟、0 秒