OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [tags/] [tn_m001/] [newlib/] [newlib/] [libc/] [time/] [mktime.c] - Blame information for rev 39

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 39 lampret
/*
2
 * mktime.c
3
 * Original Author:     G. Haley
4
 *
5
 * Converts the broken-down time, expressed as local time, in the structure
6
 * pointed to by tim_p into a calendar time value. The original values of the
7
 * tm_wday and tm_yday fields of the structure are ignored, and the original
8
 * values of the other fields have no restrictions. On successful completion
9
 * the fields of the structure are set to represent the specified calendar
10
 * time. Returns the specified calendar time. If the calendar time can not be
11
 * represented, returns the value (time_t) -1.
12
 */
13
 
14
/*
15
FUNCTION
16
<<mktime>>---convert time to arithmetic representation
17
 
18
INDEX
19
        mktime
20
 
21
ANSI_SYNOPSIS
22
        #include <time.h>
23
        time_t mktime(struct tm *<[timp]>);
24
 
25
TRAD_SYNOPSIS
26
        #include <time.h>
27
        time_t mktime(<[timp]>)
28
        struct tm *<[timp]>;
29
 
30
DESCRIPTION
31
<<mktime>> assumes the time at <[timp]> is a local time, and converts
32
its representation from the traditional representation defined by
33
<<struct tm>> into a representation suitable for arithmetic.
34
 
35
<<localtime>> is the inverse of <<mktime>>.
36
 
37
RETURNS
38
If the contents of the structure at <[timp]> do not form a valid
39
calendar time representation, the result is <<-1>>.  Otherwise, the
40
result is the time, converted to a <<time_t>> value.
41
 
42
PORTABILITY
43
ANSI C requires <<mktime>>.
44
 
45
<<mktime>> requires no supporting OS subroutines.
46
*/
47
 
48
#include <stdlib.h>
49
#include <time.h>
50
 
51
#define _SEC_IN_MINUTE 60
52
#define _SEC_IN_HOUR 3600
53
#define _SEC_IN_DAY 86400
54
 
55
static _CONST int DAYS_IN_MONTH[12] =
56
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
57
 
58
#define _DAYS_IN_MONTH(x) ((x == 1) ? days_in_feb : DAYS_IN_MONTH[x])
59
 
60
static _CONST int _DAYS_BEFORE_MONTH[12] =
61
{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
62
 
63
#define _DAYS_IN_YEAR(year) (((year) % 4) ? 365 : 366)
64
 
65
static void
66
validate_structure (tim_p)
67
     struct tm *tim_p;
68
{
69
  div_t res;
70
  int days_in_feb = 28;
71
 
72
  /* calculate time & date to account for out of range values */
73
  if (tim_p->tm_sec < 0 || tim_p->tm_sec > 59)
74
    {
75
      res = div (tim_p->tm_sec, 60);
76
      tim_p->tm_min += res.quot;
77
      if ((tim_p->tm_sec = res.rem) < 0)
78
        tim_p->tm_sec += 60;
79
    }
80
 
81
  if (tim_p->tm_min < 0 || tim_p->tm_min > 59)
82
    {
83
      res = div (tim_p->tm_min, 60);
84
      tim_p->tm_hour += res.quot;
85
      if ((tim_p->tm_min = res.rem) < 0)
86
        tim_p->tm_min += 60;
87
    }
88
 
89
  if (tim_p->tm_hour < 0 || tim_p->tm_hour > 23)
90
    {
91
      res = div (tim_p->tm_hour, 24);
92
      tim_p->tm_mday += res.quot;
93
      if ((tim_p->tm_hour = res.rem) < 0)
94
        tim_p->tm_hour += 24;
95
    }
96
 
97
  if (tim_p->tm_mon > 11)
98
    {
99
      res = div (tim_p->tm_mon, 12);
100
      tim_p->tm_year += res.quot;
101
      if ((tim_p->tm_mon = res.rem) < 0)
102
        tim_p->tm_mon += 12;
103
    }
104
 
105
  if (_DAYS_IN_YEAR (tim_p->tm_year) == 366)
106
    days_in_feb = 29;
107
 
108
  if (tim_p->tm_mday < 0)
109
    {
110
      while (tim_p->tm_mday < 0)
111
        {
112
          tim_p->tm_mday += _DAYS_IN_MONTH (tim_p->tm_mon);
113
          if (--tim_p->tm_mon == -1)
114
            {
115
              tim_p->tm_year--;
116
              tim_p->tm_mon = 12;
117
              days_in_feb =
118
                ((_DAYS_IN_YEAR (tim_p->tm_year) == 366) ?
119
                 29 : 28);
120
            }
121
        }
122
    }
123
  else
124
    {
125
      while (tim_p->tm_mday > _DAYS_IN_MONTH (tim_p->tm_mon))
126
        {
127
          tim_p->tm_mday -= _DAYS_IN_MONTH (tim_p->tm_mon);
128
          if (++tim_p->tm_mon == 12)
129
            {
130
              tim_p->tm_year++;
131
              tim_p->tm_mon = 0;
132
              days_in_feb =
133
                ((_DAYS_IN_YEAR (tim_p->tm_year) == 366) ?
134
                 29 : 28);
135
            }
136
        }
137
    }
138
}
139
 
140
time_t
141
mktime (tim_p)
142
     struct tm *tim_p;
143
{
144
  time_t tim = 0;
145
  long days = 0;
146
  int year;
147
 
148
  /* validate structure */
149
  validate_structure (tim_p);
150
 
151
  /* compute hours, minutes, seconds */
152
  tim += tim_p->tm_sec + (tim_p->tm_min * _SEC_IN_MINUTE) +
153
    (tim_p->tm_hour * _SEC_IN_HOUR);
154
 
155
  /* compute days in year */
156
  days += tim_p->tm_mday - 1;
157
  days += _DAYS_BEFORE_MONTH[tim_p->tm_mon];
158
  if (tim_p->tm_mon > 1 && _DAYS_IN_YEAR (tim_p->tm_year) == 366)
159
    days++;
160
 
161
  /* compute day of the year */
162
  tim_p->tm_yday = days;
163
 
164
  if (tim_p->tm_year > 10000
165
      || tim_p->tm_year < -10000)
166
    {
167
      return (time_t) -1;
168
    }
169
 
170
  /* compute days in other years */
171
  if (tim_p->tm_year > 70)
172
    {
173
      for (year = 70; year < tim_p->tm_year; year++)
174
        days += _DAYS_IN_YEAR (year);
175
    }
176
  else if (tim_p->tm_year < 70)
177
    {
178
      for (year = 69; year > tim_p->tm_year; year--)
179
        days -= _DAYS_IN_YEAR (year);
180
      days -= _DAYS_IN_YEAR (year);
181
    }
182
 
183
  /* compute day of the week */
184
  if ((tim_p->tm_wday = (days + 4) % 7) < 0)
185
    tim_p->tm_wday += 7;
186
 
187
  /* compute total seconds */
188
  tim += (days * _SEC_IN_DAY);
189
 
190
  return tim;
191
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.