@@ -135,65 +135,131 @@ int fat_chain_add(struct inode *inode, int new_dclus, int nr_cluster)
135135
136136extern struct timezone sys_tz ;
137137
138+ /*
139+ * The epoch of FAT timestamp is 1980.
140+ * : bits : value
141+ * date: 0 - 4: day (1 - 31)
142+ * date: 5 - 8: month (1 - 12)
143+ * date: 9 - 15: year (0 - 127) from 1980
144+ * time: 0 - 4: sec (0 - 29) 2sec counts
145+ * time: 5 - 10: min (0 - 59)
146+ * time: 11 - 15: hour (0 - 23)
147+ */
148+ #define SECS_PER_MIN 60
149+ #define SECS_PER_HOUR (60 * 60)
150+ #define SECS_PER_DAY (SECS_PER_HOUR * 24)
151+ #define UNIX_SECS_1980 315532800L
152+ #if BITS_PER_LONG == 64
153+ #define UNIX_SECS_2108 4354819200L
154+ #endif
155+ /* days between 1.1.70 and 1.1.80 (2 leap days) */
156+ #define DAYS_DELTA (365 * 10 + 2)
157+ /* 120 (2100 - 1980) isn't leap year */
158+ #define YEAR_2100 120
159+ #define IS_LEAP_YEAR (y ) (!((y) & 3) && (y) != YEAR_2100)
160+
138161/* Linear day numbers of the respective 1sts in non-leap years. */
139- static int day_n [] = {
140- /* Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec */
141- 0 , 31 , 59 , 90 , 120 , 151 , 181 , 212 , 243 , 273 , 304 , 334 , 0 , 0 , 0 , 0
162+ static time_t days_in_year [] = {
163+ /* Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec */
164+ 0 , 0 , 31 , 59 , 90 , 120 , 151 , 181 , 212 , 243 , 273 , 304 , 334 , 0 , 0 , 0 ,
142165};
143166
144- /* Convert a MS-DOS time/date pair to a UNIX date (seconds since 1 1 70). */
145- int date_dos2unix (unsigned short time , unsigned short date , int tz_utc )
167+ /* Convert a FAT time/date pair to a UNIX date (seconds since 1 1 70). */
168+ void fat_time_fat2unix (struct msdos_sb_info * sbi , struct timespec * ts ,
169+ __le16 __time , __le16 __date , u8 time_cs )
146170{
147- int month , year , secs ;
171+ u16 time = le16_to_cpu (__time ), date = le16_to_cpu (__date );
172+ time_t second , day , leap_day , month , year ;
148173
149- /*
150- * first subtract and mask after that... Otherwise, if
151- * date == 0, bad things happen
152- */
153- month = ((date >> 5 ) - 1 ) & 15 ;
154- year = date >> 9 ;
155- secs = (time & 31 )* 2 + 60 * ((time >> 5 ) & 63 )+ (time >> 11 )* 3600 + 86400 *
156- ((date & 31 )- 1 + day_n [month ]+ (year /4 )+ year * 365 - ((year & 3 ) == 0 &&
157- month < 2 ? 1 : 0 )+ 3653 );
158- /* days since 1.1.70 plus 80's leap day */
159- if (!tz_utc )
160- secs += sys_tz .tz_minuteswest * 60 ;
161- return secs ;
174+ year = date >> 9 ;
175+ month = max (1 , (date >> 5 ) & 0xf );
176+ day = max (1 , date & 0x1f ) - 1 ;
177+
178+ leap_day = (year + 3 ) / 4 ;
179+ if (year > YEAR_2100 ) /* 2100 isn't leap year */
180+ leap_day -- ;
181+ if (IS_LEAP_YEAR (year ) && month > 2 )
182+ leap_day ++ ;
183+
184+ second = (time & 0x1f ) << 1 ;
185+ second += ((time >> 5 ) & 0x3f ) * SECS_PER_MIN ;
186+ second += (time >> 11 ) * SECS_PER_HOUR ;
187+ second += (year * 365 + leap_day
188+ + days_in_year [month ] + day
189+ + DAYS_DELTA ) * SECS_PER_DAY ;
190+
191+ if (!sbi -> options .tz_utc )
192+ second += sys_tz .tz_minuteswest * SECS_PER_MIN ;
193+
194+ if (time_cs ) {
195+ ts -> tv_sec = second + (time_cs / 100 );
196+ ts -> tv_nsec = (time_cs % 100 ) * 10000000 ;
197+ } else {
198+ ts -> tv_sec = second ;
199+ ts -> tv_nsec = 0 ;
200+ }
162201}
163202
164- /* Convert linear UNIX date to a MS-DOS time/date pair. */
165- void fat_date_unix2dos (int unix_date , __le16 * time , __le16 * date , int tz_utc )
203+ /* Convert linear UNIX date to a FAT time/date pair. */
204+ void fat_time_unix2fat (struct msdos_sb_info * sbi , struct timespec * ts ,
205+ __le16 * time , __le16 * date , u8 * time_cs )
166206{
167- int day , year , nl_day , month ;
207+ time_t second = ts -> tv_sec ;
208+ time_t day , leap_day , month , year ;
168209
169- if (!tz_utc )
170- unix_date -= sys_tz .tz_minuteswest * 60 ;
210+ if (!sbi -> options . tz_utc )
211+ second -= sys_tz .tz_minuteswest * SECS_PER_MIN ;
171212
172213 /* Jan 1 GMT 00:00:00 1980. But what about another time zone? */
173- if (unix_date < 315532800 )
174- unix_date = 315532800 ;
175-
176- * time = cpu_to_le16 ((unix_date % 60 )/2 + (((unix_date /60 ) % 60 ) << 5 )+
177- (((unix_date /3600 ) % 24 ) << 11 ));
178- day = unix_date /86400 - 3652 ;
179- year = day /365 ;
180- if ((year + 3 )/4 + 365 * year > day )
214+ if (second < UNIX_SECS_1980 ) {
215+ * time = 0 ;
216+ * date = cpu_to_le16 ((0 << 9 ) | (1 << 5 ) | 1 );
217+ if (time_cs )
218+ * time_cs = 0 ;
219+ return ;
220+ }
221+ #if BITS_PER_LONG == 64
222+ if (second >= UNIX_SECS_2108 ) {
223+ * time = cpu_to_le16 ((23 << 11 ) | (59 << 5 ) | 29 );
224+ * date = cpu_to_le16 ((127 << 9 ) | (12 << 5 ) | 31 );
225+ if (time_cs )
226+ * time_cs = 199 ;
227+ return ;
228+ }
229+ #endif
230+
231+ day = second / SECS_PER_DAY - DAYS_DELTA ;
232+ year = day / 365 ;
233+ leap_day = (year + 3 ) / 4 ;
234+ if (year > YEAR_2100 ) /* 2100 isn't leap year */
235+ leap_day -- ;
236+ if (year * 365 + leap_day > day )
181237 year -- ;
182- day -= (year + 3 )/4 + 365 * year ;
183- if (day == 59 && !(year & 3 )) {
184- nl_day = day ;
238+ leap_day = (year + 3 ) / 4 ;
239+ if (year > YEAR_2100 ) /* 2100 isn't leap year */
240+ leap_day -- ;
241+ day -= year * 365 + leap_day ;
242+
243+ if (IS_LEAP_YEAR (year ) && day == days_in_year [3 ]) {
185244 month = 2 ;
186245 } else {
187- nl_day = (year & 3 ) || day <= 59 ? day : day - 1 ;
188- for (month = 0 ; month < 12 ; month ++ ) {
189- if (day_n [month ] > nl_day )
246+ if (IS_LEAP_YEAR (year ) && day > days_in_year [3 ])
247+ day -- ;
248+ for (month = 1 ; month < 12 ; month ++ ) {
249+ if (days_in_year [month + 1 ] > day )
190250 break ;
191251 }
192252 }
193- * date = cpu_to_le16 (nl_day - day_n [month - 1 ]+ 1 + (month << 5 )+ (year << 9 ));
194- }
253+ day -= days_in_year [month ];
195254
196- EXPORT_SYMBOL_GPL (fat_date_unix2dos );
255+ * time = cpu_to_le16 (((second / SECS_PER_HOUR ) % 24 ) << 11
256+ | ((second / SECS_PER_MIN ) % 60 ) << 5
257+ | (second % SECS_PER_MIN ) >> 1 );
258+ * date = cpu_to_le16 ((year << 9 ) | (month << 5 ) | (day + 1 ));
259+ if (time_cs )
260+ * time_cs = (ts -> tv_sec & 1 ) * 100 + ts -> tv_nsec / 10000000 ;
261+ }
262+ EXPORT_SYMBOL_GPL (fat_time_unix2fat );
197263
198264int fat_sync_bhs (struct buffer_head * * bhs , int nr_bhs )
199265{
0 commit comments