home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / f / ftp-102.zip / ftape-1.02 / qic / date.c < prev    next >
C/C++ Source or Header  |  1992-10-12  |  3KB  |  126 lines

  1. /* Translate to/from QIC and Unix date forms.
  2.    Copyright (C) 1992 David L. Brown, Jr, and Steve Haehnichen.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.    
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.    
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; see the file COPYING.  If not, write to
  16.    the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /*
  19.  * date.c,v 1.2 1992/10/13 01:55:04 dbrown Exp
  20.  *
  21.  * date.c,v
  22.  * Revision 1.2  1992/10/13  01:55:04  dbrown
  23.  * Added FSF copyright.
  24.  *
  25.  * Revision 1.1  1992/03/27  18:57:32  dbrown
  26.  * Initial revision
  27.  *
  28.  */
  29.  
  30. #define DATE_TEXT_LEN    14
  31.  
  32. #include <time.h>
  33. #include <sys/types.h>
  34. #include <string.h>
  35.  
  36. static char *months[] =
  37. {
  38.   "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  39.   "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
  40. };
  41.       
  42. /* Takes a ptr to a struct tm and returns a pointer to the string rep. */
  43. char*
  44. tm_to_ascii (struct tm *when)
  45. {
  46.   static char timebuf[DATE_TEXT_LEN];
  47.   struct tm *current_time;
  48.   int age;
  49.   long now;
  50.  
  51.   now = time ((time_t *) 0);
  52.   current_time = localtime (&now);
  53.   age = ((current_time->tm_year * 12 + current_time->tm_mon)
  54.      - (when->tm_year * 12 + when->tm_mon));
  55.   if (age > 6 || age < 0)
  56.     {
  57.       /* The file is fairly old (about 6 months) or in the future.
  58.      Show the year instead of the time of day.
  59.      Use two spaces in front of year, just like 'ls'. */
  60.       sprintf (timebuf, "%s %2d  %04d", months[when->tm_mon],
  61.            when->tm_mday, when->tm_year + 1900);
  62.     }
  63.   else
  64.     {
  65.       sprintf (timebuf, "%s %2d %02d:%02d", months[when->tm_mon],
  66.            when->tm_mday, when->tm_hour, when->tm_min);
  67.     }
  68.   return timebuf;
  69. }
  70.  
  71. /* Takes a QIC long and returns a pointer to a struct tm */
  72. struct tm*
  73. qic_to_tm (long qic_time)
  74. {
  75.   static struct tm when;
  76.   
  77.   when.tm_year = 70 + (qic_time >> 25);
  78.   qic_time &= 0x1ffffff;    /* Keep lowest 25 bits only */
  79.   when.tm_sec  = qic_time % 60;
  80.   qic_time /= 60;
  81.   when.tm_min  = qic_time % 60;
  82.   qic_time /= 60;
  83.   when.tm_hour = qic_time % 24;
  84.   qic_time /= 24;
  85.   when.tm_mday = (qic_time % 31) + 1;
  86.   qic_time /= 31;
  87.   when.tm_mon  = qic_time;
  88.   return &when;
  89. }
  90.  
  91. /* Takes a GMT time in seconds and returns a QIC long time. */
  92. long
  93. unix_to_qic_time (time_t unix_time)
  94. {
  95.   struct tm *when;
  96.   long qic_time;
  97.  
  98.   when = gmtime (&unix_time);
  99.   qic_time = ((when->tm_year - 70) << 25
  100.           | (when->tm_sec
  101.          + 60 * (when->tm_min
  102.              + 60 * (when->tm_hour
  103.                  + 24L * ((when->tm_mday - 1)
  104.                       + 31 * when->tm_mon)))));
  105.   return (qic_time);
  106. }
  107.  
  108. /* Returns the current local QIC long time. */
  109. long
  110. current_qic_time (void)
  111. {
  112.   time_t now;
  113.   
  114.   now = time ((time_t *) 0);
  115.   /* GMT + local-time offset */
  116.   return (unix_to_qic_time (time ((time_t *) 0)
  117.                 + localtime (&now)->tm_gmtoff));
  118. }
  119.  
  120. /* Takes a QIC long time and returns an ASCII representation */
  121. char*
  122. qic_to_text_time (long qic_time)
  123. {
  124.   return (tm_to_ascii (qic_to_tm (qic_time)));
  125. }
  126.