是的,这是很长的路,但我正在尝试以基本的方式做到这一点。 编译器开始将所有数字显示为负数,而一开始它们都是正数,我不明白为什么。 我知道这看起来很复杂,我还不知道该怎么做,因为我还没有使用结构或除基本格式之外的任何东西:
#include <stdio.h>
#include <time.h>
int main() {
// now = time(NULL); <----????? (now is more than a billion seconds - this would be long double, right?
// long double now;
int CurrentYear = 2017;
int CurrentMonth = 2;
int CurrentDay = 10;
int OldYear = 1970;
int OldMonth = 1;
int OldDay = 1;
int YearsPassed; // 47 years passed
int MonthsPassed; // 1 month passed
int DaysPassed; // 9 days passed
int YearsPassedInDays;
// 1533338720 days passed
int DaysToHours;
int Hours = 24;
int Minutes = 60;
int HoursToMinutes;
int MinutesToSeconds;
int Seconds = 60;
YearsPassedInDays = YearsPassed * 365;
printf("YearsPassedInDays = %d\n", YearsPassedInDays);
// 1533338720 days passed
DaysToHours = YearsPassedInDays * Hours;
printf("DaysToHours = %d\n", DaysToHours);
// DaysToHours: 610015968 hours
HoursToMinutes = DaysToHours * Minutes;
printf("HoursToMinutes = %d\n", HoursToMinutes);
// HoursToMinutes: 392762944 minutes
// MinutesToSeconds = HoursToMinutes * Seconds;
// printf("MinutesToSeconds = %d\n", MinutesToSeconds);
最佳答案
我认为你可以使用 time.h 库中的函数 difftime
double difftime(time_t time1, time_t time2)
Returns the difference of seconds between time1 and time2 (time1-time2). 1
关于c - 如何使用 time.h 将旧年份转换为现在的秒数(我开始得到负数),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42172415/