2013-10-16 127 views
3

我應該如何修改此代碼以每00:00 AM打印(「它的午夜」)?將time_t轉換爲整數

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

int main(void) 
{ 
    struct tm * localtime (const time_t * ptr_time); 
    struct tm 
{ 
    int tm_sec;   /* segundos, range 0 to 59   */ 
    int tm_min;   /* minutos, range 0 to 59   */ 
    int tm_hour;  /* horas, range 0 to 23    */ 
    int tm_mday;  /* dia do mes, range 1 to 31 */ 
    int tm_mon;   /* mes, range 0 to 11    */ 
    int tm_year;  /* numero de anos desde 1900 */ 
    int tm_wday;  /* dia da semana, range 0 to 6 */ 
    int tm_yday;  /* dia no ano, range 0 to 365 */ 
    int tm_isdst;  
    }; 
    time_t mytime; 
    mytime = time(NULL); 
    printf(ctime(&mytime)); 
/* 
if(str_time.tm_hour == 00 && str_time.tm_min == 00 && str_time.tm_sec == 00) 
{ 
printf("Its midnight"); 
} 
*/ 
    return 0; 
} 

的time_t的輸出是: 萬維網嗯DD HH:MM:SS YYYY

實施例: 星期二02月26 9時01分47秒2009

+0

你一定意味着time_t爲int。 –

+0

你想在哪個平臺上做到這一點? – alk

+0

大多數人會說「00:00 PM」是中午。我想你想要「00:00 AM」。 – chux

回答

1

如果可以,請使用sleep()暫停。
使用time()localtime()來確定午睡時間。

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

int main() { 
    while (1) { 
     time_t Now; 
     if (time(&Now) == -1) { 
     return -1; 
     } 
     struct tm tm; 
     tm = *localtime(&Now); 
     tm.tm_mday++; 
     tm.tm_hour = 0; 
     tm.tm_min = 0; 
     tm.tm_sec = 0; 
     tm.tm_isdst = -1; 
     time_t Then; 
     Then = mktime(&tm); 
     if (Then == -1) { 
     return -1; 
     } 
     if (Then <= Now) { 
     return -1; // Time moving backwards - Hmmmm? 
     } 
     unsigned int NapTime = (unsigned int) (Then - Now); 
     printf("Going to sleep %u\n", NapTime); 
     fflush(stdout); // Let's empty the buffers before nap time 
     if (0 != sleep(NapTime)) { 
     return -1; // trouble sleeping 
     } 
     // Done sleeping! 
     printf("Midnight\n"); 
     fflush(stdout); 
     sleep(10); 
    } 
    } 
+0

謝謝,它的工作。 – Neto

+0

@ user2887098您可能還想查看C11中新增的'thrd_sleep()'。我沒有經驗。 (感謝給我超過5000代表) – chux

0

一般而言time_t的作爲一個實施32或64位整數。雖然沒有定義這個標準的標準。所以,如果你擔心可移植性,你可能不應該使用它。但是,如果你不是,那麼你應該測試一下它是如何在你的系統上實現的。它可能已被視爲一個整數。

編輯: 如果你想打破一些的time_t的元素,然後使用類似,

struct tm * localtime (const time_t * ptr_time); 

的tm結構,這是回來了,看起來像這樣,

struct tm { 
    int tm_sec;   /* seconds, range 0 to 59   */ 
    int tm_min;   /* minutes, range 0 to 59   */ 
    int tm_hour;  /* hours, range 0 to 23    */ 
    int tm_mday;  /* day of the month, range 1 to 31 */ 
    int tm_mon;   /* month, range 0 to 11    */ 
    int tm_year;  /* The number of years since 1900 */ 
    int tm_wday;  /* day of the week, range 0 to 6 */ 
    int tm_yday;  /* day in the year, range 0 to 365 */ 
    int tm_isdst;  /* daylight saving time    */ 
}; 

編輯2:

本示例從gnu.org中提取。我拿出印刷的東西,因爲你不想使用這些東西,但我留下了其餘的東西給你弄清楚。

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

int 
main (void) 
{ 
    time_t curtime; 
    struct tm *loctime; 

    /* Get the current time. */ 
    curtime = time (NULL); 

    /* Convert it to local time representation. */ 
    loctime = localtime (&curtime); 

    return 0; 
} 
+0

我真正想要做的是把我的日期/小時放入一個變量(整數或字符串)。 – Neto

+0

啊,我原本以爲你是想要這樣的事情,但第二個猜測我的原始帖子。 time.h tm結構給你所需的所有信息。 – dsell002

+0

對不起,我很愚蠢,所以我想把條件: – Neto