62eedde55b0f357232c2b773e62f69fa592eb409
[vxquery.git] / vxquery-core / src / main / java / org / apache / vxquery / datamodel / accessors / atomic / XSTimePointable.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.ITime;
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.IntegerPointable;
28
29 /**
30 * The time 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 * DayTime (milliseconds) values.
33 *
34 * @author prestoncarman
35 */
36 public class XSTimePointable extends AbstractPointable implements ITime, ITimezone {
37 public final static int HOUR_OFFSET = 0;
38 public final static int MINUTE_OFFSET = 1;
39 public final static int MILLISECOND_OFFSET = 2;
40 public final static int TIMEZONE_HOUR_OFFSET = 6;
41 public final static int TIMEZONE_MINUTE_OFFSET = 7;
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 8;
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 XSTimePointable();
63 }
64
65 @Override
66 public ITypeTraits getTypeTraits() {
67 return TYPE_TRAITS;
68 }
69 };
70
71 public void setTime(long hour, long minute, long second, long timezoneHour, long timezoneMinute) {
72 setTime(bytes, start, hour, minute, second, timezoneHour, timezoneMinute);
73 }
74
75 public static void setTime(byte[] bytes, int start, long hour, long minute, long second, long timezoneHour,
76 long timezoneMinute) {
77 BytePointable.setByte(bytes, start + HOUR_OFFSET, (byte) hour);
78 BytePointable.setByte(bytes, start + MINUTE_OFFSET, (byte) minute);
79 IntegerPointable.setInteger(bytes, start + MILLISECOND_OFFSET, (byte) second);
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 getHour() {
86 return getHour(bytes, start);
87 }
88
89 public static long getHour(byte[] bytes, int start) {
90 return (long) BytePointable.getByte(bytes, start + HOUR_OFFSET);
91 }
92
93 @Override
94 public long getMinute() {
95 return getMinute(bytes, start);
96 }
97
98 public static long getMinute(byte[] bytes, int start) {
99 return (long) BytePointable.getByte(bytes, start + MINUTE_OFFSET);
100 }
101
102 @Override
103 public long getMilliSecond() {
104 return getMilliSecond(bytes, start);
105 }
106
107 public static long getMilliSecond(byte[] bytes, int start) {
108 return (long) IntegerPointable.getInteger(bytes, start + MILLISECOND_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 getDayTime() {
140 return getDayTime(bytes, start);
141 }
142
143 public static long getDayTime(byte[] bytes, int start) {
144 return (((getHour(bytes, start)) * 60 + getMinute(bytes, start)) * 60 * 1000 + getMilliSecond(bytes, start));
145 }
146
147 @Override
148 public long getYearMonth() {
149 return getYearMonth(bytes, start);
150 }
151
152 public static long getYearMonth(byte[] bytes, int start) {
153 return 0;
154 }
155
156 }

Copyright 2016, The Apache Software Foundation.