OSDN Git Service

- update comment to clarify that this file is specific to AIX and 32-bit mode.
[pf3gnuchains/gcc-fork.git] / libchill / abstime.c
1 /* Implement timing-related runtime actions for CHILL.
2    Copyright (C) 1992,1993 Free Software Foundation, Inc.
3    Author: Wilfried Moser
4
5 This file is part of GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
20
21 /* As a special exception, if you link this library with other files,
22    some of which are compiled with GCC, to produce an executable,
23    this library does not by itself cause the resulting executable
24    to be covered by the GNU General Public License.
25    This exception does not however invalidate any other reasons why
26    the executable file might be covered by the GNU General Public License.  */
27
28 #include <time.h>
29 #include "rtltypes.h"
30
31 EXCEPTION (rangefail);
32
33 #define SECOND_VALID         1
34 #define MINUTE_VALID         2
35 #define HOUR_VALID           4
36 #define DAY_VALID            8
37 #define MONTH_VALID         16
38 #define YEAR_VALID          32
39
40 extern void __cause_ex1 (char *ex, char *file, int lineno);
41
42 #define CAUSE_RANGEFAIL     __cause_ex1 ("rangefail", filename, lineno)
43
44 /*
45  * function _abstime
46  *
47  * parameters:
48  *     mask - mask of valid values
49  *     year
50  *     month
51  *     day
52  *     hour
53  *     minute
54  *     second
55  *
56  * returns:
57  *     unsigned long
58  *
59  * exceptions:
60  *     rangefail
61  *
62  * abstract:
63  *     perform the ABSTIME builtin call
64  *
65  */
66
67 unsigned long
68 _abstime (mask, year, month, day, hour, minute, second,
69           filename, lineno)
70      int  mask, year, month, day, hour, minute, second;
71      char *filename;
72      int  lineno;
73 {
74   struct tm   *time_str;
75   time_t      result, current_time;
76
77   /* first of all get current time */
78   if ((current_time = time (0)) == (time_t)-1)
79     /* FIXME: what excpetion ?? */
80     CAUSE_RANGEFAIL;
81
82   /* if we just have to determine the current time, we are ready.
83      This is shown by mask == 0. */
84   if (mask == 0)
85     return (unsigned long)current_time;
86
87   /* convert current time to struct tm */
88   time_str = localtime (&current_time);
89
90   if (mask & YEAR_VALID)
91     {
92       if (year < 1900)
93         CAUSE_RANGEFAIL;
94       time_str->tm_year = year - 1900;
95     }
96
97   if (mask & MONTH_VALID)
98     {
99       if (month < 1 || month > 12)
100         CAUSE_RANGEFAIL;
101       time_str->tm_mon = month - 1;
102     }
103
104   if (mask & DAY_VALID)
105     {
106       if (day < 1 || day > 31)
107         CAUSE_RANGEFAIL;
108       time_str->tm_mday = day;
109     }
110
111   if (mask & HOUR_VALID)
112     {
113       if (hour < 0 || hour > 23)
114         CAUSE_RANGEFAIL;
115       time_str->tm_hour = hour;
116     }
117
118   if (mask & MINUTE_VALID)
119     {
120       if (minute < 0 || minute > 59)
121         CAUSE_RANGEFAIL;
122       time_str->tm_min = minute;
123     }
124
125   if (mask & SECOND_VALID)
126     {
127       if (second < 0 || second > 59)
128         CAUSE_RANGEFAIL;
129       time_str->tm_sec = second;
130     }
131
132   /* do it */
133   time_str->tm_isdst = -1;
134   if ((result = mktime (time_str)) == (time_t)-1)
135     CAUSE_RANGEFAIL;
136
137   return (unsigned long)result;
138 }