KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > smb > SMBDate


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.filesys.smb;
18
19 import java.util.Calendar JavaDoc;
20 import java.util.Date JavaDoc;
21
22 /**
23  * SMB date/time class.
24  */

25 public final class SMBDate extends Date JavaDoc
26 {
27     private static final long serialVersionUID = 3258407335553806902L;
28
29     // Constants
30
//
31
// Bit masks for extracting the date/time fields from an SMB encoded date/time.
32
//
33

34     private static final int Days = 0x001F;
35     private static final int Month = 0x01E0;
36     private static final int Year = 0xFE00;
37
38     private static final int TwoSeconds = 0x001F;
39     private static final int Minutes = 0x07E0;
40     private static final int Hours = 0xF800;
41
42     /**
43      * Construct the SMBDate using a seconds since 1-Jan-1970 00:00:00 value.
44      *
45      * @param secs Seconds since base date/time 1970 value
46      */

47
48     public SMBDate(int secs)
49     {
50         super((long) (secs & 0x7FFFFFFF));
51     }
52
53     /**
54      * Construct the SMBDate using the SMB encoded date/time values.
55      *
56      * @param dat SMB encoded date value
57      * @param tim SMB encoded time value
58      */

59
60     public SMBDate(int dat, int tim)
61     {
62
63         // Extract the date from the SMB encoded value
64

65         int days = dat & Days;
66         int months = (dat & Month) >> 5;
67         int year = (dat & Year) >> 9;
68
69         // Extract the time from the SMB encoded value
70

71         int secs = (tim & TwoSeconds) * 2;
72         int mins = (tim & Minutes) >> 5;
73         int hours = (tim & Hours) >> 11;
74
75         // Use a calendar object to create the date/time value
76

77         Calendar JavaDoc cal = Calendar.getInstance();
78         cal.clear();
79         cal.set(year + 1980, months - 1, days, hours, mins, secs);
80
81         // Initialize this dates raw value
82

83         this.setTime(cal.getTime().getTime());
84     }
85
86     /**
87      * Create a new SMBDate using the long time value.
88      *
89      * @param dattim long
90      */

91     public SMBDate(long dattim)
92     {
93         super(dattim);
94     }
95
96     /**
97      * Return this date as an SMB encoded date.
98      *
99      * @return SMB encoded date value.
100      */

101
102     public final int asSMBDate()
103     {
104
105         // Use a calendar object to get the day, month and year values
106

107         Calendar JavaDoc cal = Calendar.getInstance();
108         cal.setTime(this);
109
110         // Build the SMB encoded date value
111

112         int smbDate = cal.get(Calendar.DAY_OF_MONTH);
113         smbDate += (cal.get(Calendar.MONTH) + 1) << 5;
114         smbDate += (cal.get(Calendar.YEAR) - 1980) << 9;
115
116         // Return the SMB encoded date value
117

118         return smbDate;
119     }
120
121     /**
122      * Return this time as an SMB encoded time.
123      *
124      * @return SMB encoded time value.
125      */

126
127     public final int asSMBTime()
128     {
129
130         // Use a calendar object to get the hour, minutes and seconds values
131

132         Calendar JavaDoc cal = Calendar.getInstance();
133         cal.setTime(this);
134
135         // Build the SMB encoded time value
136

137         int smbTime = cal.get(Calendar.SECOND) / 2;
138         smbTime += cal.get(Calendar.MINUTE) << 5;
139         smbTime += cal.get(Calendar.HOUR_OF_DAY) << 11;
140
141         // Return the SMB encoded time value
142

143         return smbTime;
144     }
145 }
Popular Tags