|
| 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, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +#include "literal_format.h" |
| 21 | + |
| 22 | +#include <chrono> |
| 23 | +#include <cstring> |
| 24 | +#include <iomanip> |
| 25 | + |
| 26 | +namespace iceberg { |
| 27 | + |
| 28 | +std::string FormatDate(int32_t days_since_epoch) { |
| 29 | + // Convert days since Unix epoch to date |
| 30 | + auto time_point = |
| 31 | + std::chrono::system_clock::time_point{} + std::chrono::days{days_since_epoch}; |
| 32 | + auto date = std::chrono::floor<std::chrono::days>(time_point); |
| 33 | + auto ymd = std::chrono::year_month_day{date}; |
| 34 | + |
| 35 | + std::ostringstream oss; |
| 36 | + oss << static_cast<int>(ymd.year()) << "-" << std::setfill('0') << std::setw(2) |
| 37 | + << static_cast<unsigned>(ymd.month()) << "-" << std::setfill('0') << std::setw(2) |
| 38 | + << static_cast<unsigned>(ymd.day()); |
| 39 | + return oss.str(); |
| 40 | +} |
| 41 | + |
| 42 | +std::string FormatTime(int64_t microseconds_since_midnight) { |
| 43 | + auto hours = microseconds_since_midnight / (1000000LL * 3600); |
| 44 | + auto minutes = (microseconds_since_midnight % (1000000LL * 3600)) / (1000000LL * 60); |
| 45 | + auto seconds = (microseconds_since_midnight % (1000000LL * 60)) / 1000000LL; |
| 46 | + auto micros = microseconds_since_midnight % 1000000LL; |
| 47 | + |
| 48 | + std::ostringstream oss; |
| 49 | + oss << std::setfill('0') << std::setw(2) << hours << ":" << std::setfill('0') |
| 50 | + << std::setw(2) << minutes << ":" << std::setfill('0') << std::setw(2) << seconds |
| 51 | + << "." << std::setfill('0') << std::setw(6) << micros; |
| 52 | + return oss.str(); |
| 53 | +} |
| 54 | + |
| 55 | +std::string FormatTimestamp(int64_t microseconds_since_epoch) { |
| 56 | + auto time_point = std::chrono::system_clock::time_point{} + |
| 57 | + std::chrono::microseconds{microseconds_since_epoch}; |
| 58 | + auto date = std::chrono::floor<std::chrono::days>(time_point); |
| 59 | + auto time_of_day = time_point - date; |
| 60 | + auto micros_of_day = |
| 61 | + std::chrono::duration_cast<std::chrono::microseconds>(time_of_day).count(); |
| 62 | + |
| 63 | + auto ymd = std::chrono::year_month_day{date}; |
| 64 | + |
| 65 | + std::ostringstream oss; |
| 66 | + oss << static_cast<int>(ymd.year()) << "-" << std::setfill('0') << std::setw(2) |
| 67 | + << static_cast<unsigned>(ymd.month()) << "-" << std::setfill('0') << std::setw(2) |
| 68 | + << static_cast<unsigned>(ymd.day()) << "T" << FormatTime(micros_of_day); |
| 69 | + return oss.str(); |
| 70 | +} |
| 71 | + |
| 72 | +std::string FormatTimestampTz(int64_t microseconds_since_epoch) { |
| 73 | + return FormatTimestamp(microseconds_since_epoch) + "+00:00"; |
| 74 | +} |
| 75 | + |
| 76 | +} // namespace iceberg |
0 commit comments