porttime.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #include "all.h"
  2. static int sunday(Tm *t, int d);
  3. static int dysize(int);
  4. static void ct_numb(char*, int);
  5. static void klocaltime(long tim, Tm *ct);
  6. static void kgmtime(long tim, Tm *ct);
  7. static char dmsize[12] =
  8. {
  9. 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
  10. };
  11. /*
  12. * The following table is used for 1974 and 1975 and
  13. * gives the day number of the first day after the Sunday of the
  14. * change.
  15. */
  16. static struct
  17. {
  18. short yrfrom;
  19. short yrto;
  20. short daylb;
  21. short dayle;
  22. } daytab[] =
  23. {
  24. 87, 999, 97, 303,
  25. 76, 86, 119, 303,
  26. 75, 75, 58, 303,
  27. 74, 74, 5, 333,
  28. 0, 73, 119, 303,
  29. };
  30. static struct
  31. {
  32. short minuteswest; /* minutes west of Greenwich */
  33. short dsttime; /* dst correction */
  34. } timezone =
  35. {
  36. 5*60, 1
  37. };
  38. static void
  39. klocaltime(long tim, Tm *ct)
  40. {
  41. int daylbegin, daylend, dayno, i;
  42. long copyt;
  43. copyt = tim - timezone.minuteswest*60L;
  44. kgmtime(copyt, ct);
  45. dayno = ct->yday;
  46. for(i=0;; i++)
  47. if(ct->year >= daytab[i].yrfrom &&
  48. ct->year <= daytab[i].yrto) {
  49. daylbegin = sunday(ct, daytab[i].daylb);
  50. daylend = sunday(ct, daytab[i].dayle);
  51. break;
  52. }
  53. if(timezone.dsttime &&
  54. (dayno>daylbegin || (dayno==daylbegin && ct->hour>=2)) &&
  55. (dayno<daylend || (dayno==daylend && ct->hour<1))) {
  56. copyt += 60L*60L;
  57. kgmtime(copyt, ct);
  58. }
  59. }
  60. /*
  61. * The argument is a 0-origin day number.
  62. * The value is the day number of the last
  63. * Sunday before or after the day.
  64. */
  65. static
  66. sunday(Tm *t, int d)
  67. {
  68. if(d >= 58)
  69. d += dysize(t->year) - 365;
  70. return d - (d - t->yday + t->wday + 700) % 7;
  71. }
  72. static void
  73. kgmtime(long tim, Tm *ct)
  74. {
  75. int d0, d1;
  76. long hms, day;
  77. /*
  78. * break initial number into days
  79. */
  80. hms = tim % 86400L;
  81. day = tim / 86400L;
  82. if(hms < 0) {
  83. hms += 86400L;
  84. day -= 1;
  85. }
  86. /*
  87. * generate hours:minutes:seconds
  88. */
  89. ct->sec = hms % 60;
  90. d1 = hms / 60;
  91. ct->min = d1 % 60;
  92. d1 /= 60;
  93. ct->hour = d1;
  94. /*
  95. * day is the day number.
  96. * generate day of the week.
  97. * The addend is 4 mod 7 (1/1/1970 was Thursday)
  98. */
  99. ct->wday = (day + 7340036L) % 7;
  100. /*
  101. * year number
  102. */
  103. if(day >= 0)
  104. for(d1 = 70; day >= dysize(d1); d1++)
  105. day -= dysize(d1);
  106. else
  107. for (d1 = 70; day < 0; d1--)
  108. day += dysize(d1-1);
  109. ct->year = d1;
  110. ct->yday = d0 = day;
  111. /*
  112. * generate month
  113. */
  114. if(dysize(d1) == 366)
  115. dmsize[1] = 29;
  116. for(d1 = 0; d0 >= dmsize[d1]; d1++)
  117. d0 -= dmsize[d1];
  118. dmsize[1] = 28;
  119. ct->mday = d0 + 1;
  120. ct->mon = d1;
  121. }
  122. void
  123. datestr(char *s, long t)
  124. {
  125. Tm tm;
  126. klocaltime(t, &tm);
  127. sprint(s, "%.4d%.2d%.2d", tm.year+1900, tm.mon+1, tm.mday);
  128. }
  129. int
  130. Tfmt(Fmt *f1)
  131. {
  132. char s[30];
  133. char *cp;
  134. long t;
  135. Tm tm;
  136. t = va_arg(f1->args, long);
  137. if(t == 0)
  138. return fmtstrcpy(f1, "The Epoch");
  139. klocaltime(t, &tm);
  140. strcpy(s, "Day Mon 00 00:00:00 1900");
  141. cp = &"SunMonTueWedThuFriSat"[tm.wday*3];
  142. s[0] = cp[0];
  143. s[1] = cp[1];
  144. s[2] = cp[2];
  145. cp = &"JanFebMarAprMayJunJulAugSepOctNovDec"[tm.mon*3];
  146. s[4] = cp[0];
  147. s[5] = cp[1];
  148. s[6] = cp[2];
  149. ct_numb(s+8, tm.mday);
  150. ct_numb(s+11, tm.hour+100);
  151. ct_numb(s+14, tm.min+100);
  152. ct_numb(s+17, tm.sec+100);
  153. if(tm.year >= 100) {
  154. s[20] = '2';
  155. s[21] = '0';
  156. }
  157. ct_numb(s+22, tm.year+100);
  158. return fmtstrcpy(f1, s);
  159. }
  160. static
  161. dysize(int y)
  162. {
  163. if((y%4) == 0)
  164. return 366;
  165. return 365;
  166. }
  167. static
  168. void
  169. ct_numb(char *cp, int n)
  170. {
  171. if(n >= 10)
  172. cp[0] = (n/10)%10 + '0';
  173. else
  174. cp[0] = ' ';
  175. cp[1] = n%10 + '0';
  176. }
  177. /*
  178. * compute the next time after t
  179. * that has hour hr and is not on
  180. * day in bitpattern --
  181. * for automatic dumps
  182. */
  183. long
  184. nextime(long t, int hr, int day)
  185. {
  186. Tm tm;
  187. int nhr;
  188. if(hr < 0 || hr >= 24)
  189. hr = 5;
  190. if((day&0x7f) == 0x7f)
  191. day = 0;
  192. loop:
  193. klocaltime(t, &tm);
  194. t -= tm.sec;
  195. t -= tm.min*60;
  196. nhr = tm.hour;
  197. do {
  198. t += 60*60;
  199. nhr++;
  200. } while(nhr%24 != hr);
  201. klocaltime(t, &tm);
  202. if(tm.hour != hr) {
  203. t += 60*60;
  204. klocaltime(t, &tm);
  205. if(tm.hour != hr) {
  206. t -= 60*60;
  207. klocaltime(t, &tm);
  208. }
  209. }
  210. if(day & (1<<tm.wday)) {
  211. t += 12*60*60;
  212. goto loop;
  213. }
  214. return t;
  215. }