PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms 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
Possible License(s): Apache-2.0, BSD-3-Clause, JSON, CPL-1.0
  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. package org.apache.hadoop.hive.ql.udf;
  19. import java.text.ParseException;
  20. import java.text.SimpleDateFormat;
  21. import java.util.TimeZone;
  22. import org.apache.hadoop.hive.ql.exec.Description;
  23. import org.apache.hadoop.hive.ql.exec.UDF;
  24. import org.apache.hadoop.io.IntWritable;
  25. import org.apache.hadoop.io.Text;
  26. /**
  27. * UDFDateDiff.
  28. *
  29. */
  30. @Description(name = "datediff",
  31. value = "_FUNC_(date1, date2) - Returns the number of days between date1 and date2",
  32. extended = "date1 and date2 are strings in the format "
  33. + "'yyyy-MM-dd HH:mm:ss' or 'yyyy-MM-dd'. The time parts are ignored."
  34. + "If date1 is earlier than date2, the result is negative.\n"
  35. + "Example:\n "
  36. + " > SELECT _FUNC_('2009-30-07', '2009-31-07') FROM src LIMIT 1;\n"
  37. + " 1")
  38. public class UDFDateDiff extends UDF {
  39. private final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
  40. private IntWritable result = new IntWritable();
  41. public UDFDateDiff() {
  42. formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
  43. }
  44. /**
  45. * Calculate the difference in the number of days. The time part of the string
  46. * will be ignored. If dateString1 is earlier than dateString2, then the
  47. * result can be negative.
  48. *
  49. * @param dateString1
  50. * the date string in the format of "yyyy-MM-dd HH:mm:ss" or
  51. * "yyyy-MM-dd".
  52. * @param dateString2
  53. * the date string in the format of "yyyy-MM-dd HH:mm:ss" or
  54. * "yyyy-MM-dd".
  55. * @return the difference in days.
  56. */
  57. public IntWritable evaluate(Text dateString1, Text dateString2) {
  58. if (dateString1 == null || dateString2 == null) {
  59. return null;
  60. }
  61. try {
  62. // NOTE: This implementation avoids the extra-second problem
  63. // by comparing with UTC epoch and integer division.
  64. long diffInMilliSeconds = (formatter.parse(dateString1.toString())
  65. .getTime() - formatter.parse(dateString2.toString()).getTime());
  66. // 86400 is the number of seconds in a day
  67. result.set((int) (diffInMilliSeconds / (86400 * 1000)));
  68. return result;
  69. } catch (ParseException e) {
  70. return null;
  71. }
  72. }
  73. }