1 /*
2  * Copyright (C) 2003-2009 JNode.org
3  *               2009,2010 Matthias Treydte <mt@waldheinz.de>
4  *
5  * This library is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU Lesser General Public License as published
7  * by the Free Software Foundation; either version 2.1 of the License, or
8  * (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
13  * License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; If not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 package de.waldheinz.fs.fat;
21 
22 import java.util.Calendar;
23 
24 /**
25  * This class contains some methods for date and time conversions between Java
26  * and the format known from DOS filesystems (e.g. fat)
27  *
28  * @author Ewout Prangsma &lt; epr at jnode.org&gt;
29  */
30 final class DosUtils {
31 
DosUtils()32     private DosUtils() { /* no instances */ }
33 
34     /**
35      * Decode a 16-bit encoded DOS date/time into a java date/time.
36      *
37      * @param dosDate
38      * @param dosTime
39      * @return long
40      */
decodeDateTime(int dosDate, int dosTime)41     public static long decodeDateTime(int dosDate, int dosTime) {
42         final Calendar cal = Calendar.getInstance();
43 
44         cal.set(Calendar.MILLISECOND, 0);
45         cal.set(Calendar.SECOND, (dosTime & 0x1f) * 2);
46         cal.set(Calendar.MINUTE, (dosTime >> 5) & 0x3f);
47         cal.set(Calendar.HOUR_OF_DAY, dosTime >> 11);
48 
49         cal.set(Calendar.DATE, dosDate & 0x1f);
50         cal.set(Calendar.MONTH, ((dosDate >> 5) & 0x0f) - 1);
51         cal.set(Calendar.YEAR, 1980 + (dosDate >> 9));
52 
53         return cal.getTimeInMillis();
54     }
55 
56     /**
57      * Encode a java date/time into a 16-bit encoded DOS time
58      *
59      * @param javaDateTime
60      * @return long
61      */
encodeTime(long javaDateTime)62     public static int encodeTime(long javaDateTime) {
63         Calendar cal = Calendar.getInstance();
64         cal.setTimeInMillis(javaDateTime);
65         return 2048 * cal.get(Calendar.HOUR_OF_DAY) + 32 * cal.get(Calendar.MINUTE) +
66                 cal.get(Calendar.SECOND) / 2;
67     }
68 
69     /**
70      * Encode a java date/time into a 16-bit encoded DOS date
71      *
72      * @param javaDateTime
73      * @return long
74      */
encodeDate(long javaDateTime)75     public static int encodeDate(long javaDateTime) {
76         Calendar cal = Calendar.getInstance();
77         cal.setTimeInMillis(javaDateTime);
78         return 512 * (cal.get(Calendar.YEAR) - 1980) + 32 * (cal.get(Calendar.MONTH) + 1) +
79                 cal.get(Calendar.DATE);
80     }
81 }
82