【问题标题】:C++ / Converting time(NULL) to exact years, months, weeks, hours, and minutes from Jan 1, 1970C++ / 将时间(NULL)转换为从 1970 年 1 月 1 日开始的精确年、月、周、小时和分钟
【发布时间】:2013-06-27 12:14:22
【问题描述】:

我正在尝试编写 C++ 代码来计算自 1970 年 1 月 1 日以来的年数、月数、周数、小时数和分钟数。我包括我目前拥有的代码。请帮我。提前致谢。

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


int main(){

double seconds, minutes, days, weeks, months, years, hours;


seconds = time(NULL); 
minutes = seconds / 60;
hours = minutes / 60;
days = hours / 24;
weeks = days / 7;
months = weeks / 4;
years = days / 365;

months = (int) (days / 30.42) % 12;
weeks = (int) (days / 7) % 52;
days = (int) (hours / 24) % 24;
hours = (int) (minutes / 60) % 1;
minutes = (int) (seconds / 60) % 60; 


printf("%d years \n", (int)years); 
printf(" %d months \n", (int)months);
printf(" %d weeks\n", (int)weeks);
printf(" %d days \n", (int)days);
printf(" %d minutes\n", (int)minutes);
printf(" %d hours\n\n", (int)hours);


system("pause");
}

【问题讨论】:

  • 为什么不使用localtime?它会将time_t 转换为struct tm,这正是您所需要的。
  • 无闰年逻辑。需要解释魔术常数30.42
  • 感谢您的回复。不需要闰年逻辑。我认为 30.42 常数是为了弥补闰年。不确定,因为我是新手。我在让系统准确地计算出 time(null) 是一个有凝聚力的时间值单位时遇到了问题。我希望我可以使用当地时间,但我必须使用上面指定的功能。谢谢你们让他们来:)
  • 您有 2 周的计算:您是在寻找一个月中的一周还是一年中的一周,还是两者兼而有之?

标签: c++ c visual-studio time date-arithmetic


【解决方案1】:

首先您需要考虑在哪个时区需要此信息。

然后,不要自己编写代码,而是使用 gmtime_r 以 UTC 获取结果,或使用 localtime_r 以当前 TZ 的本地时区获取结果。

【讨论】:

    【解决方案2】:

    您应该首先检查标准函数locatime()gmtime()。他们很容易实现您的目标。

      time_t t = time(NULL);
      if (t == -1) { printf("time() failure"); return; }
      struct tm *tmp;
      tmp = localtime(&t);
      if (tmp == NULL) { printf("gmtime() failure"); return; }
      int seconds = tmp->tm_sec;
      int minutes = tmp->tm_min;
      int hours = tmp->tm_hour;
      int days = tmp->tm_mday + 1;
      int weeks = (days-1)/7; // OP code has 2 `weeks` calculated, go with week-of-the-month rather than week-of-the-year
      days -= weeks*7;
      int months = tmp->tm_mon + 1;
      int years = tmp->tm_year + 1900;
    
      printf("%d years \n", years);
      printf("%d months \n", months);
      printf("%d weeks \n", weeks);
      printf("%d days \n", days);
      printf("%d hours \n", hours);
      printf("%d minutes \n", minutes);
      printf("%d seconds \n", seconds);
    

    如果你真的想自己做这件事,你有一些工作要做。您没有指定时区,所以让我们使用最简单的:UTC。此外,让我们在unsigned 中尽可能简单地执行此操作。如果需要,您可以将其更改为 int

    // Get the time
    time_t t = time(NULL);
    if (t < 0) {
      ; // handle this error condition
    }
    unsigned seconds = t%60;
    t /= 60;
    unsigned minutes = t%60;
    t /= 60;
    unsigned hours = t%24;
    t /= 24;
    // now begins the tricky bit.
    // `t` represent the number of days since Jan 1, 1970.
    
    // I would show more here, but unless I know you are wanting this path, I'd rather not do the work.
    
    
    printf("%d years \n", (int)years);
    printf("%d months \n", (int)months);
    printf("%d weeks\n", (int)weeks);
    printf("%d days \n", (int)days);
    printf("%d minutes\n", (int)minutes);
    printf("%d hours\n\n", (int)hours);  
    

    【讨论】:

    • 楚克斯...谢谢!这更多是我正在寻找的。这非常棘手。我附上了我最新版本的代码。我知道有些模组已经关闭,因为任何 % 1 都是 0,但我仍在努力解决。有没有见识的人?谢谢大家的回复!!
    • 我在上面编辑了我的帖子以反映我到目前为止所拥有的内容。我认为它有点接近......
    • 你还差得很远。在您的本地代码中,尝试将seconds = time(NULL); 临时替换为seconds = 4*24*3600 + 3*3600 + 2*60 +1,并让您的代码报告Days:4,Hours:3, Minutes:2, Seconds:1。暂时忘记 YMW 字段。一旦你得到这个工作,重新编辑你的帖子。 GTG
    • 为了将来参考,最好从你已经知道的命令开始; look that up 并查看“另见”部分和(在本例中)“引用者”部分。
    猜你喜欢
    • 2012-01-19
    • 1970-01-01
    • 2020-02-15
    • 1970-01-01
    • 2021-03-31
    • 2016-06-16
    • 1970-01-01
    • 2021-11-24
    相关资源
    最近更新 更多