~ubuntu-branches/ubuntu/maverick/libcdio/maverick

« back to all changes in this revision

Viewing changes to lib/udf/udf_time.c

  • Committer: Bazaar Package Importer
  • Author(s): Nicolas Boullis
  • Date: 2007-10-04 00:52:35 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20071004005235-4e3gdi4x2q2d14jx
Tags: 0.78.2+dfsg1-1
* Repack the source tarball to remove non-DFSG-free
  documentation. Thanks to Joerg Jaspert for pointing this.
* Also update debian/copyright to reflect the status of the removed
  documentation.
* Add libncurses5-dev | libncurses-dev to the build-dependencies, for
  cdda-player.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
    Copyright (C) 2005 Rocky Bernstein <rocky@panix.com>
 
3
    Copyright (C) 1993, 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
 
4
 
 
5
    Modified From part of the GNU C Library.
 
6
    Contributed by Paul Eggert (eggert@twinsun.com).
 
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
21
*/
 
22
 
 
23
/* Some history from the GNU/Linux kernel from which this is also taken...
 
24
   dgb 10/02/98: ripped this from glibc source to help convert
 
25
                 timestamps to unix time
 
26
 
 
27
       10/04/98: added new table-based lookup after seeing how ugly the
 
28
                 gnu code is
 
29
  
 
30
   blf 09/27/99: ripped out all the old code and inserted new table from
 
31
                 John Brockmeyer (without leap second corrections)
 
32
                 rewrote udf_stamp_to_time and fixed timezone
 
33
                 accounting in udf_timespec_to_stamp.
 
34
*/
 
35
 
 
36
/*
 
37
 * We don't take into account leap seconds. This may be correct or incorrect.
 
38
 * For more NIST information (especially dealing with leap seconds), see:
 
39
 *  http://www.boulder.nist.gov/timefreq/pubs/bulletin/leapsecond.htm
 
40
 */
 
41
 
 
42
#ifdef HAVE_CONFIG_H
 
43
#include "config.h"
 
44
#endif
 
45
 
 
46
#ifdef NEED_TIMEZONEVAR
 
47
#define timezonevar 1
 
48
#endif
 
49
 
 
50
#include "udf_private.h"
 
51
#include <cdio/udf.h>
 
52
 
 
53
/**
 
54
   Imagine the below enum values as #define'd or constant values
 
55
   rather than distinct values of an enum.
 
56
*/
 
57
enum {
 
58
  HOURS_PER_DAY    =   24,
 
59
  SECS_PER_MINUTE  =   60,
 
60
  MAX_YEAR_SECONDS =   69,
 
61
  DAYS_PER_YEAR    =  365,  /* That is, in most of the years. */
 
62
  EPOCH_YEAR       = 1970,
 
63
  SECS_PER_HOUR    = (60 * SECS_PER_MINUTE),
 
64
  SECS_PER_DAY     = SECS_PER_HOUR * HOURS_PER_DAY
 
65
} debug_udf_time_enum;
 
66
  
 
67
#ifndef __isleap
 
68
/* Nonzero if YEAR is a leap year (every 4 years,
 
69
   except every 100th isn't, and every 400th is).  */
 
70
#define __isleap(year)  \
 
71
  ((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))
 
72
#endif
 
73
 
 
74
/* How many days come before each month (0-12).  */
 
75
static const unsigned short int __mon_yday[2][13] =
 
76
  {
 
77
    /* Normal years.  */
 
78
    { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, DAYS_PER_YEAR },
 
79
    /* Leap years.  */
 
80
    { 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, DAYS_PER_YEAR+1 }
 
81
  };
 
82
 
 
83
#define SPY(y,l,s) (SECS_PER_DAY * (DAYS_PER_YEAR*y+l)+s) /* Seconds per year */
 
84
 
 
85
static time_t year_seconds[MAX_YEAR_SECONDS]= {
 
86
  /*1970*/ SPY( 0, 0,0), SPY( 1, 0,0), SPY( 2, 0,0), SPY( 3, 1,0), 
 
87
  /*1974*/ SPY( 4, 1,0), SPY( 5, 1,0), SPY( 6, 1,0), SPY( 7, 2,0), 
 
88
  /*1978*/ SPY( 8, 2,0), SPY( 9, 2,0), SPY(10, 2,0), SPY(11, 3,0), 
 
89
  /*1982*/ SPY(12, 3,0), SPY(13, 3,0), SPY(14, 3,0), SPY(15, 4,0), 
 
90
  /*1986*/ SPY(16, 4,0), SPY(17, 4,0), SPY(18, 4,0), SPY(19, 5,0), 
 
91
  /*1990*/ SPY(20, 5,0), SPY(21, 5,0), SPY(22, 5,0), SPY(23, 6,0), 
 
92
  /*1994*/ SPY(24, 6,0), SPY(25, 6,0), SPY(26, 6,0), SPY(27, 7,0), 
 
93
  /*1998*/ SPY(28, 7,0), SPY(29, 7,0), SPY(30, 7,0), SPY(31, 8,0), 
 
94
  /*2002*/ SPY(32, 8,0), SPY(33, 8,0), SPY(34, 8,0), SPY(35, 9,0), 
 
95
  /*2006*/ SPY(36, 9,0), SPY(37, 9,0), SPY(38, 9,0), SPY(39,10,0), 
 
96
  /*2010*/ SPY(40,10,0), SPY(41,10,0), SPY(42,10,0), SPY(43,11,0), 
 
97
  /*2014*/ SPY(44,11,0), SPY(45,11,0), SPY(46,11,0), SPY(47,12,0), 
 
98
  /*2018*/ SPY(48,12,0), SPY(49,12,0), SPY(50,12,0), SPY(51,13,0), 
 
99
  /*2022*/ SPY(52,13,0), SPY(53,13,0), SPY(54,13,0), SPY(55,14,0), 
 
100
  /*2026*/ SPY(56,14,0), SPY(57,14,0), SPY(58,14,0), SPY(59,15,0), 
 
101
  /*2030*/ SPY(60,15,0), SPY(61,15,0), SPY(62,15,0), SPY(63,16,0), 
 
102
  /*2034*/ SPY(64,16,0), SPY(65,16,0), SPY(66,16,0), SPY(67,17,0), 
 
103
  /*2038*/ SPY(68,17,0)
 
104
};
 
105
 
 
106
#ifdef HAVE_TIMEZONE_VAR
 
107
extern long timezone;
 
108
#endif
 
109
 
 
110
time_t *
 
111
udf_stamp_to_time(time_t *dest, long int *dest_usec, 
 
112
                  const udf_timestamp_t src)
 
113
{
 
114
  int yday;
 
115
  uint8_t type = src.type_tz >> 12;
 
116
  int16_t offset;
 
117
  
 
118
  if (type == 1) {
 
119
    offset = src.type_tz << 4;
 
120
    /* sign extent offset */
 
121
    offset = (offset >> 4);
 
122
    if (offset == -2047) /* unspecified offset */
 
123
      offset = 0;
 
124
  }
 
125
  else
 
126
    offset = 0;
 
127
  
 
128
  if ((src.year < EPOCH_YEAR) ||
 
129
      (src.year >= EPOCH_YEAR+MAX_YEAR_SECONDS))
 
130
    {
 
131
      *dest = -1;
 
132
      *dest_usec = -1;
 
133
      return NULL;
 
134
    }
 
135
  *dest = year_seconds[src.year - EPOCH_YEAR];
 
136
  *dest -= offset * SECS_PER_MINUTE;
 
137
  
 
138
  yday = ((__mon_yday[__isleap (src.year)]
 
139
           [src.month-1]) + (src.day-1));
 
140
  *dest += src.second + 
 
141
    ( SECS_PER_MINUTE *
 
142
      ( ( (yday* HOURS_PER_DAY) + src.hour ) * 60 + src.minute ) );
 
143
 
 
144
  *dest_usec = src.microseconds
 
145
    + (src.centiseconds * 10000)
 
146
    + (src.hundreds_of_microseconds * 100);
 
147
  return dest;
 
148
}
 
149
 
 
150
 
 
151
/*!
 
152
  Convert a UDF timestamp to a time_t. If microseconds are desired,
 
153
  use dest_usec. The return value is the same as dest. */
 
154
udf_timestamp_t *
 
155
udf_timespec_to_stamp(const struct timespec ts, udf_timestamp_t *dest)
 
156
{
 
157
  long int days, rem, y;
 
158
  const unsigned short int *ip;
 
159
  int16_t offset = 0;
 
160
  int16_t tv_sec;
 
161
 
 
162
#ifdef HAVE_TIMEZONE_VAR  
 
163
  offset = -timezone;
 
164
#endif
 
165
  
 
166
  if (!dest)
 
167
    return dest;
 
168
  
 
169
  dest->type_tz = 0x1000 | (offset & 0x0FFF);
 
170
  
 
171
  tv_sec       = ts.tv_sec + (offset * SECS_PER_MINUTE);
 
172
  days         = tv_sec / SECS_PER_DAY;
 
173
  rem          = tv_sec % SECS_PER_DAY;
 
174
  dest->hour   = rem / SECS_PER_HOUR;
 
175
  rem         %= SECS_PER_HOUR;
 
176
  dest->minute = rem / SECS_PER_MINUTE;
 
177
  dest->second = rem % SECS_PER_MINUTE;
 
178
  y            = EPOCH_YEAR;
 
179
  
 
180
#define DIV(a,b) ((a) / (b) - ((a) % (b) < 0))
 
181
#define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400))
 
182
  
 
183
  while (days < 0 || days >= (__isleap(y) ? DAYS_PER_YEAR+1 : DAYS_PER_YEAR)) {
 
184
    long int yg = y + days / DAYS_PER_YEAR - (days % DAYS_PER_YEAR < 0);
 
185
    
 
186
    /* Adjust DAYS and Y to match the guessed year.  */
 
187
    days -= ((yg - y) * DAYS_PER_YEAR
 
188
             + LEAPS_THRU_END_OF (yg - 1)
 
189
             - LEAPS_THRU_END_OF (y - 1));
 
190
    y = yg;
 
191
  }
 
192
  dest->year = y;
 
193
  ip = __mon_yday[__isleap(y)];
 
194
  for (y = 11; days < (long int) ip[y]; --y)
 
195
    continue;
 
196
  days -= ip[y];
 
197
  dest->month = y + 1;
 
198
  dest->day   = days + 1;
 
199
  
 
200
  dest->centiseconds = ts.tv_nsec / 10000000;
 
201
  dest->hundreds_of_microseconds = ( (ts.tv_nsec / 1000)
 
202
                                     - (dest->centiseconds * 10000) ) / 100;
 
203
  dest->microseconds = ( (ts.tv_nsec / 1000) 
 
204
                         - (dest->centiseconds * 10000)
 
205
                         - (dest->hundreds_of_microseconds * 100) );
 
206
  return dest;
 
207
}
 
208
 
 
209
/*!
 
210
  Return the modification time of the file.
 
211
 */
 
212
time_t
 
213
udf_get_modification_time(const udf_dirent_t *p_udf_dirent)
 
214
{
 
215
  if (p_udf_dirent) {
 
216
    time_t ret_time;
 
217
    long int usec;
 
218
    udf_stamp_to_time(&ret_time, &usec, p_udf_dirent->fe.modification_time);
 
219
    return ret_time;
 
220
  }
 
221
  return 0;
 
222
}
 
223
 
 
224
/*!
 
225
  Return the access time of the file.
 
226
 */
 
227
time_t
 
228
udf_get_access_time(const udf_dirent_t *p_udf_dirent)
 
229
{
 
230
  if (p_udf_dirent) {
 
231
    time_t ret_time;
 
232
    long int usec;
 
233
    udf_stamp_to_time(&ret_time, &usec, p_udf_dirent->fe.access_time);
 
234
    return ret_time;
 
235
  }
 
236
  return 0;
 
237
}
 
238
 
 
239
/*!
 
240
  Return the attribute (most recent create or access) time of the file
 
241
 */
 
242
time_t
 
243
udf_get_attribute_time(const udf_dirent_t *p_udf_dirent)
 
244
{
 
245
  if (p_udf_dirent) {
 
246
    time_t ret_time;
 
247
    long int usec;
 
248
    udf_stamp_to_time(&ret_time, &usec, p_udf_dirent->fe.attribute_time);
 
249
    return ret_time;
 
250
  }
 
251
  return 0;
 
252
}
 
253