Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
timestamp-value.cc
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
16 
17 #include "common/names.h"
18 
19 using boost::date_time::not_a_date_time;
20 using boost::posix_time::nanoseconds;
21 using boost::posix_time::ptime;
22 using boost::posix_time::ptime_from_tm;
23 using boost::posix_time::to_tm;
24 
25 DEFINE_bool(use_local_tz_for_unix_timestamp_conversions, false,
26  "When true, TIMESTAMPs are interpreted in the local time zone when converting to "
27  "and from Unix times. When false, TIMESTAMPs are interpreted in the UTC time zone. "
28  "Set to true for Hive compatibility.");
29 
30 namespace impala {
31 
32 const char* TimestampValue::LLVM_CLASS_NAME = "class.impala::TimestampValue";
33 const double TimestampValue::ONE_BILLIONTH = 0.000000001;
34 
35 TimestampValue::TimestampValue(const char* str, int len) {
36  TimestampParser::Parse(str, len, &date_, &time_);
37 }
38 
39 TimestampValue::TimestampValue(const char* str, int len,
40  const DateTimeFormatContext& dt_ctx) {
41  TimestampParser::Parse(str, len, dt_ctx, &date_, &time_);
42 }
43 
44 int TimestampValue::Format(const DateTimeFormatContext& dt_ctx, int len, char* buff) {
45  return TimestampParser::Format(dt_ctx, date_, time_, len, buff);
46 }
47 
49  DCHECK(HasDateAndTime());
50  try {
51  tm temp_tm = to_tm(ptime(date_, time_)); // will throw if date/time is invalid
52  time_t utc = timegm(&temp_tm);
53  if (UNLIKELY(NULL == localtime_r(&utc, &temp_tm))) {
54  *this = ptime(not_a_date_time);
55  return;
56  }
57  // Unlikely but a time zone conversion may push the value over the min/max
58  // boundary resulting in an exception.
59  ptime local = ptime_from_tm(temp_tm);
60  // Neither time_t nor struct tm allow fractional seconds so they have to be handled
61  // separately.
62  local += nanoseconds(time_.fractional_seconds());
63  *this = local;
64  } catch (std::exception& from_boost) {
65  *this = ptime(not_a_date_time);
66  }
67 }
68 
69 ostream& operator<<(ostream& os, const TimestampValue& timestamp_value) {
70  return os << timestamp_value.DebugString();
71 }
72 
73 }
int Format(const DateTimeFormatContext &dt_ctx, int len, char *buff)
DEFINE_bool(use_local_tz_for_unix_timestamp_conversions, false,"When true, TIMESTAMPs are interpreted in the local time zone when converting to ""and from Unix times. When false, TIMESTAMPs are interpreted in the UTC time zone. ""Set to true for Hive compatibility.")
static bool Parse(const char *str, int len, boost::gregorian::date *d, boost::posix_time::time_duration *t)
boost::gregorian::date date_
4 -bytes - stores the date as a day
static int Format(const DateTimeFormatContext &dt_ctx, const boost::gregorian::date &d, const boost::posix_time::time_duration &t, int len, char *buff)
static const char * LLVM_CLASS_NAME
static const double ONE_BILLIONTH
#define UNLIKELY(expr)
Definition: compiler-util.h:33
ostream & operator<<(ostream &os, const map< TNetworkAddress, llama::TAllocatedResource > &resources)
bool HasDateAndTime() const
boost::posix_time::time_duration time_
8 bytes - stores the nanoseconds within the current day
std::string DebugString() const