01: /* Copyright (C) 2004 - 2007 db4objects Inc. http://www.db4o.com
02:
03: This file is part of the db4o open source object database.
04:
05: db4o is free software; you can redistribute it and/or modify it under
06: the terms of version 2 of the GNU General Public License as published
07: by the Free Software Foundation and as clarified by db4objects' GPL
08: interpretation policy, available at
09: http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
10: Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
11: Suite 350, San Mateo, CA 94403, USA.
12:
13: db4o is distributed in the hope that it will be useful, but WITHOUT ANY
14: WARRANTY; without even the implied warranty of MERCHANTABILITY or
15: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16: for more details.
17:
18: You should have received a copy of the GNU General Public License along
19: with this program; if not, write to the Free Software Foundation, Inc.,
20: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21: package com.db4o.internal.handlers.net;
22:
23: import java.util.*;
24:
25: import com.db4o.internal.*;
26:
27: /**
28: * @exclude
29: * @sharpen.ignore
30: */
31: // TODO: Between .NET and Java there seems to be a difference of two days between era offsets?!?
32: public class NetDateTime extends NetSimpleTypeHandler {
33: private final static String ZEROES = "0000";
34:
35: private final static String[] MONTHS = { "Jan", "Feb", "Mar",
36: "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov",
37: "Dec" };
38:
39: // ms between 01.01.0001,00:00:00.000 and 01.01.1970,00:00:00.000
40: //private static final long ERA_DIFFERENCE_IN_MS = 62135604000000L; // Carl's diff
41: private static final long ERA_DIFFERENCE_IN_MS = 62135596800000L; // .net diff
42: //private static final long ERA_DIFFERENCE_IN_MS = 62135769600000L; // java diff
43:
44: // ratio from .net ticks (100ns) to java ms
45: private static final long TICKS_TO_MS_RATIO = 10000;
46:
47: public NetDateTime(ObjectContainerBase stream) {
48: super (stream, 25, 8);
49: }
50:
51: public String toString(byte[] bytes) {
52: long ticks = 0;
53: for (int i = 0; i < 8; i++) {
54: ticks = (ticks << 8) + (bytes[i] & 255);
55: }
56: long ms = ticks / TICKS_TO_MS_RATIO - ERA_DIFFERENCE_IN_MS;
57: Date date = new Date(ms);
58: Calendar cal = Calendar
59: .getInstance(TimeZone.getTimeZone("UTC"));
60: cal.setTime(date);
61: StringBuffer result = new StringBuffer()
62: .append(prependZeroes(cal.get(Calendar.YEAR), 4))
63: .append('-')
64: .append(MONTHS[cal.get(Calendar.MONTH)])
65: .append('-')
66: .append(
67: prependZeroes(cal.get(Calendar.DAY_OF_MONTH), 2))
68: .append(", ")
69: .append(prependZeroes(cal.get(Calendar.HOUR_OF_DAY), 2))
70: .append(':')
71: .append(prependZeroes(cal.get(Calendar.MINUTE), 2))
72: .append(':')
73: .append(prependZeroes(cal.get(Calendar.SECOND), 2))
74: .append('.')
75: .append(prependZeroes(cal.get(Calendar.MILLISECOND), 3))
76: .append(" UTC");
77: return result.toString();
78: }
79:
80: private String prependZeroes(int val, int size) {
81: String str = String.valueOf(val);
82: int missing = size - str.length();
83: if (missing > 0) {
84: str = ZEROES.substring(0, missing) + str;
85: }
86: return str;
87: }
88: }
|