ctime.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * This routine converts time as follows.
  3. * The epoch is 0000 Jan 1 1970 GMT.
  4. * The argument time is in seconds since then.
  5. * The localtime(t) entry returns a pointer to an array
  6. * containing
  7. *
  8. * seconds (0-59)
  9. * minutes (0-59)
  10. * hours (0-23)
  11. * day of month (1-31)
  12. * month (0-11)
  13. * year-1970
  14. * weekday (0-6, Sun is 0)
  15. * day of the year
  16. * daylight savings flag
  17. *
  18. * The routine gets the daylight savings time from the environment.
  19. *
  20. * asctime(tvec))
  21. * where tvec is produced by localtime
  22. * returns a ptr to a character string
  23. * that has the ascii time in the form
  24. *
  25. * \\
  26. * Thu Jan 01 00:00:00 1970n0
  27. * 01234567890123456789012345
  28. * 0 1 2
  29. *
  30. * ctime(t) just calls localtime, then asctime.
  31. */
  32. #include <sys/types.h>
  33. #include <sys/stat.h>
  34. #include <fcntl.h>
  35. #include <time.h>
  36. #include <unistd.h>
  37. #include <string.h>
  38. static char dmsize[12] =
  39. {
  40. 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  41. };
  42. /*
  43. * The following table is used for 1974 and 1975 and
  44. * gives the day number of the first day after the Sunday of the
  45. * change.
  46. */
  47. static int dysize(int);
  48. static void ct_numb(char*, int);
  49. static void readtimezone(void);
  50. static int rd_name(char**, char*);
  51. static int rd_long(char**, long*);
  52. #define TZSIZE 150
  53. static
  54. struct
  55. {
  56. char stname[4];
  57. char dlname[4];
  58. long stdiff;
  59. long dldiff;
  60. long dlpairs[TZSIZE];
  61. } timezone;
  62. char*
  63. ctime(const time_t *t)
  64. {
  65. return asctime(localtime(t));
  66. }
  67. struct tm*
  68. gmtime_r(const time_t *timp, struct tm *result)
  69. {
  70. int d0, d1;
  71. long hms, day;
  72. time_t tim;
  73. tim = *timp;
  74. /*
  75. * break initial number into days
  76. */
  77. hms = tim % 86400L;
  78. day = tim / 86400L;
  79. if(hms < 0) {
  80. hms += 86400L;
  81. day -= 1;
  82. }
  83. /*
  84. * generate hours:minutes:seconds
  85. */
  86. result->tm_sec = hms % 60;
  87. d1 = hms / 60;
  88. result->tm_min = d1 % 60;
  89. d1 /= 60;
  90. result->tm_hour = d1;
  91. /*
  92. * day is the day number.
  93. * generate day of the week.
  94. * The addend is 4 mod 7 (1/1/1970 was Thursday)
  95. */
  96. result->tm_wday = (day + 7340036L) % 7;
  97. /*
  98. * year number
  99. */
  100. if(day >= 0)
  101. for(d1 = 70; day >= dysize(d1); d1++)
  102. day -= dysize(d1);
  103. else
  104. for (d1 = 70; day < 0; d1--)
  105. day += dysize(d1-1);
  106. result->tm_year = d1;
  107. result->tm_yday = d0 = day;
  108. /*
  109. * generate month
  110. */
  111. if(dysize(d1) == 366)
  112. dmsize[1] = 29;
  113. for(d1 = 0; d0 >= dmsize[d1]; d1++)
  114. d0 -= dmsize[d1];
  115. dmsize[1] = 28;
  116. result->tm_mday = d0 + 1;
  117. result->tm_mon = d1;
  118. result->tm_isdst = 0;
  119. return result;
  120. }
  121. struct tm*
  122. gmtime(const time_t *timp)
  123. {
  124. static struct tm xtime;
  125. return gmtime_r(timp, &xtime);
  126. }
  127. struct tm*
  128. localtime_r(const time_t *timp, struct tm *result)
  129. {
  130. struct tm *ct;
  131. time_t t, tim;
  132. long *p;
  133. int i, dlflag;
  134. tim = *timp;
  135. if(timezone.stname[0] == 0)
  136. readtimezone();
  137. t = tim + timezone.stdiff;
  138. dlflag = 0;
  139. for(p = timezone.dlpairs; *p; p += 2)
  140. if(t >= p[0])
  141. if(t < p[1]) {
  142. t = tim + timezone.dldiff;
  143. dlflag++;
  144. break;
  145. }
  146. ct = gmtime_r(&t, result);
  147. ct->tm_isdst = dlflag;
  148. return ct;
  149. }
  150. struct tm*
  151. localtime(const time_t *timp)
  152. {
  153. static struct tm xtime;
  154. return localtime_r(timp, &xtime);
  155. }
  156. char*
  157. asctime_r(const struct tm *t, char *buf)
  158. {
  159. char *ncp;
  160. strcpy(buf, "Thu Jan 01 00:00:00 1970\n");
  161. ncp = &"SunMonTueWedThuFriSat"[t->tm_wday*3];
  162. buf[0] = *ncp++;
  163. buf[1] = *ncp++;
  164. buf[2] = *ncp;
  165. ncp = &"JanFebMarAprMayJunJulAugSepOctNovDec"[t->tm_mon*3];
  166. buf[4] = *ncp++;
  167. buf[5] = *ncp++;
  168. buf[6] = *ncp;
  169. ct_numb(buf+8, t->tm_mday);
  170. ct_numb(buf+11, t->tm_hour+100);
  171. ct_numb(buf+14, t->tm_min+100);
  172. ct_numb(buf+17, t->tm_sec+100);
  173. if(t->tm_year >= 100) {
  174. buf[20] = '2';
  175. buf[21] = '0';
  176. }
  177. ct_numb(buf+22, t->tm_year+100);
  178. return buf;
  179. }
  180. char*
  181. asctime(const struct tm *t)
  182. {
  183. static char cbuf[30];
  184. return asctime_r(t, cbuf);
  185. }
  186. static
  187. dysize(int y)
  188. {
  189. if((y%4) == 0)
  190. return 366;
  191. return 365;
  192. }
  193. static
  194. void
  195. ct_numb(char *cp, int n)
  196. {
  197. cp[0] = ' ';
  198. if(n >= 10)
  199. cp[0] = (n/10)%10 + '0';
  200. cp[1] = n%10 + '0';
  201. }
  202. static
  203. void
  204. readtimezone(void)
  205. {
  206. char buf[TZSIZE*11+30], *p;
  207. int i;
  208. memset(buf, 0, sizeof(buf));
  209. i = open("/env/timezone", 0);
  210. if(i < 0)
  211. goto error;
  212. if(read(i, buf, sizeof(buf)) >= sizeof(buf))
  213. goto error;
  214. close(i);
  215. p = buf;
  216. if(rd_name(&p, timezone.stname))
  217. goto error;
  218. if(rd_long(&p, &timezone.stdiff))
  219. goto error;
  220. if(rd_name(&p, timezone.dlname))
  221. goto error;
  222. if(rd_long(&p, &timezone.dldiff))
  223. goto error;
  224. for(i=0; i<TZSIZE; i++) {
  225. if(rd_long(&p, &timezone.dlpairs[i]))
  226. goto error;
  227. if(timezone.dlpairs[i] == 0)
  228. return;
  229. }
  230. error:
  231. timezone.stdiff = 0;
  232. strcpy(timezone.stname, "GMT");
  233. timezone.dlpairs[0] = 0;
  234. }
  235. static
  236. rd_name(char **f, char *p)
  237. {
  238. int c, i;
  239. for(;;) {
  240. c = *(*f)++;
  241. if(c != ' ' && c != '\n')
  242. break;
  243. }
  244. for(i=0; i<3; i++) {
  245. if(c == ' ' || c == '\n')
  246. return 1;
  247. *p++ = c;
  248. c = *(*f)++;
  249. }
  250. if(c != ' ' && c != '\n')
  251. return 1;
  252. *p = 0;
  253. return 0;
  254. }
  255. static
  256. rd_long(char **f, long *p)
  257. {
  258. int c, s;
  259. long l;
  260. s = 0;
  261. for(;;) {
  262. c = *(*f)++;
  263. if(c == '-') {
  264. s++;
  265. continue;
  266. }
  267. if(c != ' ' && c != '\n')
  268. break;
  269. }
  270. if(c == 0) {
  271. *p = 0;
  272. return 0;
  273. }
  274. l = 0;
  275. for(;;) {
  276. if(c == ' ' || c == '\n')
  277. break;
  278. if(c < '0' || c > '9')
  279. return 1;
  280. l = l*10 + c-'0';
  281. c = *(*f)++;
  282. }
  283. if(s)
  284. l = -l;
  285. *p = l;
  286. return 0;
  287. }