ctime.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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. #define TZSIZE 150
  50. static void readtimezone(void);
  51. static int rd_name(char**, char*);
  52. static int rd_long(char**, long*);
  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. localtime(const time_t *timp)
  69. {
  70. struct tm *ct;
  71. time_t t, tim;
  72. long *p;
  73. int i, dlflag;
  74. tim = *timp;
  75. if(timezone.stname[0] == 0)
  76. readtimezone();
  77. t = tim + timezone.stdiff;
  78. dlflag = 0;
  79. for(p = timezone.dlpairs; *p; p += 2)
  80. if(t >= p[0])
  81. if(t < p[1]) {
  82. t = tim + timezone.dldiff;
  83. dlflag++;
  84. break;
  85. }
  86. ct = gmtime(&t);
  87. ct->tm_isdst = dlflag;
  88. return ct;
  89. }
  90. struct tm*
  91. gmtime(const time_t *timp)
  92. {
  93. int d0, d1;
  94. long hms, day;
  95. time_t tim;
  96. static struct tm xtime;
  97. tim = *timp;
  98. /*
  99. * break initial number into days
  100. */
  101. hms = tim % 86400L;
  102. day = tim / 86400L;
  103. if(hms < 0) {
  104. hms += 86400L;
  105. day -= 1;
  106. }
  107. /*
  108. * generate hours:minutes:seconds
  109. */
  110. xtime.tm_sec = hms % 60;
  111. d1 = hms / 60;
  112. xtime.tm_min = d1 % 60;
  113. d1 /= 60;
  114. xtime.tm_hour = d1;
  115. /*
  116. * day is the day number.
  117. * generate day of the week.
  118. * The addend is 4 mod 7 (1/1/1970 was Thursday)
  119. */
  120. xtime.tm_wday = (day + 7340036L) % 7;
  121. /*
  122. * year number
  123. */
  124. if(day >= 0)
  125. for(d1 = 70; day >= dysize(d1); d1++)
  126. day -= dysize(d1);
  127. else
  128. for (d1 = 70; day < 0; d1--)
  129. day += dysize(d1-1);
  130. xtime.tm_year = d1;
  131. xtime.tm_yday = d0 = day;
  132. /*
  133. * generate month
  134. */
  135. if(dysize(d1) == 366)
  136. dmsize[1] = 29;
  137. for(d1 = 0; d0 >= dmsize[d1]; d1++)
  138. d0 -= dmsize[d1];
  139. dmsize[1] = 28;
  140. xtime.tm_mday = d0 + 1;
  141. xtime.tm_mon = d1;
  142. xtime.tm_isdst = 0;
  143. return &xtime;
  144. }
  145. char*
  146. asctime(const struct tm *t)
  147. {
  148. char *ncp;
  149. static char cbuf[30];
  150. strcpy(cbuf, "Thu Jan 01 00:00:00 1970\n");
  151. ncp = &"SunMonTueWedThuFriSat"[t->tm_wday*3];
  152. cbuf[0] = *ncp++;
  153. cbuf[1] = *ncp++;
  154. cbuf[2] = *ncp;
  155. ncp = &"JanFebMarAprMayJunJulAugSepOctNovDec"[t->tm_mon*3];
  156. cbuf[4] = *ncp++;
  157. cbuf[5] = *ncp++;
  158. cbuf[6] = *ncp;
  159. ct_numb(cbuf+8, t->tm_mday);
  160. ct_numb(cbuf+11, t->tm_hour+100);
  161. ct_numb(cbuf+14, t->tm_min+100);
  162. ct_numb(cbuf+17, t->tm_sec+100);
  163. if(t->tm_year >= 100) {
  164. cbuf[20] = '2';
  165. cbuf[21] = '0';
  166. }
  167. ct_numb(cbuf+22, t->tm_year+100);
  168. return cbuf;
  169. }
  170. static
  171. dysize(int y)
  172. {
  173. if((y%4) == 0)
  174. return 366;
  175. return 365;
  176. }
  177. static
  178. void
  179. ct_numb(char *cp, int n)
  180. {
  181. cp[0] = ' ';
  182. if(n >= 10)
  183. cp[0] = (n/10)%10 + '0';
  184. cp[1] = n%10 + '0';
  185. }
  186. static
  187. void
  188. readtimezone(void)
  189. {
  190. char buf[TZSIZE*11+30], *p;
  191. int i;
  192. memset(buf, 0, sizeof(buf));
  193. i = open("/env/timezone", 0);
  194. if(i < 0)
  195. goto error;
  196. if(read(i, buf, sizeof(buf)) >= sizeof(buf))
  197. goto error;
  198. close(i);
  199. p = buf;
  200. if(rd_name(&p, timezone.stname))
  201. goto error;
  202. if(rd_long(&p, &timezone.stdiff))
  203. goto error;
  204. if(rd_name(&p, timezone.dlname))
  205. goto error;
  206. if(rd_long(&p, &timezone.dldiff))
  207. goto error;
  208. for(i=0; i<TZSIZE; i++) {
  209. if(rd_long(&p, &timezone.dlpairs[i]))
  210. goto error;
  211. if(timezone.dlpairs[i] == 0)
  212. return;
  213. }
  214. error:
  215. timezone.stdiff = 0;
  216. strcpy(timezone.stname, "GMT");
  217. timezone.dlpairs[0] = 0;
  218. }
  219. static
  220. rd_name(char **f, char *p)
  221. {
  222. int c, i;
  223. for(;;) {
  224. c = *(*f)++;
  225. if(c != ' ' && c != '\n')
  226. break;
  227. }
  228. for(i=0; i<3; i++) {
  229. if(c == ' ' || c == '\n')
  230. return 1;
  231. *p++ = c;
  232. c = *(*f)++;
  233. }
  234. if(c != ' ' && c != '\n')
  235. return 1;
  236. *p = 0;
  237. return 0;
  238. }
  239. static
  240. rd_long(char **f, long *p)
  241. {
  242. int c, s;
  243. long l;
  244. s = 0;
  245. for(;;) {
  246. c = *(*f)++;
  247. if(c == '-') {
  248. s++;
  249. continue;
  250. }
  251. if(c != ' ' && c != '\n')
  252. break;
  253. }
  254. if(c == 0) {
  255. *p = 0;
  256. return 0;
  257. }
  258. l = 0;
  259. for(;;) {
  260. if(c == ' ' || c == '\n')
  261. break;
  262. if(c < '0' || c > '9')
  263. return 1;
  264. l = l*10 + c-'0';
  265. c = *(*f)++;
  266. }
  267. if(s)
  268. l = -l;
  269. *p = l;
  270. return 0;
  271. }