annotate game_time.c @ 58:1cccf6d964b7

*** empty log message ***
author gongo
date Thu, 09 Nov 2006 01:43:07 +0000
parents 0fae5658fb0b
children 0b65ca27f113
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
1 /**
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
2 * 2006/07/31(MON) Nobutaka OSHIRO
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
3 */
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
4 #include "game_time.h"
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
5
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
6 int game_time_get_msec() {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
7
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
8 struct timeval now;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
9 int time_msec;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
10
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
11 /**
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
12 * *** man timeval ***
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
13 * struct timeval {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
14 * time_t tv_sec // second
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
15 * suseconds_t tv_usec; // micro sec
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
16 * };
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
17 **/
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
18
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
19 // get time micro second ( micro second = 1 / 1000,000 second)
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
20 gettimeofday(&now, NULL);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
21
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
22 // evaluation milli second ( milli second = 1 / 1000 second )
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
23 time_msec = now.tv_sec * 1000 + now.tv_usec / 1000;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
24
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
25 return time_msec;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
26 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
27
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
28
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
29 int game_time_conv_ms2cs (int msec)
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
30 {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
31 return msec / 10 % 100; /** 1/100 sec **/
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
32 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
33
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
34 int game_time_conv_ms2sec (int msec)
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
35 {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
36 return msec / 1000 % 60;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
37 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
38
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
39 int game_time_conv_ms2minute (int msec)
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
40 {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
41 return msec / 60000 % 60;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
42 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
43
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
44
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
45 /**
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
46 * NAME:
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
47 * game_time_set_raptime - Return RAP TIME ("XXX:XX:XX")
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
48 *
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
49 * DESCRIPTION:
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
50 * 1. convert (int --> string)
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
51 * (int) argument2 (milli second) --> "XXX:XX:XX"
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
52 *
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
53 * 2. set ( string --> char * )
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
54 * "XXX:XX:XX" --> char *argument1
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
55 **/
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
56 void game_time_set_raptime (char *s,int msec)
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
57 {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
58 char chminute[3];
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
59 char chsec[2];
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
60 char chcsec[2];
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
61 char raptime[10];
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
62 int minute = game_time_conv_ms2minute (msec);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
63 int sec = game_time_conv_ms2sec (msec);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
64 int csec = game_time_conv_ms2cs (msec);
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
65 int i;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
66
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
67
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
68 if (minute < 10 ){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
69 chminute[0] = ' ';
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
70 chminute[1] = '0';
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
71 chminute[2] = minute + 48;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
72 } else if (minute < 100 ) {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
73 chminute[0] = ' ';
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
74 chminute[1] = (minute / 10) + 48;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
75 chminute[2] = (minute % 10) + 48;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
76 } else {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
77 chminute[0] = (minute / 100) + 48;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
78 chminute[1] = (minute / 10) + 48;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
79 chminute[2] = (minute % 10) + 48;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
80 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
81 if (sec < 10 ){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
82 chsec[0] = '0';
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
83 chsec[1] = sec + 48;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
84 } else {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
85 chsec[0] = (sec / 10) + 48;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
86 chsec[1] = (sec % 10) + 48;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
87 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
88
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
89 if (csec < 10 ){
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
90 chcsec[0] = '0';
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
91 chcsec[1] = csec + 48;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
92 } else {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
93 chcsec[0] = (csec / 10) + 48;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
94 chcsec[1] = (csec % 10) + 48;
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
95 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
96
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
97
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
98 /** raptime[0-9] == "XXX:XX:XX\0" **/
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
99 raptime[0] = chminute[0];
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
100 raptime[1] = chminute[1];
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
101 raptime[2] = chminute[2];
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
102 raptime[3] = ':';
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
103 raptime[4] = chsec[0];
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
104 raptime[5] = chsec[1];
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
105 raptime[6] = ':';
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
106 raptime[7] = chcsec[0];
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
107 raptime[8] = chcsec[1];
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
108 raptime[9] = '\0'; // NULL
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
109
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
110
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
111 for (i = 0; raptime[i] != '\0'; i++) {
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
112 s[i] = raptime[i];
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
113 }
0fae5658fb0b Initial revision
gongo
parents:
diff changeset
114 }