This is an automated email from the ASF dual-hosted git repository.

gangwu pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/orc.git


The following commit(s) were added to refs/heads/main by this push:
     new 873d00c1f ORC-1481: [C++] Better error message when TZDB is unavailable
873d00c1f is described below

commit 873d00c1f42753e9522cc488db59b1c979299eea
Author: Gang Wu <[email protected]>
AuthorDate: Wed Aug 23 17:58:22 2023 +0800

    ORC-1481: [C++] Better error message when TZDB is unavailable
    
    ### What changes were proposed in this pull request?
    Check the availability of TZDB and throw helpful error message before it 
crashes.
    
    ### Why are the changes needed?
    When local IANA TZDB is unavailable or TZDIR env is not properly set, 
getTimezoneByName() simply fails without much helpful guidance on what to do.
    
    ### How was this patch tested?
    Added a test case TestTimezone.testMissingTZDB to make sure error message 
is as expected.
    
    Closes #1587 from wgtmac/ORC-1481.
    
    Authored-by: Gang Wu <[email protected]>
    Signed-off-by: Gang Wu <[email protected]>
---
 c++/src/Timezone.cc      | 12 +++++++++---
 c++/test/TestTimezone.cc | 18 ++++++++++++++++++
 2 files changed, 27 insertions(+), 3 deletions(-)

diff --git a/c++/src/Timezone.cc b/c++/src/Timezone.cc
index 6628eba83..9f1352e30 100644
--- a/c++/src/Timezone.cc
+++ b/c++/src/Timezone.cc
@@ -24,7 +24,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <time.h>
-#include <iostream>
+#include <filesystem>
 #include <map>
 #include <sstream>
 
@@ -675,6 +675,12 @@ namespace orc {
     if (itr != timezoneCache.end()) {
       return *(itr->second).get();
     }
+    if (!std::filesystem::exists(std::filesystem::path(filename))) {
+      std::stringstream ss;
+      ss << "Time zone file " << filename << " does not exist."
+         << " Please install IANA time zone database and set TZDIR env.";
+      throw TimezoneError(ss.str());
+    }
     try {
       std::unique_ptr<InputStream> file = readFile(filename);
       size_t size = static_cast<size_t>(file->getLength());
@@ -880,8 +886,8 @@ namespace orc {
           strftime(buffer, sizeof(buffer), "%F %H:%M:%S", &timeStruct);
         }
       }
-      std::cout << "  Transition: " << (result == nullptr ? "null" : buffer) 
<< " ("
-                << transitions[t] << ") -> " << 
variants[currentVariant[t]].name << "\n";
+      out << "  Transition: " << (result == nullptr ? "null" : buffer) << " (" 
<< transitions[t]
+          << ") -> " << variants[currentVariant[t]].name << "\n";
     }
   }
 
diff --git a/c++/test/TestTimezone.cc b/c++/test/TestTimezone.cc
index 2386753a4..69f3b854e 100644
--- a/c++/test/TestTimezone.cc
+++ b/c++/test/TestTimezone.cc
@@ -18,6 +18,7 @@
 
 #include "Adaptor.hh"
 #include "Timezone.hh"
+#include "wrap/gmock.h"
 #include "wrap/gtest-wrapper.h"
 
 #include <iostream>
@@ -401,4 +402,21 @@ namespace orc {
     // DST ends in Los Angeles November 5, 2:00 am
     EXPECT_EQ(1699164000 + 8 * 3600, la->convertFromUTC(1699164000));
   }
+
+#ifndef _MSC_VER
+  TEST(TestTimezone, testMissingTZDB) {
+    const char* tzDirBackup = std::getenv("TZDIR");
+    setenv("TZDIR", "/path/to/wrong/tzdb", 1);
+    EXPECT_THAT([]() { getTimezoneByName("America/Los_Angeles"); },
+                testing::ThrowsMessage<TimezoneError>(testing::HasSubstr(
+                    "Time zone file /path/to/wrong/tzdb/America/Los_Angeles 
does not exist."
+                    " Please install IANA time zone database and set TZDIR 
env.")));
+    if (tzDirBackup != nullptr) {
+      setenv("TZDIR", tzDirBackup, 1);
+    } else {
+      unsetenv("TZDIR");
+    }
+  }
+#endif
+
 }  // namespace orc

Reply via email to