home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.sbin / sendmail / src / arpadate.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-20  |  4.5 KB  |  178 lines

  1. /*
  2.  * Copyright (c) 1983 Eric P. Allman
  3.  * Copyright (c) 1988 Regents of the University of California.
  4.  * All rights reserved.
  5.  *
  6.  * Redistribution and use in source and binary forms, with or without
  7.  * modification, are permitted provided that the following conditions
  8.  * are met:
  9.  * 1. Redistributions of source code must retain the above copyright
  10.  *    notice, this list of conditions and the following disclaimer.
  11.  * 2. Redistributions in binary form must reproduce the above copyright
  12.  *    notice, this list of conditions and the following disclaimer in the
  13.  *    documentation and/or other materials provided with the distribution.
  14.  * 3. All advertising materials mentioning features or use of this software
  15.  *    must display the following acknowledgement:
  16.  *    This product includes software developed by the University of
  17.  *    California, Berkeley and its contributors.
  18.  * 4. Neither the name of the University nor the names of its contributors
  19.  *    may be used to endorse or promote products derived from this software
  20.  *    without specific prior written permission.
  21.  *
  22.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  23.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  26.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  27.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  28.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  29.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32.  * SUCH DAMAGE.
  33.  */
  34.  
  35. #ifndef lint
  36. static char sccsid[] = "@(#)arpadate.c    5.11 (Berkeley) 6/1/90";
  37. #endif /* not lint */
  38.  
  39. # include "conf.h"
  40. # include <time.h>
  41. # include <sys/types.h>
  42. # include "useful.h"
  43.  
  44. /*
  45. **  ARPADATE -- Create date in ARPANET format
  46. **
  47. **    Parameters:
  48. **        ud -- unix style date string.  if NULL, one is created.
  49. **
  50. **    Returns:
  51. **        pointer to an ARPANET date field
  52. **
  53. **    Side Effects:
  54. **        none
  55. **
  56. **    WARNING:
  57. **        date is stored in a local buffer -- subsequent
  58. **        calls will overwrite.
  59. **
  60. **    Bugs:
  61. **        Timezone is computed from local time, rather than
  62. **        from whereever (and whenever) the message was sent.
  63. **        To do better is very hard.
  64. **
  65. **        Some sites are now inserting the timezone into the
  66. **        local date.  This routine should figure out what
  67. **        the format is and work appropriately.
  68. */
  69.  
  70. char *
  71. arpadate(ud)
  72.     register char *ud;
  73. {
  74.     register char *p;
  75.     register char *q;
  76.     register int off;
  77.     register int i;
  78.     register struct tm *lt;
  79.     time_t t;
  80.     struct tm gmt;
  81.     static char b[40];
  82.     extern struct tm *localtime(), *gmtime();
  83.     extern char *ctime();
  84.     extern time_t time();
  85.  
  86.     /*
  87.     **  Get current time.
  88.     **    This will be used if a null argument is passed and
  89.     **    to resolve the timezone.
  90.     */
  91.  
  92.     (void) time(&t);
  93.     if (ud == NULL)
  94.         ud = ctime(&t);
  95.  
  96.     /*
  97.     **  Crack the UNIX date line in a singularly unoriginal way.
  98.     */
  99.  
  100.     q = b;
  101.  
  102.     p = &ud[0];        /* Mon */
  103.     *q++ = *p++;
  104.     *q++ = *p++;
  105.     *q++ = *p++;
  106.     *q++ = ',';
  107.     *q++ = ' ';
  108.  
  109.     p = &ud[8];        /* 16 */
  110.     if (*p == ' ')
  111.         p++;
  112.     else
  113.         *q++ = *p++;
  114.     *q++ = *p++;
  115.     *q++ = ' ';
  116.  
  117.     p = &ud[4];        /* Sep */
  118.     *q++ = *p++;
  119.     *q++ = *p++;
  120.     *q++ = *p++;
  121.     *q++ = ' ';
  122.  
  123.     p = &ud[22];        /* 79 */
  124.     *q++ = *p++;
  125.     *q++ = *p++;
  126.     *q++ = ' ';
  127.  
  128.     p = &ud[11];        /* 01:03:52 */
  129.     for (i = 8; i > 0; i--)
  130.         *q++ = *p++;
  131.  
  132.     /*
  133.      * should really get the timezone from the time in "ud" (which
  134.      * is only different if a non-null arg was passed which is different
  135.      * from the current time), but for all practical purposes, returning
  136.      * the current local zone will do (its all that is ever needed).
  137.      */
  138.     gmt = *gmtime(&t);
  139.     lt = localtime(&t);
  140.  
  141.     off = (lt->tm_hour - gmt.tm_hour) * 60 + lt->tm_min - gmt.tm_min;
  142.  
  143.     /* assume that offset isn't more than a day ... */
  144.     if (lt->tm_year < gmt.tm_year)
  145.         off -= 24 * 60;
  146.     else if (lt->tm_year > gmt.tm_year)
  147.         off += 24 * 60;
  148.     else if (lt->tm_yday < gmt.tm_yday)
  149.         off -= 24 * 60;
  150.     else if (lt->tm_yday > gmt.tm_yday)
  151.         off += 24 * 60;
  152.  
  153.     *q++ = ' ';
  154.     if (off == 0) {
  155.         *q++ = 'G';
  156.         *q++ = 'M';
  157.         *q++ = 'T';
  158.     } else {
  159.         if (off < 0) {
  160.             off = -off;
  161.             *q++ = '-';
  162.         } else
  163.             *q++ = '+';
  164.  
  165.         if (off >= 24*60)        /* should be impossible */
  166.             off = 23*60+59;        /* if not, insert silly value */
  167.  
  168.         *q++ = (off / 600) + '0';
  169.         *q++ = (off / 60) % 10 + '0';
  170.         off %= 60;
  171.         *q++ = (off / 10) + '0';
  172.         *q++ = (off % 10) + '0';
  173.     }
  174.     *q = '\0';
  175.  
  176.     return (b);
  177. }
  178.