admin 管理员组文章数量: 887021
2024年1月19日发(作者:sql语句创建视图)
static int FOURYEARS = 365*3+366;static int norMoth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};static int leapMoth[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};void getHourMinSec(int nSecond){ int nHour = nSecond/3600; int nMin = (nSecond%3600)/60; int nSec = (nSecond%3600)%60; printf("%d:%d:%dn", nHour+8, nMin, nSec);}void getMothAndDay(bool bLeapYear, int nDays, int *nMoth, int *nDay){ int i = 0; int nTmp = 0; int *pMoth = bLeapYear?leapMoth:norMoth; for (i=0; i<12; i++) { nTmp = nDays-pMoth[i]; if (nTmp <= 0) { *nMoth = i+1; if (nTmp == 0) { *nDay = pMoth[i]; } else { *nDay = nDays; } break; } nDays = nTmp; } return;}void print_time(){ time_t nTime = time(NULL); int nDays = nTime/DAYS + ((nTime%DAYS)?1:0); int nYear4 = nDays/FOURYEARS; int nRemain = nDays%FOURYEARS; int nDecyear = 1970 + nYear4*4; int nDecmoth = 0;
int nDecday = 0; bool bLeapyear = false; if (nRemain < 365) { ; } else if (nRemain < 365*2) { nDecyear += 1; nRemain -= 365; } else if (nRemain < 365*3) { nDecyear += 2; nRemain -= 365*2; } else { nDecyear += 3; nRemain -= 365*3; bLeapyear = true; } getMothAndDay(bLeapyear, nRemain, &nDecmoth, &nDecday); printf("%d:%d:%dn", nDecyear, nDecmoth, nDecday); getHourMinSec(nTime%DAYS); return;}int main(void){print_time();return 0;
}
版权声明:本文标题:C语言实现时间戳转日期的算法 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.freenas.com.cn/free/1705599141h491844.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论