C语言实现时间戳转日期的算法(推荐)
更新时间:2020年4月25日 17:35 点击:2609
1、算法
时间是有周期规律的,4年一个周期(平年、平年、平年、闰年)共计1461天。Windows上C库函数time(NULL)返回的是从1970年1月1日以来的毫秒数,我们最后算出来的年数一定要加上这个基数1970。总的天数除以1461就可以知道经历了多少个周期;总的天数对1461取余数就可以知道剩余的不足一个周期的天数,对这个余数进行判断也就可以得到月份和日了。
当然了,C语言库函数:localtime就可以获得一个时间戳对应的具体日期了,这里 主要说的是实现的一种算法。
2、C语言代码实现
int nTime = time(NULL);//得到当前系统时间 int nDays = nTime/DAYMS + 1;//time函数获取的是从1970年以来的毫秒数,因此需要先得到天数 int nYear4 = nDays/FOURYEARS;//得到从1970年以来的周期(4年)的次数 int nRemain = nDays%FOURYEARS;//得到不足一个周期的天数 int nDesYear = 1970 + nYear4*4; int nDesMonth = 0, nDesDay = 0; bool bLeapYear = false; if ( nRemain<365 )//一个周期内,第一年 {//平年 } else if ( nRemain<(365+365) )//一个周期内,第二年 {//平年 nDesYear += 1; nRemain -= 365; } else if ( nRemain<(365+365+365) )//一个周期内,第三年 {//平年 nDesYear += 2; nRemain -= (365+365); } else//一个周期内,第四年,这一年是闰年 {//润年 nDesYear += 3; nRemain -= (365+365+365); bLeapYear = true; } GetMonthAndDay(nRemain, nDesMonth, nDesDay, bLeapYear);
计算月份和日期的函数:
static const int MON1[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //平年 static const int MON2[12] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; //闰年 static const int FOURYEARS = (366 + 365 +365 +365); //每个四年的总天数 static const int DAYMS = 24*3600; //每天的毫秒数 void GetMonthAndDay(int nDays, int& nMonth, int& nDay, bool IsLeapYear) { int *pMonths = IsLeapYear?MON2:MON1; //循环减去12个月中每个月的天数,直到剩余天数小于等于0,就找到了对应的月份 for ( int i=0; i<12; ++i ) { int nTemp = nDays - pMonths[i]; if ( nTemp<=0 ) { nMonth = i+1; if ( nTemp == 0 )//表示刚好是这个月的最后一天,那么天数就是这个月的总天数了 nDay = pMonths[i]; else nDay = nDays; break; } nDays = nTemp; } }
3、附上C语言库函数的实现
<pre name="code" class="cpp">/*** *errno_t _gmtime32_s(ptm, timp) - convert *timp to a structure (UTC) * *Purpose: * Converts the calendar time value, in 32 bit internal format, to * broken-down time (tm structure) with the corresponding UTC time. * *Entry: * const time_t *timp - pointer to time_t value to convert * *Exit: * errno_t = 0 success * tm members filled-in * errno_t = non zero * tm members initialized to -1 if ptm != NULL * *Exceptions: * *******************************************************************************/ errno_t __cdecl _gmtime32_s ( struct tm *ptm, const __time32_t *timp ) { __time32_t caltim;/* = *timp; *//* calendar time to convert */ int islpyr = 0; /* is-current-year-a-leap-year flag */ REG1 int tmptim; REG3 int *mdays;/* pointer to days or lpdays */ struct tm *ptb = ptm; _VALIDATE_RETURN_ERRCODE( ( ptm != NULL ), EINVAL ) memset( ptm, 0xff, sizeof( struct tm ) ); _VALIDATE_RETURN_ERRCODE( ( timp != NULL ), EINVAL ) caltim = *timp; _VALIDATE_RETURN_ERRCODE_NOEXC( ( caltim >= _MIN_LOCAL_TIME ), EINVAL ) /* * Determine years since 1970. First, identify the four-year interval * since this makes handling leap-years easy (note that 2000 IS a * leap year and 2100 is out-of-range). */ tmptim = (int)(caltim / _FOUR_YEAR_SEC); caltim -= ((__time32_t)tmptim * _FOUR_YEAR_SEC); /* * Determine which year of the interval */ tmptim = (tmptim * 4) + 70; /* 1970, 1974, 1978,...,etc. */ if ( caltim >= _YEAR_SEC ) { tmptim++; /* 1971, 1975, 1979,...,etc. */ caltim -= _YEAR_SEC; if ( caltim >= _YEAR_SEC ) { tmptim++; /* 1972, 1976, 1980,...,etc. */ caltim -= _YEAR_SEC; /* * Note, it takes 366 days-worth of seconds to get past a leap * year. */ if ( caltim >= (_YEAR_SEC + _DAY_SEC) ) { tmptim++; /* 1973, 1977, 1981,...,etc. */ caltim -= (_YEAR_SEC + _DAY_SEC); } else { /* * In a leap year after all, set the flag. */ islpyr++; } } } /* * tmptim now holds the value for tm_year. caltim now holds the * number of elapsed seconds since the beginning of that year. */ ptb->tm_year = tmptim; /* * Determine days since January 1 (0 - 365). This is the tm_yday value. * Leave caltim with number of elapsed seconds in that day. */ ptb->tm_yday = (int)(caltim / _DAY_SEC); caltim -= (__time32_t)(ptb->tm_yday) * _DAY_SEC; /* * Determine months since January (0 - 11) and day of month (1 - 31) */ if ( islpyr ) mdays = _lpdays; else mdays = _days; for ( tmptim = 1 ; mdays[tmptim] < ptb->tm_yday ; tmptim++ ) ; ptb->tm_mon = --tmptim; ptb->tm_mday = ptb->tm_yday - mdays[tmptim]; /* * Determine days since Sunday (0 - 6) */ ptb->tm_wday = ((int)(*timp / _DAY_SEC) + _BASE_DOW) % 7; /* * Determine hours since midnight (0 - 23), minutes after the hour * (0 - 59), and seconds after the minute (0 - 59). */ ptb->tm_hour = (int)(caltim / 3600); caltim -= (__time32_t)ptb->tm_hour * 3600L; ptb->tm_min = (int)(caltim / 60); ptb->tm_sec = (int)(caltim - (ptb->tm_min) * 60); ptb->tm_isdst = 0; return 0; }
以上这篇C语言实现时间戳转日期的算法(推荐)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持猪先飞。
上一篇: C语言实现字符转unix时间戳的简单实例
下一篇: C基础 寻找随机函数的G点详解
相关文章
- 这篇文章主要为大家详细介绍了C语言实现放烟花的程序,有音乐播放,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-02-23
- 本篇文章主要介绍C语言中char的知识,并附有代码实例,以便大家在学习的时候更好的理解,有需要的可以看一下...2020-04-25
- 这篇文章主要介绍了详解如何将c语言文件打包成exe可执行程序,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-25
- 这篇文章主要介绍了PostgreSQL TIMESTAMP类型 时间戳操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-12-26
- 一、日期减去天数等于第二个日期function cc(dd,dadd){//可以加上错误处理var a = new Date(dd)a = a.valueOf()a = a - dadd * 24 * 60 * 60 * 1000a = new Date(a)alert(a.getFullYear() + "年" + (a.getMonth() +...2015-11-08
- 这篇文章主要介绍了解决python 两个时间戳相减出现结果错误的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-12
- free函数是释放之前某一次malloc函数申请的空间,而且只是释放空间,并不改变指针的值。下面我们就来详细探讨下...2020-04-25
详解C语言中的rename()函数和remove()函数的使用方法
这篇文章主要介绍了详解C语言中的rename()函数和remove()函数的使用方法,是C语言入门学习中的基础知识,需要的朋友可以参考下...2020-04-25- 这篇文章主要介绍了C语言中计算正弦的相关函数总结,包括正弦和双曲线正弦以及反正弦的函数,需要的朋友可以参考下...2020-04-25
- 本文实例讲述了php计算两个日期相差天数的方法。...2015-03-15
- 这篇文章主要介绍了PostgreSQL 字符串处理与日期处理操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-02-01
- moment是一款多语言支持的日期处理类库, 在vue中如何使用呢?这篇文章主要给大家介绍了关于vue之moment使用的相关资料,需要的朋友可以参考下...2021-05-13
- 这篇文章主要介绍了C语言中求和、计算平均值、方差和标准差的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-12-10
- 实例讲解之前,先来介绍几个核心函数: mktime 函数 mktime() 函数返回一个日期的 Unix 时间戳。 参数总是表示 GMT 日期,因此 is_dst 对结果没有影响。 参数可以从右到左依次空着,空着的参数会被设为相应的当前 GMT 值。...2015-11-08
- 这篇文章主要介绍了C#实现将字符串转换成日期格式的方法,涉及C#操作时间及字符串的相关技巧,非常简单实用,需要的朋友可以参考下...2020-06-25
- 本篇文章主要讲解C语言 基本语法,这里提供简单的示例和代码来详细讲解C语言的基本语法,开始学习C语言的朋友可以看一下,希望能够给你带来帮助...2021-09-18
- 分享一个利用php根据日期或时间戳获取相应的干支纪年,生肖和星座信息的函数方法,具体函数代码以及使用方法如下: /** 判断干支、生肖和星座 */ function birthext($birth){ if(strstr($birth,'-')===false&&strlen($bi...2015-10-21
- 这篇文章主要为大家详细介绍了Unity时间戳的使用方法,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
php 获取今日、昨日、上周、本月的起始时间戳和结束时间戳的方法
php获取今日开始时间戳和结束时间戳$beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));$endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;//php获取昨日起始时间戳和结束时间...2013-10-04- 这篇文章主要介绍了C语言中send()函数和sendto()函数的使用方法,是C语言入门学习中的基础知识,需要的朋友可以参考下...2020-04-25