ecutime.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*+-------------------------------------------------------------------------
  2. ecutime.c -- ecu time-related functions
  3. wht@wht.net
  4. Defined functions:
  5. elapsed_time_text(elapsed_seconds)
  6. epoch_secs_to_str(epoch_secs, type, buf)
  7. get_day(zflag)
  8. get_month(zflag)
  9. timeofday_text(type, buf)
  10. tod_plus_msec_text()
  11. Hofstadter's Law: It will always take longer, even if you take
  12. into consideration Hofstadter's Law. "Yeah, but July only
  13. seemed to last thirty minutes."
  14. --------------------------------------------------------------------------*/
  15. /*+:EDITS:*/
  16. /*:04-26-2000-11:15-wht@bob-RELEASE 4.42 */
  17. /*:12-12-1997-21:11-wht@kepler-complete isolation of substituted ftime */
  18. /*:01-24-1997-02:37-wht@yuriatin-SOURCE RELEASE 4.00 */
  19. /*:11-01-1996-18:30-wht@yuriatin-add type 11 to epoch_secs_to_str */
  20. /*:09-11-1996-20:00-wht@yuriatin-3.48-major telnet,curses,structural overhaul */
  21. /*:11-23-1995-11:20-wht@kepler-source control 3.37 for tsx-11 */
  22. /*:11-14-1995-10:23-wht@kepler-3.37.80-source control point: SOCKETS */
  23. /*:05-04-1994-04:39-wht@n4hgf-ECU release 3.30 */
  24. /*:03-02-1994-05:27-wht@n4hgf-add tod_plus_msec_text */
  25. /*:01-12-1994-07:17-wht@fep-move Ftime() to nap.c */
  26. /*:12-12-1993-13:11-wht@fep-use ecu_time.h + gettimeofday-based Ftime clone */
  27. /*:09-10-1992-13:59-wht@n4hgf-ECU release 3.20 */
  28. /*:08-22-1992-15:38-wht@n4hgf-ECU release 3.20 BETA */
  29. /*:07-25-1991-12:56-wht@n4hgf-ECU release 3.10 */
  30. /*:08-14-1990-20:40-wht@n4hgf-ecu3.00-flush old edit history */
  31. #include "ecu.h"
  32. struct tm *gmtime();
  33. struct tm *localtime();
  34. /*+-------------------------------------------------------------------------
  35. get_month(zflag) - month 1-12 - zflag true for UTC (Z)), else local time
  36. --------------------------------------------------------------------------*/
  37. int
  38. get_month(zflag)
  39. int zflag;
  40. {
  41. long epoch_secs = time((long *)0);
  42. struct tm *tod = (zflag) ? gmtime(&epoch_secs) : localtime(&epoch_secs);
  43. return (tod->tm_mon + 1);
  44. } /* end of get_month */
  45. /*+-------------------------------------------------------------------------
  46. get_day(zflag) - day 0-6 - zflag true for UTC (Z)), else local time
  47. --------------------------------------------------------------------------*/
  48. int
  49. get_day(zflag)
  50. int zflag;
  51. {
  52. long epoch_secs = time((long *)0);
  53. struct tm *tod = (zflag) ? gmtime(&epoch_secs) : localtime(&epoch_secs);
  54. return (tod->tm_wday);
  55. } /* end of get_day */
  56. /*+-----------------------------------------------------------------------
  57. char *epoch_secs_to_str(epoch_secs,type,buf)
  58. time of day types:
  59. 0 hh:mm
  60. 1 hh:mm:ss
  61. 2 mm-dd-yyyy hh:mm
  62. 3 mm-dd-yyyy hh:mm:ss
  63. 4 mm-dd-yyyy hh:mm:ss (UTC hh:mm)
  64. 5 mm-dd-yyyy
  65. 6 hh:mmZ
  66. 7 hh:mm:ssZ
  67. 8 mm-dd-yyyy in UTC
  68. 9 mm-dd
  69. returns 'buf' address
  70. ------------------------------------------------------------------------*/
  71. char *
  72. epoch_secs_to_str(epoch_secs, type, buf)
  73. long epoch_secs;
  74. int type;
  75. char *buf;
  76. {
  77. struct tm *tod = 0;
  78. switch(type)
  79. {
  80. case 0:
  81. case 1:
  82. case 2:
  83. case 3:
  84. case 4:
  85. case 5:
  86. case 9:
  87. tod = localtime(&epoch_secs);
  88. break;
  89. case 6:
  90. case 7:
  91. case 8:
  92. tod = gmtime(&epoch_secs);
  93. break;
  94. default:
  95. pprintf("logic error in epoch_secs_to_str: %d\n",type);
  96. termecu(TERMECU_LOGIC_ERROR);
  97. }
  98. switch (type)
  99. {
  100. default:
  101. case 0:
  102. case 6:
  103. sprintf(buf, "%02d:%02d", tod->tm_hour, tod->tm_min);
  104. if (type == 6)
  105. strcat(buf, "Z");
  106. break;
  107. case 1:
  108. case 7:
  109. sprintf(buf, "%02d:%02d:%02d", tod->tm_hour,
  110. tod->tm_min, tod->tm_sec);
  111. if (type == 7)
  112. strcat(buf, "Z");
  113. break;
  114. case 2:
  115. sprintf(buf, "%02d-%02d-%04d %02d:%02d",
  116. tod->tm_mon + 1, tod->tm_mday, tod->tm_year + 1900,
  117. tod->tm_hour, tod->tm_min);
  118. break;
  119. case 3:
  120. sprintf(buf, "%02d-%02d-%04d %02d:%02d:%02d",
  121. tod->tm_mon + 1, tod->tm_mday, tod->tm_year + 1900,
  122. tod->tm_hour, tod->tm_min, tod->tm_sec);
  123. break;
  124. case 4:
  125. sprintf(buf, "%02d-%02d-%04d %02d:%02d:%02d",
  126. tod->tm_mon + 1, tod->tm_mday, tod->tm_year + 1900,
  127. tod->tm_hour, tod->tm_min, tod->tm_sec);
  128. tod = gmtime(&epoch_secs);
  129. sprintf(&buf[strlen(buf)], " (UTC %02d:%02d)",
  130. tod->tm_hour, tod->tm_min);
  131. break;
  132. case 5:
  133. case 8:
  134. sprintf(buf, "%02d-%02d-%04d",
  135. tod->tm_mon + 1, tod->tm_mday, tod->tm_year + 1900);
  136. break;
  137. case 9:
  138. sprintf(buf, "%02d-%02d", tod->tm_mon + 1, tod->tm_mday);
  139. break;
  140. }
  141. return (buf);
  142. } /* end of epoch_secs_to_str */
  143. /*+-----------------------------------------------------------------------
  144. char *timeofday_text(type,buf)
  145. time of day types: (for refernce only; see epoch_secs_to_str())
  146. 0 hh:mm
  147. 1 hh:mm:ss
  148. 2 mm-dd-yyyy hh:mm
  149. 3 mm-dd-yyyy hh:mm:ss
  150. 4 mm-dd-yyyy hh:mm:ss (UTC hh:mm)
  151. 5 mm-dd-yyyy
  152. 6 hh:mmZ
  153. 7 hh:mm:ssZ
  154. 8 mm-dd-yyyy (UTC date)
  155. 9 mm-dd
  156. returns 'buf' address
  157. ------------------------------------------------------------------------*/
  158. char *
  159. timeofday_text(type, buf)
  160. int type;
  161. char *buf;
  162. {
  163. static char s128[128];
  164. if(!buf)
  165. buf = s128;
  166. return (epoch_secs_to_str(time((long *)0), type, buf));
  167. } /* end of timeofday_text */
  168. /*+-----------------------------------------------------------------------
  169. char *elapsed_time_text(elapsed_seconds)
  170. "hh:mm:ss" returned
  171. static string address is returned
  172. ------------------------------------------------------------------------*/
  173. char *
  174. elapsed_time_text(elapsed_seconds)
  175. long elapsed_seconds;
  176. {
  177. static char elapsed_time_str[40];
  178. long hh, mm, ss;
  179. hh = elapsed_seconds / 3600;
  180. elapsed_seconds -= hh * 3600;
  181. mm = elapsed_seconds / 60L;
  182. elapsed_seconds -= mm * 60L;
  183. ss = elapsed_seconds;
  184. sprintf(elapsed_time_str, "%02ld:%02ld:%02ld", hh, mm, ss);
  185. return (elapsed_time_str);
  186. } /* end of elapsed_time_text */
  187. /*+-------------------------------------------------------------------------
  188. tod_plus_msec_text()
  189. --------------------------------------------------------------------------*/
  190. char *
  191. tod_plus_msec_text()
  192. {
  193. static char buf[40];
  194. struct TIMEB tb;
  195. struct tm *tod = 0;
  196. Ftime(&tb);
  197. tod = localtime(&tb.time);
  198. sprintf(buf, "%02d:%02d:%02d.%03d",
  199. tod->tm_hour, tod->tm_min, tod->tm_sec, tb.millitm);
  200. return (buf);
  201. } /* end of tod_plus_msec_text */
  202. /* end of ecutime.c */
  203. /* vi: set tabstop=4 shiftwidth=4: */