001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.hslf.util;
019:
020: import java.util.Calendar;
021: import java.util.Date;
022: import java.util.GregorianCalendar;
023:
024: import org.apache.poi.util.LittleEndian;
025:
026: /**
027: * A helper class for dealing with SystemTime Structs, as defined at
028: * http://msdn.microsoft.com/library/en-us/sysinfo/base/systemtime_str.asp .
029: *
030: * Discrepancies between Calendar and SYSTEMTIME:
031: * - that January = 1 in SYSTEMTIME, 0 in Calendar.
032: * - that the day of the week (0) starts on Sunday in SYSTEMTIME, and Monday in Calendar
033: * It is also the case that this does not store the timezone, and no... it is not
034: * stored as UTC either, but rather the local system time (yuck.)
035: *
036: * @author Daniel Noll
037: * @author Nick Burch
038: */
039: public class SystemTimeUtils {
040: /**
041: * Get the date found in the byte array, as a java Data object
042: */
043: public static Date getDate(byte[] data) {
044: return getDate(data, 0);
045: }
046:
047: /**
048: * Get the date found in the byte array, as a java Data object
049: */
050: public static Date getDate(byte[] data, int offset) {
051: Calendar cal = new GregorianCalendar();
052:
053: cal.set(Calendar.YEAR, LittleEndian.getShort(data, offset));
054: cal.set(Calendar.MONTH,
055: LittleEndian.getShort(data, offset + 2) - 1);
056: // Not actually needed - can be found from day of month
057: //cal.set(Calendar.DAY_OF_WEEK, LittleEndian.getShort(data,offset+4)+1);
058: cal.set(Calendar.DAY_OF_MONTH, LittleEndian.getShort(data,
059: offset + 6));
060: cal.set(Calendar.HOUR_OF_DAY, LittleEndian.getShort(data,
061: offset + 8));
062: cal.set(Calendar.MINUTE, LittleEndian.getShort(data,
063: offset + 10));
064: cal.set(Calendar.SECOND, LittleEndian.getShort(data,
065: offset + 12));
066: cal.set(Calendar.MILLISECOND, LittleEndian.getShort(data,
067: offset + 14));
068:
069: return cal.getTime();
070: }
071:
072: /**
073: * Convert the supplied java Date into a SystemTime struct, and write it
074: * into the supplied byte array.
075: */
076: public static void storeDate(Date date, byte[] dest) {
077: storeDate(date, dest, 0);
078: }
079:
080: /**
081: * Convert the supplied java Date into a SystemTime struct, and write it
082: * into the supplied byte array.
083: */
084: public static void storeDate(Date date, byte[] dest, int offset) {
085: Calendar cal = new GregorianCalendar();
086: cal.setTime(date);
087:
088: LittleEndian.putShort(dest, offset + 0, (short) cal
089: .get(Calendar.YEAR));
090: LittleEndian.putShort(dest, offset + 2, (short) (cal
091: .get(Calendar.MONTH) + 1));
092: LittleEndian.putShort(dest, offset + 4, (short) (cal
093: .get(Calendar.DAY_OF_WEEK) - 1));
094: LittleEndian.putShort(dest, offset + 6, (short) cal
095: .get(Calendar.DAY_OF_MONTH));
096: LittleEndian.putShort(dest, offset + 8, (short) cal
097: .get(Calendar.HOUR_OF_DAY));
098: LittleEndian.putShort(dest, offset + 10, (short) cal
099: .get(Calendar.MINUTE));
100: LittleEndian.putShort(dest, offset + 12, (short) cal
101: .get(Calendar.SECOND));
102: LittleEndian.putShort(dest, offset + 14, (short) cal
103: .get(Calendar.MILLISECOND));
104: }
105: }
|