convtime.c
上传用户:xu_441
上传日期:2007-01-04
资源大小:1640k
文件大小:3k
源码类别:

Email客户端

开发平台:

Unix_Linux

  1. /*
  2.  * Copyright (c) 1998, 1999 Sendmail, Inc. and its suppliers.
  3.  * All rights reserved.
  4.  * Copyright (c) 1983, 1995-1997 Eric P. Allman.  All rights reserved.
  5.  * Copyright (c) 1988, 1993
  6.  * The Regents of the University of California.  All rights reserved.
  7.  *
  8.  * By using this file, you agree to the terms and conditions set
  9.  * forth in the LICENSE file which can be found at the top level of
  10.  * the sendmail distribution.
  11.  *
  12.  */
  13. #ifndef lint
  14. static char id[] = "@(#)$Id: convtime.c,v 8.25 1999/06/16 21:11:26 ca Exp $";
  15. #endif /* ! lint */
  16. #include <sendmail.h>
  17. /*
  18. **  CONVTIME -- convert time
  19. **
  20. ** Takes a time as an ascii string with a trailing character
  21. ** giving units:
  22. **   s -- seconds
  23. **   m -- minutes
  24. **   h -- hours
  25. **   d -- days (default)
  26. **   w -- weeks
  27. ** For example, "3d12h" is three and a half days.
  28. **
  29. ** Parameters:
  30. ** p -- pointer to ascii time.
  31. ** units -- default units if none specified.
  32. **
  33. ** Returns:
  34. ** time in seconds.
  35. **
  36. ** Side Effects:
  37. ** none.
  38. */
  39. time_t
  40. convtime(p, units)
  41. char *p;
  42. int units;
  43. {
  44. register time_t t, r;
  45. register char c;
  46. r = 0;
  47. if (strcasecmp(p, "now") == 0)
  48. return NOW;
  49. while (*p != '')
  50. {
  51. t = 0;
  52. while ((c = *p++) != '' && isascii(c) && isdigit(c))
  53. t = t * 10 + (c - '0');
  54. if (c == '')
  55. {
  56. c = units;
  57. p--;
  58. }
  59. else if (strchr("wdhms", c) == NULL)
  60. {
  61. usrerr("Invalid time unit `%c'", c);
  62. c = units;
  63. }
  64. switch (c)
  65. {
  66.   case 'w': /* weeks */
  67. t *= 7;
  68. /* FALLTHROUGH */
  69.   case 'd': /* days */
  70. /* FALLTHROUGH */
  71.   default:
  72. t *= 24;
  73. /* FALLTHROUGH */
  74.   case 'h': /* hours */
  75. t *= 60;
  76. /* FALLTHROUGH */
  77.   case 'm': /* minutes */
  78. t *= 60;
  79. /* FALLTHROUGH */
  80.   case 's': /* seconds */
  81. break;
  82. }
  83. r += t;
  84. }
  85. return r;
  86. }
  87. /*
  88. **  PINTVL -- produce printable version of a time interval
  89. **
  90. ** Parameters:
  91. ** intvl -- the interval to be converted
  92. ** brief -- if TRUE, print this in an extremely compact form
  93. ** (basically used for logging).
  94. **
  95. ** Returns:
  96. ** A pointer to a string version of intvl suitable for
  97. ** printing or framing.
  98. **
  99. ** Side Effects:
  100. ** none.
  101. **
  102. ** Warning:
  103. ** The string returned is in a static buffer.
  104. */
  105. #define PLURAL(n) ((n) == 1 ? "" : "s")
  106. char *
  107. pintvl(intvl, brief)
  108. time_t intvl;
  109. bool brief;
  110. {
  111. static char buf[256];
  112. register char *p;
  113. int wk, dy, hr, mi, se;
  114. if (intvl == 0 && !brief)
  115. return "zero seconds";
  116. if (intvl == NOW)
  117. return "too long";
  118. /* decode the interval into weeks, days, hours, minutes, seconds */
  119. se = intvl % 60;
  120. intvl /= 60;
  121. mi = intvl % 60;
  122. intvl /= 60;
  123. hr = intvl % 24;
  124. intvl /= 24;
  125. if (brief)
  126. {
  127. dy = intvl;
  128. wk = 0;
  129. }
  130. else
  131. {
  132. dy = intvl % 7;
  133. intvl /= 7;
  134. wk = intvl;
  135. }
  136. /* now turn it into a sexy form */
  137. p = buf;
  138. if (brief)
  139. {
  140. if (dy > 0)
  141. {
  142. (void) snprintf(p, SPACELEFT(buf, p), "%d+", dy);
  143. p += strlen(p);
  144. }
  145. (void) snprintf(p, SPACELEFT(buf, p), "%02d:%02d:%02d",
  146. hr, mi, se);
  147. return buf;
  148. }
  149. /* use the verbose form */
  150. if (wk > 0)
  151. {
  152. (void) snprintf(p, SPACELEFT(buf, p), ", %d week%s", wk, PLURAL(wk));
  153. p += strlen(p);
  154. }
  155. if (dy > 0)
  156. {
  157. (void) snprintf(p, SPACELEFT(buf, p), ", %d day%s", dy, PLURAL(dy));
  158. p += strlen(p);
  159. }
  160. if (hr > 0)
  161. {
  162. (void) snprintf(p, SPACELEFT(buf, p), ", %d hour%s", hr, PLURAL(hr));
  163. p += strlen(p);
  164. }
  165. if (mi > 0)
  166. {
  167. (void) snprintf(p, SPACELEFT(buf, p), ", %d minute%s", mi, PLURAL(mi));
  168. p += strlen(p);
  169. }
  170. if (se > 0)
  171. {
  172. (void) snprintf(p, SPACELEFT(buf, p), ", %d second%s", se, PLURAL(se));
  173. p += strlen(p);
  174. }
  175. return (buf + 2);
  176. }