c - 如何使用 time.h 将旧年份转换为现在的秒数(我开始得到负数)

标签 c

是的,这是很长的路,但我正在尝试以基本的方式做到这一点。 编译器开始将所有数字显示为负数,而一开始它们都是正数,我不明白为什么。 我知道这看起来很复杂,我还不知道该怎么做,因为我还没有使用结构或除基本格式之外的任何东西:

#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/

相关文章:

c - 检查每个元素在数组中出现次数的函数

c - 从 C 代码控制 shell?

c - 取消引用指向链表上不完整类型的指针

c - 双转换字符串

c - ANSI C 的可嵌入 VM 运行时

c - 使用 fgets 将数据读入未初始化的 char 指针变量会在第二次读取时崩溃

c - 在 C 中读取文本文件并将其内容打印到屏幕

c - 显示随机生成的整数的频率

一个非常简单的 C 结构中的编译错误

c - 包含包含 header 时未找到枚举声明