Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

  1. /****************************************************************************
  2. *
  3. *                            Open Watcom Project
  4. *
  5. *    Portions Copyright (c) 1983-2002 Sybase, Inc. All Rights Reserved.
  6. *
  7. *  ========================================================================
  8. *
  9. *    This file contains Original Code and/or Modifications of Original
  10. *    Code as defined in and that are subject to the Sybase Open Watcom
  11. *    Public License version 1.0 (the 'License'). You may not use this file
  12. *    except in compliance with the License. BY USING THIS FILE YOU AGREE TO
  13. *    ALL TERMS AND CONDITIONS OF THE LICENSE. A copy of the License is
  14. *    provided with the Original Code and Modifications, and is also
  15. *    available at www.sybase.com/developer/opensource.
  16. *
  17. *    The Original Code and all software distributed under the License are
  18. *    distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  19. *    EXPRESS OR IMPLIED, AND SYBASE AND ALL CONTRIBUTORS HEREBY DISCLAIM
  20. *    ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF
  21. *    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR
  22. *    NON-INFRINGEMENT. Please see the License for the specific language
  23. *    governing rights and limitations under the License.
  24. *
  25. *  ========================================================================
  26. *
  27. * Description:  mktime() without timezone and struct tm fields adjustment.
  28. *               used by mktime() and DOS clock()
  29. *
  30. ****************************************************************************/
  31.  
  32. #include "variety.h"
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <time.h>
  36. #include <limits.h>
  37. #include "rtdata.h"
  38. #include "timedata.h"
  39.  
  40. #define MONTH_YR        ( 12 )
  41. #define DAY_YR          ( 365 )
  42. #define HOUR_YR         ( DAY_YR * 24 )
  43. #define MINUTE_YR       ( HOUR_YR * 60 )
  44. #define SECOND_YR       ( MINUTE_YR * 60 )
  45. #define __MONTHS        ( INT_MIN / MONTH_YR )
  46. #define __DAYS          ( INT_MIN / DAY_YR )
  47.  
  48. // these ones can underflow in 16bit environments,
  49. // so check the relative values first
  50. #if ( HOUR_YR ) < ( INT_MAX / 60 )
  51.  #define __MINUTES      ( INT_MIN / MINUTE_YR )
  52.  #if ( MINUTE_YR ) < ( INT_MAX / 60 )
  53.   #define __SECONDS     ( INT_MIN / SECOND_YR )
  54.  #else
  55.   #define __SECONDS     ( 0 )
  56.  #endif
  57. #else
  58.  #define __MINUTES      ( 0 )
  59.  #define __SECONDS      ( 0 )
  60. #endif
  61.  
  62. #define SMALLEST_YEAR_VALUE ( __MONTHS + __DAYS + __MINUTES + __SECONDS )
  63.  
  64. time_t __local_mktime( const struct tm *t, long *pdays, long *pseconds )
  65. {
  66.     int         month;
  67.     int         year;
  68.     long        days;
  69.     long        seconds;
  70.     short const *month_start;
  71.  
  72.     month_start = __diyr;
  73.     month = t->tm_mon % 12; /* put tm_mon into range */
  74.     year = t->tm_year;
  75.     if( year < SMALLEST_YEAR_VALUE )
  76.         return( ( time_t ) -1 );
  77.     year += t->tm_mon / 12;
  78.     while( month < 0 )
  79.         --year, month += 12;
  80.     if( year < 0 )
  81.         return( ( time_t ) -1 );
  82.     if( __leapyear( ( unsigned ) ( year + 1900 ) ) )
  83.         month_start = __dilyr;
  84.     days = year * 365L                   /* # of days in the years */
  85.         + ( ( year + 3 ) / 4 )           /* add # of leap years before year */
  86.         - ( ( year + 99 ) / 100 )        /* sub # of leap centuries */
  87.         + ( ( year + 399 - 100 ) / 400 ) /* add # of leap 4 centuries */
  88.                                          /* adjust for 1900 offset */
  89.                                          /* note: -100 == 300 (mod 400) */
  90.         + month_start[month]             /* # of days to 1st of month*/
  91.         + t->tm_mday - 1;                /* day of the month */
  92.     seconds = ( ( ( long ) ( t->tm_hour ) ) *60L + ( long ) ( t->tm_min ) ) *60L + t->tm_sec;
  93.                                          /* seconds needs to be positive for __brktime */
  94.     while( seconds < 0 )
  95.         days -= 1, seconds += ( long ) SECONDS_PER_DAY;
  96.     while( seconds >= ( long ) SECONDS_PER_DAY )
  97.         days += 1, seconds -= ( long ) SECONDS_PER_DAY;
  98.     if( days < ( DAYS_FROM_1900_TO_1970 - 1 ) )
  99.         return( ( time_t ) -1 );
  100.     if ( pdays ) *pdays = days;
  101.     if ( pseconds ) *pseconds = seconds;
  102.     return( seconds + ( days - DAYS_FROM_1900_TO_1970 ) * ( long ) SECONDS_PER_DAY );
  103. }
  104.