-2

我正在使用time()返回自 1970 年 1 月以来经过的秒数的函数;我想推断当前时间和日期,这是我所看到的..

   #define SECONDS_PER_YEAR  31536000u
   #define SECONDS_PER_MONTH 2628288u
    #define SECONDS_PER_WEEK  604800u

...

       time_t unaccountedSeconds = time(NULL);

        // Calculate years
          int years = unaccountedSeconds / SECONDS_PER_YEAR;
           // Seconds not absorbed by years
                    unaccountedSeconds = unaccountedSeconds % SECONDS_PER_YEAR;

      // Calculate months
           int months = unaccountedSeconds / SECONDS_PER_MONTH;
                 // Seconds not absorbed by months
         unaccountedSeconds = unaccountedSeconds % SECONDS_PER_MONTH;

              // Calculate weeks

那对我不起作用。

4

3 回答 3

0

我建议你使用 localtime(3) 函数。这会为您分解,并为您提供一个填充的 struct tm。

于 2013-05-13T16:26:15.847 回答
0

尝试这个:

time_t now = time(NULL);
struct tm* tmTime = localtime(&now);

int years = tmTime.tm_year;
int months = tmTime.tm_mon;
int wDay = tmTime.tm_wday; // day of the week
// ...
于 2013-05-13T16:28:46.973 回答
0

人-一次

man -a asctime

man -本地时间

result = time(NULL);
printf("current time %s  its %ju secs since the Epoch\n", 
             asctime(localtime(&result)), (uintmax_t)result);
return(0);
于 2013-05-13T16:28:51.297 回答