C语言实现时间戳转日期的算法

来源:互联网 发布:自制图片软件 编辑:程序博客网 时间:2024/05/22 12:03

一.可以通过现有函数实现

C语言库函数:localtime就可以获得一个时间戳对应的具体日期了

在标准C/C++中,我们可通过tm结构来获得日期和时间,tm结构在time.h中的定义如下:

  #ifndef _TM_DEFINED
  struct tm {
  int tm_sec; /* 秒–取值区间为[0,59] */
  int tm_min; /* 分 - 取值区间为[0,59] */
  int tm_hour; /* 时 - 取值区间为[0,23] */
  int tm_mday; /* 一个月中的日期 - 取值区间为[1,31] */
  int tm_mon; /* 月份(从一月开始,0代表一月) - 取值区间为[0,11] */
  int tm_year; /* 年份,其值从1900开始 */
  int tm_wday; /* 星期–取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 */
  int tm_yday; /* 从每年的1月1日开始的天数–取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 */
  int tm_isdst; /* 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的进候,tm_isdst为0;不了解情况时,tm_isdst()为负。*/
  };
  #define _TM_DEFINED
  #endif
  ANSI C标准称使用tm结构的这种时间表示为分解时间(broken-down time)。


格式转换

  可以使用的函数是gmtime()和localtime()将time()获得的日历时间time_t结构体转换成tm结构体。
  其中gmtime()函数是将日历时间转化为世界标准时间(即格林尼治时间),并返回一个tm结构体来保存这个时间,而localtime()函数是将日历时间转化为本地时间。


程序举例 

 #include <stdio.h>

  #include <time.h>
  int main(void)
  {
  struct tm *ptr;
  time_t lt;
  lt =time(NULL);
  ptr=localtime(&lt);
  printf("second:%d\n",ptr->tm_sec);
  printf("minute:%d\n",ptr->tm_min);
  printf("hour:%d\n",ptr->tm_hour);
  printf("mday:%d\n",ptr->tm_mday);
  printf("month:%d\n",ptr->tm_mon+1);
  printf("year:%d\n",ptr->tm_year+1900);
  return 0;

  }

范例:

#include <time.h>
main(){
char *wday[] = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
time_t timep;
structtm *p;
time(&timep);
p = localtime(&timep);//取得当地时间
printf ("%d%d%d ",(1900+p->tm_year),(l+p->tm_mon), p->tm_mday);
printf("%s%d:%d:%d\n", wday[p->tm_wday], p->tm_hour, p->tm_min, p->tm_sec);
}

执行结果:
2000/10/28 Sat 11:12:22

二.算法实现

时间是有周期规律的,4年一个周期(平年、平年、平年、闰年)共计1461天。Windows上C库函数time(NULL)返回的是从1970年1月1日以来的毫秒数,我们最后算出来的年数一定要加上这个基数1970。总的天数除以1461就可以知道经历了多少个周期;总的天数对1461取余数就可以知道剩余的不足一个周期的天数,对这个余数进行判断也就可以得到月份和日了。

static int DAYS = 24*3600;
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:%d\n", 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:%d\n", nDecyear, nDecmoth, nDecday);
    getHourMinSec(nTime%DAYS);
    return;
}

int main(void)

{
print_time();
return 0;

}