|
|
楼主 |
发表于 2013-5-27 08:52:43
|
显示全部楼层
/**************************************************************
* Copyright (C) 2006-2013 All rights reserved.
* @Version: 1.0
* @Created: 2013-05-26 19:01
* @Author: chin - qin@126.com
* @Description:
*
* @History:
**************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
/*高斯的出生日期,此处可以改为你自己的生日来测试*/
#define BIRTH_YEAR 1777
#define BIRTH_MONTH 4
#define BIRTH_DAY 30
static int leap_month[] = {31,29,31,30,31,30,31,31,30,31,30,31};
static int non_leap_month[] = {31,28,31,30,31,30,31,31,30,31,30,31};
/**
* @brief is_leap_year
* 判断是否为闰年
* @Param: year
*
* Returns: 是闰年返回1,否则返回0
*/
int is_leap_year(int year)
{
int leap= (year % 400 == 0) ||
((year % 4 == 0)&&(year % 100 != 0)) ;
return leap;
}
/**
* @brief get_gauss_date
* 用户输入高斯日期
* Returns:
*/
unsigned long get_gauss_date()
{
unsigned long date;
char s[32];
while(1)
{
printf(" lease input a number as the Gauss date[1-37527]:");
fgets(s, 32, stdin);
date = atol(s);
if(date != 0)
break;
}
return date;
}
/**
* @brief still_left_time
* 计算XXXX年还剩下多少天
* @Param: year
* @Param: month
* @Param: day
*
* Returns:
*/
int still_left_time(int year, int month, int day)
{
int *p, days = 0, i;
if(is_leap_year(year))
p = leap_month;
else
p = non_leap_month;
days = p[month-1] - day;
for(i=month; i< 12; i++)
days += p;
return days + 1;
}
/**
* @brief format_date
* 将距离当前日期date天转换成具体的日期格式
* @Param: date
* @Param: year 当前日期年
* @Param: month 当前日期月
* @Param: day 当前日期日
*
* Returns:
*/
char *format_date(unsigned long date, int year, int month, int day)
{
int i = 0, *p;
unsigned long count = 0;
static char date_str[32];
if(is_leap_year(year))
p = leap_month;
else
p = non_leap_month;
if(month == BIRTH_MONTH && day == BIRTH_DAY)
{
count = p[month-1] - day + 1;
if(count >= date)
{
sprintf(date_str, "%04d-%02d-%02d",year, month, day + (int)date - 1);
return date_str;
}
}
for(i=month; i<12; i++)
{
count += p;
if(count >= date)
{
month = i + 1;
day = date - ( count - p );
sprintf(date_str, "%04d-%02d-%02d",year, month, day);
break;
}
}
return date_str;
}
/**
* @brief caculate_gauss_date
* 计算高斯日期函数
* @Param: date 高斯日期
* @Param: date_str 格式化后的日期
*
* Returns:
*/
char *caculate_gauss_date(unsigned long date, char *date_str)
{
unsigned long count1 = 0;
int left_time;
int days_of_year;
int year = BIRTH_YEAR, month = BIRTH_MONTH, day = BIRTH_DAY;
int i = 0;
if(date_str)
{
/*计算xxxx年这一年还剩下多少天*/
left_time = still_left_time(year, month, day);
if( date <= left_time )
{
return strcpy(date_str, format_date(date, year, month, day ) );
}
date = date - left_time;
year = BIRTH_YEAR;
month = 12;
day = 31;
/*从1788-01-01开始计时*/
while(1)
{
i++;
days_of_year = is_leap_year( BIRTH_YEAR + i ) > 0 ? 366 : 365;
count1 += days_of_year;
if(count1 >= date)
{
year = BIRTH_YEAR + i;
date = date - (count1 - days_of_year);
break;
}
}
/*不足一年数的情况,格式化成具体日期*/
strcpy(date_str, format_date(date, year, 0, day ) );
}
return date_str;
}
int main()
{
unsigned long date;
char date_str[32];
date = get_gauss_date();
printf("Gauss log date is:%lu\n",date);
caculate_gauss_date(date,date_str);
printf("Format date is:%s\n",date_str);
return 0;
} |
|