teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

誤記修正

2020/07/16 13:59

投稿

cateye
cateye

スコア6851

answer CHANGED
@@ -29,7 +29,6 @@
29
29
  timer->tm_hour, timer->tm_min, timer->tm_sec);
30
30
  puts(str);
31
31
  tm_sec = timer->tm_sec;
32
-
33
32
  }
34
33
  sleep(1);
35
34
  }

1

ソース追記

2020/07/16 13:59

投稿

cateye
cateye

スコア6851

answer CHANGED
@@ -6,4 +6,43 @@
6
6
  書式を変えましょう↓
7
7
  "%04d%02d%02d%02d%02d%02d"→"%04d年%02d月%02d日 %02d時%02d分%02d秒"
8
8
 
9
- 蛇足ながら、『関数にする必要ありますか?』
9
+ 蛇足ながら、『関数にする必要ありますか?』
10
+ 追記:ただ、表示するだけで良ければ・・・
11
+ ```c
12
+ #include <stdio.h>
13
+ #include <unistd.h>
14
+ #include <time.h>
15
+ //
16
+ int main( )
17
+ {
18
+ time_t current;
19
+ struct tm *timer;
20
+ int tm_sec = 0;
21
+ char str[256];
22
+ //
23
+ while(1) {
24
+ current = time(NULL);
25
+ timer = localtime(&current);
26
+ if(timer->tm_sec != tm_sec) {
27
+ sprintf(str, "%04d年%02d月%02d日 %02d時%02d分%02d秒",
28
+ timer->tm_year + 1900, timer->tm_mon + 1, timer->tm_mday,
29
+ timer->tm_hour, timer->tm_min, timer->tm_sec);
30
+ puts(str);
31
+ tm_sec = timer->tm_sec;
32
+
33
+ }
34
+ sleep(1);
35
+ }
36
+ //
37
+ }
38
+ ```
39
+ usr ~/Project/test % ./a.out
40
+ 2020年07月16日 22時56分36秒
41
+ 2020年07月16日 22時56分37秒
42
+ 2020年07月16日 22時56分38秒
43
+ 2020年07月16日 22時56分39秒
44
+ 2020年07月16日 22時56分40秒
45
+ 2020年07月16日 22時56分41秒
46
+ 2020年07月16日 22時56分42秒
47
+ 2020年07月16日 22時56分43秒
48
+ ^C