gpt4 book ai didi

c - 时间不正确,但代码似乎正确 :S (unexpected outcome)

转载 作者:行者123 更新时间:2023-11-30 17:08:44 24 4
gpt4 key购买 nike

所以我试图构建一个程序来告诉你准确的时间(以年、日、小时和分钟为单位)。

我已经检查了我的代码,看起来没问题,结果接近正确,但并不完全正确,我在下面发布了我的代码:

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

void year_day(time_t seconds, float *yearsLPtr, float *dayLPtr){
int yearS;
float t_constant;//seconds per year

t_constant = 365.2425*24*60*60; //seconds in a year (years to 4dp)
*yearsLPtr =(seconds/t_constant);// years as float since 1970 1/1 00:00
yearS=*yearsLPtr; //sending float to int
*dayLPtr = (((seconds/t_constant) - yearS)*365.2425);//used exact number for years to neglext error
*yearsLPtr+=1970; //adding 1970 to change in years to get current year
}

void hours_minutes(float dayL, float *hoursLPtr, float *minsLPtr){
float t_constant;
int dayS, minS;
dayS=dayL;
t_constant= 365.2425*24*60*60;
*hoursLPtr= (dayL-dayS)*24;
minS= *hoursLPtr;
*minsLPtr= (((dayL-dayS)*24)-minS)*60;


}

void print_time (float yearsL, float dayL, float hoursL, float minsL){

int constant, yearsS, dayS, hoursS, minsS;

yearsS=yearsL; //converting from float back to int, couldve pointed to int straight away tho
dayS=dayL;
hoursS=hoursL;
minsS=minsL;

printf("Year: %i \t", yearsS);
printf("Day: %i \t",dayS);
printf("Hour: %i \t",hoursS);
printf("Min: %i \t",minsS);

}


int main() {

float yearsL, dayL, hoursL, minsL;

time_t current_seconds; //time t like long/double for longer values

current_seconds=time(NULL);

year_day(current_seconds,&yearsL,&dayL);
hours_minutes(dayL,&hoursL,&minsL);
print_time(yearsL,dayL,hoursL,minsL);





getchar();

}

最佳答案

我发现的一个问题是您只使用了 time()。此函数不会返回您所在时区的时间。如果您需要您所在时区的时间,则需要使用 localtime() 来获取正确的时间。

关于c - 时间不正确,但代码似乎正确 :S (unexpected outcome),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33549548/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com