d7289276c3988a3afdafcb7b23c5b50c6845ddba
[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.vxquery.datamodel.api.IDate;
20 import org.apache.vxquery.datamodel.api.ITimezone;
21
22 import org.apache.hyracks.api.dataflow.value.ITypeTraits;
23 import org.apache.hyracks.data.std.api.AbstractPointable;
24 import org.apache.hyracks.data.std.api.IPointable;
25 import org.apache.hyracks.data.std.api.IPointableFactory;
26 import org.apache.hyracks.data.std.primitive.BytePointable;
27 import org.apache.hyracks.data.std.primitive.ShortPointable;
28
29 /**
30 * The date is split up into five sections. Due to leap year, we have decided to keep the
31 * storage simple by saving each date section separately. For calculations you can access
32 * YearMonth (months) and DayTime (milliseconds) values.
33 * The date pointable is also used for GDay, GMonth, GYear, GDayMonth and GMonthYear.
34 *
35 * @author prestoncarman
36 */
37 public class XSDatePointable extends AbstractPointable implements IDate, ITimezone {
38 public final static int YEAR_OFFSET = 0;
39 public final static int MONTH_OFFSET = 2;
40 public final static int DAY_OFFSET = 3;
41 public final static int TIMEZONE_HOUR_OFFSET = 4;
42 public final static int TIMEZONE_MINUTE_OFFSET = 5;
43 // CHRONON_OF_DAY is used to convert days into milliseconds.
44 private final static long CHRONON_OF_DAY = 24 * 60 * 60 * 1000;
45
46 public static final ITypeTraits TYPE_TRAITS = new ITypeTraits() {
47 private static final long serialVersionUID = 1L;
48
49 @Override
50 public boolean isFixedLength() {
51 return true;
52 }
53
54 @Override
55 public int getFixedLength() {
56 return 6;
57 }
58 };
59
60 public static final IPointableFactory FACTORY = new IPointableFactory() {
61 private static final long serialVersionUID = 1L;
62
63 @Override
64 public IPointable createPointable() {
65 return new XSDatePointable();
66 }
67
68 @Override
69 public ITypeTraits getTypeTraits() {
70 return TYPE_TRAITS;
71 }
72 };
73
74 public void setDate(long year, long month, long day, long timezoneHour, long timezoneMinute) {
75 setDate(bytes, start, year, month, day, timezoneHour, timezoneMinute);
76 }
77
78 public static void setDate(byte[] bytes, int start, long year, long month, long day, long timezoneHour,
79 long timezoneMinute) {
80 ShortPointable.setShort(bytes, start + YEAR_OFFSET, (short) year);
81 BytePointable.setByte(bytes, start + MONTH_OFFSET, (byte) month);
82 BytePointable.setByte(bytes, start + DAY_OFFSET, (byte) day);
83 BytePointable.setByte(bytes, start + TIMEZONE_HOUR_OFFSET, (byte) timezoneHour);
84 BytePointable.setByte(bytes, start + TIMEZONE_MINUTE_OFFSET, (byte) timezoneMinute);
85 }
86
87 @Override
88 public long getYear() {
89 return getYear(bytes, start);
90 }
91
92 public static long getYear(byte[] bytes, int start) {
93 return (long) ShortPointable.getShort(bytes, start + YEAR_OFFSET);
94 }
95
96 @Override
97 public long getMonth() {
98 return getMonth(bytes, start);
99 }
100
101 public static long getMonth(byte[] bytes, int start) {
102 return (long) BytePointable.getByte(bytes, start + MONTH_OFFSET);
103 }
104
105 @Override
106 public long getDay() {
107 return getDay(bytes, start);
108 }
109
110 public static long getDay(byte[] bytes, int start) {
111 return (long) BytePointable.getByte(bytes, start + DAY_OFFSET);
112 }
113
114 @Override
115 public long getTimezoneHour() {
116 return getTimezoneHour(bytes, start);
117 }
118
119 public static long getTimezoneHour(byte[] bytes, int start) {
120 return (long) BytePointable.getByte(bytes, start + TIMEZONE_HOUR_OFFSET);
121 }
122
123 @Override
124 public long getTimezoneMinute() {
125 return getTimezoneMinute(bytes, start);
126 }
127
128 public static long getTimezoneMinute(byte[] bytes, int start) {
129 return (long) BytePointable.getByte(bytes, start + TIMEZONE_MINUTE_OFFSET);
130 }
131
132 @Override
133 public long getTimezone() {
134 return getTimezone(bytes, start);
135 }
136
137 public static long getTimezone(byte[] bytes, int start) {
138 return (getTimezoneHour(bytes, start) * 60 + getTimezoneMinute(bytes, start));
139 }
140
141 @Override
142 public long getYearMonth() {
143 return getYearMonth(bytes, start);
144 }
145
146 public static long getYearMonth(byte[] bytes, int start) {
147 return (getYear(bytes, start) * 12 + getMonth(bytes, start));
148 }
149
150 @Override
151 public long getDayTime() {
152 return getDayTime(bytes, start);
153 }
154
155 public static long getDayTime(byte[] bytes, int start) {
156 return getDay(bytes, start) * CHRONON_OF_DAY;
157 }
158
159 }

Copyright 2016, The Apache Software Foundation.