Skip to content

Commit 167cea0

Browse files
pprudhviPindikura Ravindra
authored andcommitted
ARROW-6137: [C++][Gandiva] Use snprintf instead of stringstream in castVARCHAR(timestamp)
Closes apache#5053 from pprudhvi/timestamp-to-string and squashes the following commits: f342f9a <Prudhvi Porandla> change string format of timestamp in castVarchar(timestamp) Authored-by: Prudhvi Porandla <prudhvi.porandla@icloud.com> Signed-off-by: Pindikura Ravindra <ravindra@dremio.com>
1 parent a4b7059 commit 167cea0

1 file changed

Lines changed: 15 additions & 12 deletions

File tree

cpp/src/gandiva/precompiled/time.cc

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18-
#include <iomanip>
19-
#include <sstream>
2018
#include "./epoch_time_point.h"
2119

2220
extern "C" {
2321

22+
#define __STDC_FORMAT_MACROS
23+
#include <inttypes.h>
2424
#include <stdlib.h>
2525
#include <string.h>
2626
#include <time.h>
@@ -701,17 +701,19 @@ const char* castVARCHAR_timestamp_int64(int64 context, timestamp in, int64 lengt
701701
int64 second = extractSecond_timestamp(in);
702702
int64 millis = in % MILLIS_IN_SEC;
703703

704-
// format to yyyy-MM-dd hh:mm:ss.sss
705-
std::stringstream s;
706-
s << std::setfill('0') << std::setw(4) << year << "-" << std::setw(2) << month << "-"
707-
<< std::setw(2) << day << " " << std::setw(2) << hour << ":" << std::setw(2) << minute
708-
<< ":" << std::setw(2) << second << "." << std::setw(3) << millis;
709-
std::string timestamp_str = s.str();
710-
int32 timestamp_str_len = static_cast<int32>(timestamp_str.length());
704+
static const int kTimeStampStringLen = 23;
705+
const int char_buffer_length = kTimeStampStringLen + 1; // snprintf adds \0
706+
char char_buffer[char_buffer_length];
707+
708+
// yyyy-MM-dd hh:mm:ss.sss
709+
snprintf(char_buffer, char_buffer_length,
710+
"%04" PRId64 "-%02" PRId64 "-%02" PRId64 " %02" PRId64 ":%02" PRId64
711+
":%02" PRId64 ".%03" PRId64,
712+
year, month, day, hour, minute, second, millis);
711713

712714
*out_len = static_cast<int32>(length);
713-
if (*out_len > timestamp_str_len) {
714-
*out_len = timestamp_str_len;
715+
if (*out_len > kTimeStampStringLen) {
716+
*out_len = kTimeStampStringLen;
715717
}
716718

717719
if (*out_len <= 0) {
@@ -728,7 +730,8 @@ const char* castVARCHAR_timestamp_int64(int64 context, timestamp in, int64 lengt
728730
*out_len = 0;
729731
return "";
730732
}
731-
memcpy(ret, timestamp_str.data(), *out_len);
733+
734+
memcpy(ret, char_buffer, *out_len);
732735
return ret;
733736
}
734737
} // extern "C"

0 commit comments

Comments
 (0)