home *** CD-ROM | disk | FTP | other *** search
/ RBBS in a Box Volume 1 #2 / RBBS_vol1_no2.iso / 014r / pdtar.zip / CTIME.C next >
C/C++ Source or Header  |  1988-05-16  |  2KB  |  82 lines

  1. /* 
  2.  * Original comment: "ctime.c   yah (yet another hack) from date.c"
  3.  * Note - this came from the comp.os.minix newsgroup, but I
  4.  * don't know who the author is; he got it from date.c, but somehow
  5.  * made a mistake in the number of seconds per year -- he thought
  6.  * there were 60 hours in a day.  So I fixed that, but other
  7.  * than that it's all the same... -- Eric Roskos
  8.  */
  9.  
  10.  
  11. int days_per_month[] =
  12.   { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  13. char *months[] =
  14.   { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  15.     "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
  16. char *days[] =
  17.   { "Thu", "Fri", "Sat", "Sun", "Mon", "Tue", "Wed" };
  18.  
  19. struct {
  20.     int year, month, day, hour, min, sec;
  21. } tm;
  22.  
  23. long s_p_min;
  24. long s_p_hour;
  25. long s_p_day;
  26. long s_p_year;
  27.  
  28. char t_buf[26];
  29.  
  30. char *ctime(t) 
  31. long *t;
  32. {
  33.   long tt;
  34.  
  35.   s_p_min  = 60L;
  36.   s_p_hour = 60L * 60L;
  37.   s_p_day  = 60L * 60L * 24L;
  38.   s_p_year = 60L * 60L * 24L * 365L;
  39.  
  40.   tt = *t;
  41.   cv_time(tt);
  42.  
  43.   sprintf(t_buf,"%s %s %02d %02d:%02d:%02d %04d\n", days[(tt / s_p_day) % 7], 
  44.            months[tm.month], tm.day, tm.hour, tm.min, tm.sec, tm.year); 
  45.   return(t_buf);
  46. }
  47.  
  48. cv_time(t)
  49. long t;
  50. {
  51.   tm.year = 0;
  52.   tm.month = 0;
  53.   tm.day = 1;
  54.   tm.hour = 0;
  55.   tm.min = 0;
  56.   tm.sec = 0;
  57.   while (t >= s_p_year) {
  58.     if (((tm.year + 2) % 4) == 0)
  59.         t -= s_p_day;
  60.     tm.year += 1;
  61.     t -= s_p_year;
  62.   }
  63.   if (((tm.year + 2) % 4) == 0)
  64.     days_per_month[1]++;
  65.   tm.year += 1970;
  66.   while ( t >= (days_per_month[tm.month] * s_p_day))
  67.     t -= days_per_month[tm.month++] * s_p_day;
  68.   while (t >= s_p_day) {
  69.     t -= s_p_day;
  70.     tm.day++;
  71.   }
  72.   while (t >= s_p_hour) {
  73.     t -= s_p_hour;
  74.     tm.hour++;
  75.   }
  76.   while (t >= s_p_min) {
  77.     t -= s_p_min;
  78.     tm.min++;
  79.   }
  80.   tm.sec = (int) t;
  81. }
  82.