Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 计算从历元开始经过的天数-基于公式_C_Date_Formula_Leap Year - Fatal编程技术网

C 计算从历元开始经过的天数-基于公式

C 计算从历元开始经过的天数-基于公式,c,date,formula,leap-year,C,Date,Formula,Leap Year,我试图计算从给定的GMT时间经过的天数 嗯,我能用迭代的计算方法(找到正常年数)使它工作 (和闰年) 函数get_number_of_leap_years_from_base_year迭代从1970年到给定日期的所有年份,并检查每年是否为闰年,最后添加所有天数 是否有任何其他方法(公式)来计算正常和闰年经过的次数 /* so-prg-2: Calculating number normal & leap years passed */ #include <stdio.h>

我试图计算从给定的
GMT
时间经过的天数

嗯,我能用迭代的计算方法(找到正常年数)使它工作 (和闰年)

函数
get_number_of_leap_years_from_base_year
迭代从1970年到给定日期的所有年份,并检查每年是否为闰年,最后添加所有天数

是否有任何其他方法(公式)来计算正常和闰年经过的次数

/* so-prg-2: Calculating number normal & leap years passed */

#include <stdio.h>
#include <string.h>
#include <time.h>

#define BASE_YEAR 1970

void print_time_readable_format(struct tm tm);
int convert_gmt_date_time_to_tm_format(char* gmt_time_fmt);
int get_number_of_leap_years_from_base_year(int start_year, int end_year);
int calculate_days_elapsed_from_epoch(struct tm tm);

int main()
{
    int days = 0;
    char gmt_time_fmt[] = "Dec 28 18:40:01 2020 GMT";
    //char gmt_time_fmt[] = "Jan 20 19:00:01 2019 GMT";
    //char gmt_time_fmt[] = "Dec 27 14:52:30 2020 GMT";
    //char gmt_time_fmt[] = "Jan 01 00:00:01 1970 GMT";
    days = convert_gmt_date_time_to_tm_format(gmt_time_fmt);

    printf("GMT = %s and days are %d\n", gmt_time_fmt, days);
    return 0;
}

int convert_gmt_date_time_to_tm_format(char* gmt_time_fmt)
{
    struct tm tm;
    char tm_time_fmt[255];

    //set tm struture to 0
    memset(&tm, 0, sizeof(struct tm));
    // convert gmt_time_fmt to format required by 'tm' structure
    strptime(gmt_time_fmt, "%B %d %H:%M:%S %Y GMT", &tm);

    strftime(tm_time_fmt, sizeof(tm_time_fmt), "%s", &tm);
    printf("tm_time_fmt = %s\n", tm_time_fmt);

    print_time_readable_format(tm);
    return calculate_days_elapsed_from_epoch(tm);
}

int calculate_days_elapsed_from_epoch(struct tm tm)
{
    int days_by_month [2][12] = {
        /* normal years */
        { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334},
        /* leap years */
        { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}
    };

    int current_year = tm.tm_year+1900;
    int total_years_passed = current_year - BASE_YEAR;
    /* -1, to skip the current year */
    int nleap_years_passed = get_number_of_leap_years_from_base_year(BASE_YEAR, current_year-1); 
    int normal_years = total_years_passed - nleap_years_passed;
    int total_days_passed = (normal_years*365 + nleap_years_passed*366 );
    
    printf(" **Years total_days_passed =%d\n", total_days_passed);
    
    total_days_passed += days_by_month[(current_year%4 == 0) - (current_year%100 == 0) + (current_year%400 == 0)][tm.tm_mon];
    total_days_passed += tm.tm_mday - 1; /* to skip the current day */
    
    printf(" **total_days_passed =%d\n", total_days_passed);
    return total_days_passed;
}

int get_number_of_leap_years_from_base_year(int start_year, int end_year)
{
    int leap_year_count = 0;
    int year = start_year;
    
    while( year <= end_year)
    {
        if( (year%4 == 0) - (year%100 == 0) + (year%400 == 0) )
            leap_year_count++;
        year++;
    }
    printf("leap_year_count = %d\n", leap_year_count);
    return leap_year_count;
}

void print_time_readable_format(struct tm tm)
{
    printf("tm.tm_year = %d ", tm.tm_year);
    printf("tm.tm_mon = %d ", tm.tm_mon);
    printf("tm.tm_mday = %d ",tm.tm_mday);
    printf("tm.tm_hour = %d ", tm.tm_hour); 
    printf("tm.tm_min = %d ", tm.tm_min );
    printf("tm.tm_sec = %d\n", tm.tm_sec );
}
/*so-prg-2:计算通过的正常年份和闰年数*/
#包括
#包括
#包括
#1970年定义基准年
无效打印时间可读格式(struct tm);
int将日期时间转换为tm格式(字符*gmt时间fmt);
int从基础年(int开始年、int结束年)获取闰年数;
int计算从新纪元开始经过的天数(struct tm);
int main()
{
整数天=0;
char gmt_time_fmt[]=“12月28日18:40:01 2020 gmt”;
//char gmt_time_fmt[]=“2019年1月20日19:00:01 gmt”;
//char gmt_time_fmt[]=“12月27日14:52:30 2020 gmt”;
//char gmt_time_fmt[]=“Jan 01 00:00:01 1970 gmt”;
天数=将gmt日期时间转换为gmt格式(gmt时间fmt);
printf(“GMT=%s和天为%d\n”,GMT\u time\u fmt,天);
返回0;
}
int将\u gmt\u日期\u时间\u转换为\u tm\u格式(char*gmt\u时间\u fmt)
{
struct-tm;
char tm_time_fmt[255];
//将tm结构设置为0
memset(&tm,0,sizeof(struct-tm));
//将gmt\u time\u fmt转换为“tm”结构所需的格式
strtime(gmt\u time\u fmt,“%B%d%H:%M:%S%Y gmt”,&tm);
strftime(tm_time_fmt,sizeof(tm_time_fmt),%s,&tm);
printf(“tm\u time\u fmt=%s\n”,tm\u time\u fmt);
打印时间可读格式(tm);
返回计算时间(tm)中经过的天数;
}
int计算从新纪元开始经过的天数(struct tm)
{
整数天/月[2][12]={
/*正常年份*/
{ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334},
/*闰年*/
{ 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}
};
int当前年份=tm.tm当前年份+1900;
int total_years_passed=当前_year-基准年;
/*-1,跳过当前年度*/
int nleap_years_passed=从基本年(基本年,当前年-1)中获取跳跃年数;
int正常年数=总通过年数-nleap通过年数;
整数总通过天数=(正常通过天数*365+nleap通过天数*366);
printf(“**年总通过天数=%d\n”,总通过天数);
总通过天数+=按月天数[(当前年份%4==0)-(当前年份%100==0)+(当前年份%400==0)][tm.tm\mon];
通过的总天数+=tm.tm\u mday-1;/*跳过当前日期*/
printf(“**通过的总天数=%d\n”,通过的总天数);
返回经过的总天数;
}
int从基础年(int开始年、int结束年)获取闰年数
{
整数闰年计数=0;
int year=开始年份;

而(年闰年的条件总结如下:

  • 闰年,如果完全可见400
  • 如果可以被100看到,则不是闰年,但不能被400整除
  • 闰年,如果不可被100整除,但可被4整除
  • 其他年份都不是闰年
因此,逻辑可以表示为:

if (((year%4 == 0) && (year%100 != 0)) || (year%400 == 0))
{
    leap_year_count++;
}
year++;
但不确定重新分解现有逻辑是否会增加速度优势。

使用
mktime()
因为您的代码可以同时使用标准C和POSIX,所以也没有理由不使用标准C

它为您提供一个
time\u t
值,它是自历元以来的秒数

int计算从新纪元开始经过的天数(struct tm)
{
时间t=mktime(&tm);
返回t/86400;//24*60*60=86400
}
但是,如果目标是计算自纪元以来的秒数,则可以立即从
mktime()
中获得答案

请注意,
mktime()
传递了一个
struct tm
指针,它接受“超出范围”的值并对结果进行规范化。另请参阅“演示
mktime()
一节中的示例代码

计算闰日 我的库中潜伏着一个函数
jl\u dmy\u conversion()
,它将年、月、日的组合转换为1899-12-31年以来的天数(因此在这个系统中,第一天是1900-01-01)。但它包括闰日数的计算。此代码是包的内部代码,其中参数已在日期范围0001-01-01..9999-12-31内验证为有效,因此它不会对无效数据进行太多保护。还有另一个调用此代码的函数会进行数据验证。一些这里显示的数据来自一个头文件,大部分来自包含实现的源文件

typedef int Date;
枚举{DATE_NULL=-2147483648};/*Informix NULL DATE*/
#定义年份(y)((y)%4)==0&((y)%100)!=0 | |((y)%400)==0))
#定义PRId_日期“d”
/*
**400年中有97个闰年(因为三年
**可以被100整除但不能被400整除的不是闰年)
**正好是20871周。
*/
#以400年为单位定义天数(400*365+97)
#以2000年为单位定义天数(以400年为单位定义5*天)
枚举
{
1月的天数=31天,
二月的天数=28天,
三月份的天数=31,
4月的天数=30天,
五月份的天数=31天,
六月的天数=30天,
7月的天数=31天,
8月份的天数=31,
9月的天数=30天,
10月份的天数=31,
11月的天数=30天,
12月的天数=31
};
每月内的静态常数天数[][2]=
{
{ 0,                 0                  },
{一月天,一月天},
{二月天,二月天+1},
{三月天,三月天},
{四月天,四月天},
{五月天,五月天},
{六月天,六月天
#include <stdio.h>
#include <time.h>

static void print_time(time_t t, const struct tm *tm)
{
    char buffer[32];
    strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S %A", tm);
    printf("%lld: %s (%d)\n", (long long)t, buffer, tm->tm_yday);
}

int main(void)
{
   struct tm tm = { .tm_year = 2020 - 1900, .tm_mon = 12 - 1, .tm_mday = 28,
                    .tm_hour = 8, .tm_min = 20, .tm_sec = 26 };
   time_t t0 = mktime(&tm);
   print_time(t0, &tm);
   tm.tm_mday += 6;
   tm.tm_hour += 18;
   tm.tm_min += 43;
   tm.tm_sec += 32;
   time_t t1 = mktime(&tm);
   print_time(t1, &tm);
   return 0;
}
1609168826: 2020-12-28 08:20:26 Monday (362)
1609754638: 2021-01-04 03:03:58 Monday (3)