home *** CD-ROM | disk | FTP | other *** search
/ Aminet 18 / aminetcdnumber181997.iso / Aminet / text / hyper / hsc_source.lha / hsc / source / ugly / utime.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-12-06  |  3.6 KB  |  160 lines

  1. /*
  2.  * ugly/utime.c
  3.  *
  4.  * misc. time-handling functions
  5.  *
  6.  * Copyright (C) 1994,95,96  Thomas Aglassinger
  7.  *
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 2 of the License, or
  11.  * (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this program; if not, write to the Free Software
  20.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  *
  22.  * updated:  6-Dec-1996
  23.  * created:  6-Jun-1996
  24.  *
  25.  *-------------------------------------------------------------------
  26.  *
  27.  */
  28.  
  29. #include <stdlib.h>
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <time.h>
  33.  
  34. #include "utypes.h"
  35. #include "umemory.h"
  36. #include "ustring.h"
  37. #include "expstr.h"
  38.  
  39. #define NOEXTERN_UGLY_UTIME_H
  40. #include "utime.h"
  41.  
  42. /* debugging defines */
  43. #ifdef D
  44. #undef D
  45. #endif
  46.  
  47. #if DEBUG_UGLY_TIME
  48. #define D(x) x
  49. #define DUT "*utime * "
  50. #else
  51. #define D(x)                    /* nufin */
  52. #endif
  53.  
  54. BOOL time2estr(EXPSTR * dest, struct tm *time)
  55. {
  56. #define TIMEBUF_INC_STR "12345678901234567890"
  57.  
  58.     /*localtime(&(hp->start_time)) ); */
  59.     BOOL ok = TRUE;
  60.     size_t strftrc = 0;
  61.  
  62.     /* reset destination string */
  63.     set_estr(dest, TIMEBUF_INC_STR);
  64.  
  65.     /* write time to string */
  66.     while (ok && !strftrc)
  67.     {
  68.         /* expand dest */
  69.         ok = app_estr(dest, TIMEBUF_INC_STR);
  70.  
  71.         D(fprintf(stderr, DUT "dest: inc+%ld\n",
  72.                   strlen(TIMEBUF_INC_STR)));
  73.  
  74.         /* write string */
  75.         strftrc = strftime(estr2str(dest), estrlen(dest),
  76.                            UGLY_TIME_TEMPLATE_STRFTIME, time);
  77.     }
  78.  
  79.     D(if (!ok) fprintf(stderr, DUT "time2estr: FAILED\n"));
  80.  
  81.     return (ok);
  82. }
  83.  
  84. VOID clrtime(struct tm * time)
  85. {
  86.     time->tm_sec = 0;
  87.     time->tm_min = 0;
  88.     time->tm_hour = 0;
  89.     time->tm_mday = 0;
  90.     time->tm_mon = 0;
  91.     time->tm_year = 0;
  92.     time->tm_wday = 0;
  93.     time->tm_yday = 0;
  94.     time->tm_isdst = 0;
  95. }
  96.  
  97. BOOL str2time(struct tm *time, STRPTR timestr)
  98. {
  99.     BOOL ok = FALSE;
  100.     int scanrc = EOF;
  101.  
  102.     printf("testing ugly time funtions\n");
  103.  
  104.     clrtime(time);
  105.  
  106.     /* convert string to time */
  107.     scanrc = sscanf(timestr, UGLY_TIME_TEMPLATE_SSCANF,
  108.                     &time->tm_year, &time->tm_mon, &time->tm_mday,
  109.                     &time->tm_hour, &time->tm_min, &time->tm_sec);
  110.  
  111.     if (scanrc != EOF)
  112.     {
  113.         time_t ttime;
  114.  
  115.         /* adjust values */
  116.         time->tm_year -= 1990;
  117.         time->tm_mon--;
  118.         time->tm_mday--;
  119.  
  120.         /* fill rest of time-structure */
  121.         ttime = mktime(time);
  122.  
  123.         if (ttime != ((time_t) - 1))
  124.         {
  125.  
  126.             struct tm *ltime = localtime(&ttime);
  127.  
  128.             D(
  129.                  {
  130.                  STRARR timebuf2[200];
  131.                  strftime(timebuf2, 200, UGLY_TIME_TEMPLATE_STRFTIME, ltime);
  132.                  fprintf(stderr, DUT "ltime = `%s'\n", timebuf2);
  133.                  }
  134.             );
  135.  
  136.             if (ltime)
  137.             {
  138.                 memcpy(time, ltime, sizeof(struct tm));
  139.                 ok = TRUE;
  140.             }
  141.             else
  142.             {
  143.                 D(fprintf(stderr, DUT "str2time: localtime() FAILED\n"));
  144.             }
  145.  
  146.         }
  147.         else
  148.         {
  149.             D(fprintf(stderr, DUT "str2time: mktime() FAILED\n"));
  150.         }
  151.     }
  152.     else
  153.     {
  154.         D(fprintf(stderr, DUT "str2time: sscanf() FAILED\n"));
  155.     }
  156.  
  157.     return (ok);
  158. }
  159.  
  160.