PageRenderTime 27ms CodeModel.GetById 13ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/release-0.1-rc2/hive/external/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFDateDiff.java

#
Java | 83 lines | 36 code | 10 blank | 37 comment | 4 complexity | b4703f6d1484db5a276ba7edc69b730d MD5 | raw file
 1/**
 2 * Licensed to the Apache Software Foundation (ASF) under one
 3 * or more contributor license agreements.  See the NOTICE file
 4 * distributed with this work for additional information
 5 * regarding copyright ownership.  The ASF licenses this file
 6 * to you under the Apache License, Version 2.0 (the
 7 * "License"); you may not use this file except in compliance
 8 * with the License.  You may obtain a copy of the License at
 9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19package org.apache.hadoop.hive.ql.udf;
20
21import java.text.ParseException;
22import java.text.SimpleDateFormat;
23import java.util.TimeZone;
24
25import org.apache.hadoop.hive.ql.exec.Description;
26import org.apache.hadoop.hive.ql.exec.UDF;
27import org.apache.hadoop.io.IntWritable;
28import org.apache.hadoop.io.Text;
29
30/**
31 * UDFDateDiff.
32 *
33 */
34@Description(name = "datediff",
35    value = "_FUNC_(date1, date2) - Returns the number of days between date1 and date2",
36    extended = "date1 and date2 are strings in the format "
37    + "'yyyy-MM-dd HH:mm:ss' or 'yyyy-MM-dd'. The time parts are ignored."
38    + "If date1 is earlier than date2, the result is negative.\n"
39    + "Example:\n "
40    + "  > SELECT _FUNC_('2009-30-07', '2009-31-07') FROM src LIMIT 1;\n"
41    + "  1")
42public class UDFDateDiff extends UDF {
43  private final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
44
45  private IntWritable result = new IntWritable();
46
47  public UDFDateDiff() {
48    formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
49  }
50
51  /**
52   * Calculate the difference in the number of days. The time part of the string
53   * will be ignored. If dateString1 is earlier than dateString2, then the
54   * result can be negative.
55   * 
56   * @param dateString1
57   *          the date string in the format of "yyyy-MM-dd HH:mm:ss" or
58   *          "yyyy-MM-dd".
59   * @param dateString2
60   *          the date string in the format of "yyyy-MM-dd HH:mm:ss" or
61   *          "yyyy-MM-dd".
62   * @return the difference in days.
63   */
64  public IntWritable evaluate(Text dateString1, Text dateString2) {
65
66    if (dateString1 == null || dateString2 == null) {
67      return null;
68    }
69
70    try {
71      // NOTE: This implementation avoids the extra-second problem
72      // by comparing with UTC epoch and integer division.
73      long diffInMilliSeconds = (formatter.parse(dateString1.toString())
74          .getTime() - formatter.parse(dateString2.toString()).getTime());
75      // 86400 is the number of seconds in a day
76      result.set((int) (diffInMilliSeconds / (86400 * 1000)));
77      return result;
78    } catch (ParseException e) {
79      return null;
80    }
81  }
82
83}