f635b1d49864abed43a774fbad80aa13f36502ae
[vxquery.git] / vxquery-core / src / main / java / org / apache / vxquery / datamodel / accessors / atomic / XSDatePointable.java
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.vxquery.datamodel.accessors.atomic;
18
19 import org.apache.hyracks.api.dataflow.value.ITypeTraits;
20 import org.apache.hyracks.data.std.api.AbstractPointable;
21 import org.apache.hyracks.data.std.api.IPointable;
22 import org.apache.hyracks.data.std.api.IPointableFactory;
23 import org.apache.hyracks.data.std.primitive.BytePointable;
24 import org.apache.hyracks.data.std.primitive.ShortPointable;
25 import org.apache.vxquery.datamodel.api.IDate;
26 import org.apache.vxquery.datamodel.api.ITimezone;
27
28 /**
29 * The date is split up into five sections. Due to leap year, we have decided to keep the
30 * storage simple by saving each date section separately. For calculations you can access
31 * YearMonth (months) and DayTime (milliseconds) values.
32 * The date pointable is also used for GDay, GMonth, GYear, GDayMonth and GMonthYear.
33 */
34 public class XSDatePointable extends AbstractPointable implements IDate, ITimezone {
35 public final static int YEAR_OFFSET = 0;
36 public final static int MONTH_OFFSET = 2;
37 public final static int DAY_OFFSET = 3;
38 public final static int TIMEZONE_HOUR_OFFSET = 4;
39 public final static int TIMEZONE_MINUTE_OFFSET = 5;
40 // CHRONON_OF_DAY is used to convert days into milliseconds.
41 private final static long CHRONON_OF_DAY = 24 * 60 * 60 * 1000;
42
43 public static final ITypeTraits TYPE_TRAITS = new ITypeTraits() {
44 private static final long serialVersionUID = 1L;
45
46 @Override
47 public boolean isFixedLength() {
48 return true;
49 }
50
51 @Override
52 public int getFixedLength() {
53 return 6;
54 }
55 };
56
57 public static final IPointableFactory FACTORY = new IPointableFactory() {
58 private static final long serialVersionUID = 1L;
59
60 @Override
61 public IPointable createPointable() {
62 return new XSDatePointable();
63 }
64
65 @Override
66 public ITypeTraits getTypeTraits() {
67 return TYPE_TRAITS;
68 }
69 };
70
71 public void setDate(long year, long month, long day, long timezoneHour, long timezoneMinute) {
72 setDate(bytes, start, year, month, day, timezoneHour, timezoneMinute);
73 }
74
75 public static void setDate(byte[] bytes, int start, long year, long month, long day, long timezoneHour,
76 long timezoneMinute) {
77 ShortPointable.setShort(bytes, start + YEAR_OFFSET, (short) year);
78 BytePointable.setByte(bytes, start + MONTH_OFFSET, (byte) month);
79 BytePointable.setByte(bytes, start + DAY_OFFSET, (byte) day);
80 BytePointable.setByte(bytes, start + TIMEZONE_HOUR_OFFSET, (byte) timezoneHour);
81 BytePointable.setByte(bytes, start + TIMEZONE_MINUTE_OFFSET, (byte) timezoneMinute);
82 }
83
84 @Override
85 public long getYear() {
86 return getYear(bytes, start);
87 }
88
89 public static long getYear(byte[] bytes, int start) {
90 return (long) ShortPointable.getShort(bytes, start + YEAR_OFFSET);
91 }
92
93 @Override
94 public long getMonth() {
95 return getMonth(bytes, start);
96 }
97
98 public static long getMonth(byte[] bytes, int start) {
99 return (long) BytePointable.getByte(bytes, start + MONTH_OFFSET);
100 }
101
102 @Override
103 public long getDay() {
104 return getDay(bytes, start);
105 }
106
107 public static long getDay(byte[] bytes, int start) {
108 return (long) BytePointable.getByte(bytes, start + DAY_OFFSET);
109 }
110
111 @Override
112 public long getTimezoneHour() {
113 return getTimezoneHour(bytes, start);
114 }
115
116 public static long getTimezoneHour(byte[] bytes, int start) {
117 return (long) BytePointable.getByte(bytes, start + TIMEZONE_HOUR_OFFSET);
118 }
119
120 @Override
121 public long getTimezoneMinute() {
122 return getTimezoneMinute(bytes, start);
123 }
124
125 public static long getTimezoneMinute(byte[] bytes, int start) {
126 return (long) BytePointable.getByte(bytes, start + TIMEZONE_MINUTE_OFFSET);
127 }
128
129 @Override
130 public long getTimezone() {
131 return getTimezone(bytes, start);
132 }
133
134 public static long getTimezone(byte[] bytes, int start) {
135 return (getTimezoneHour(bytes, start) * 60 + getTimezoneMinute(bytes, start));
136 }
137
138 @Override
139 public long getYearMonth() {
140 return getYearMonth(bytes, start);
141 }
142
143 public static long getYearMonth(byte[] bytes, int start) {
144 return (getYear(bytes, start) * 12 + getMonth(bytes, start));
145 }
146
147 @Override
148 public long getDayTime() {
149 return getDayTime(bytes, start);
150 }
151
152 public static long getDayTime(byte[] bytes, int start) {
153 return getDay(bytes, start) * CHRONON_OF_DAY;
154 }
155
156 }