/*************************************************************************************
**************************************************************************************
************************ unix timestamp to time string [begin] ***********************
**************************************************************************************
***************************************************************************************/
#define UTC_BASE_YEAR 1970
#define MONTH_PER_YEAR 12
#define DAY_PER_YEAR 365
#define SEC_PER_DAY 86400
#define SEC_PER_HOUR 3600
#define SEC_PER_MIN 60
/*每个月的天数*/
const unsigned char g_day_per_mon[MONTH_PER_YEAR] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};/** 功能:
* 判断是否是闰年
* 参数:
* year:需要判断的年份数
*
* 返回值:
* 闰年返回1,否则返回0*/
static unsigned char applib_dt_is_leap_year(unsigned shortyear)
{if ((year % 400) == 0) {return 1;
}else if ((year % 100) == 0) {return 0;
}else if ((year % 4) == 0) {return 1;
}else{return 0;
}
}/** 功能:
* 得到每个月有多少天
* 参数:
* month:需要得到天数的月份数
* year:该月所对应的年份数
*
* 返回值:
* 该月有多少天
**/
static unsigned char applib_dt_last_day_of_mon(unsigned char month, unsigned shortyear)
{if ((month == 0) || (month > 12)) {return g_day_per_mon[1] +applib_dt_is_leap_year(year);
}if (month != 2) {return g_day_per_mon[month – 1];
}else{return g_day_per_mon[1] +applib_dt_is_leap_year(year);
}
}static void change(longts)
{int year = 0;int month = 0;int day = 0;int hour = 0;int minute = 0;int second = 0;//将时间戳值转化成天数。通过天数可以比较方便地算出年、月、日。
int days = ts /SEC_PER_DAY;//这个时间戳值的年。
int yearTmp = 0;int dayTmp = 0;//使用夹逼法计算 days 天中包含的年数。
for (yearTmp = UTC_BASE_YEAR; days > 0; yearTmp++) {
dayTmp= (DAY_PER_YEAR + applib_dt_is_leap_year(yearTmp)); //这一年有多少天?
if (days >= dayTmp) //条件成立,则 yearTmp 即是这个时间戳值所代表的年数。
{
days-=dayTmp;
}else{break;
}
}
year=yearTmp;//这个时间戳值的月
int monthTmp = 0;for (monthTmp = 1; monthTmp < MONTH_PER_YEAR; monthTmp++) {
dayTmp=applib_dt_last_day_of_mon(monthTmp, year);if (days >=dayTmp) {
days-=dayTmp;
}else{break;
}
}
month=monthTmp;
day= days + 1;//转化成秒。
int secs = ts %SEC_PER_DAY;//这个时间戳值的小时数。
hour = secs /SEC_PER_HOUR;//这个时间戳值的分钟数。
secs %=SEC_PER_HOUR;
minute= secs /SEC_PER_MIN;//这个时间戳的秒钟数。
second = secs %SEC_PER_MIN;printf(“%d-%d-%d %d:%d:%d\n\n”, year, month, day, hour, minute, second);
}voidmain()
{
change(1558443421);
}/*************************************************************************************
**************************************************************************************
************************ unix timestamp to time string [ end ] ***********************
**************************************************************************************
***************************************************************************************/
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/189539.html原文链接:https://javaforall.cn