1

Cで(現在の日付なしで)現在の時刻を取得したいのですが、主な問題は関数でそれをしたいときです。私がそれらを使用しないときは、何も問題ありません。なぜ私のコードが1時間しか表示されないのか、誰か教えてもらえますか? (添付の画像を見てください)。前もって感謝します。

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

char* get_time_string()
{
    struct tm *tm;
    time_t t;
    char *str_time = (char *) malloc(100*sizeof(char));
    t = time(NULL);
    tm = localtime(&t);
    strftime(str_time, sizeof(str_time), "%H:%M:%S", tm);
    return str_time;
}

int main(int argc, char **argv)
{
    char *t = get_time_string();
    printf("%s\n", t);
    return 0;
}

ここに画像の説明を入力

4

5 に答える 5

0

この概念を使用して、システム時刻を取得して更新します。私は何年も前に私のプロジェクトでこれを使用しました。要件に応じて変更できます。

updtime()                 /* FUNCTION FOR UPDATION OF TIME */
{
 struct time tt;
 char str[3];
 gettime(&tt);
 itoa(tt.ti_hour,str,10);
 setfillstyle(1,7);
 bar(getmaxx()-70,getmaxy()-18,getmaxx()-30,getmaxy()-10);
 setcolor(0);
 outtextxy(getmaxx()-70,getmaxy()-18,str);
 outtextxy(getmaxx()-55,getmaxy()-18,":");
 itoa(tt.ti_min,str,10);
 outtextxy(getmaxx()-45,getmaxy()-18,str);
return(0);
}
The previous function will update time whenever you will call it like 

and this will give you time

 int temp;
 struct time tt;
 gettime(&tt);                       /*Get current time*/
 temp = tt.ti_min;

時間を更新したい場合は、次のコードを使用できます。

   gettime(&tt);
   if(tt.ti_min != temp)  /*Check for any time update */
   {
    temp = tt.ti_min;
    updtime();
   }

これは複雑なコードですが、理解すればすべての問題が解決します。

楽しみ :)

于 2013-04-11T09:16:30.750 に答える
0

これを試して...

int main ()
 {
  time_t rawtime;
  struct tm * timeinfo;

  time ( &rawtime );
  timeinfo = localtime ( &rawtime );
  printf ( "Current local time and date: %s", asctime (timeinfo) );

  return 0;
}
于 2013-04-11T09:11:50.867 に答える