/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

#include "iceberg/util/timepoint.h"

#include <chrono>
#include <iomanip>
#include <sstream>

namespace iceberg {

TimePointMs TimePointMsFromUnixMs(int64_t unix_ms) {
  return TimePointMs{std::chrono::milliseconds(unix_ms)};
}

int64_t UnixMsFromTimePointMs(TimePointMs time_point_ms) {
  return std::chrono::duration_cast<std::chrono::milliseconds>(
             time_point_ms.time_since_epoch())
      .count();
}

TimePointNs TimePointNsFromUnixNs(int64_t unix_ns) {
  return TimePointNs{std::chrono::nanoseconds(unix_ns)};
}

int64_t UnixNsFromTimePointNs(TimePointNs time_point_ns) {
  return std::chrono::duration_cast<std::chrono::nanoseconds>(
             time_point_ns.time_since_epoch())
      .count();
}

std::string FormatTimePointMs(TimePointMs time_point_ms) {
  auto unix_ms = UnixMsFromTimePointMs(time_point_ms);
  auto time_t = std::chrono::system_clock::to_time_t(time_point_ms);

  // Format as ISO 8601-like string: YYYY-MM-DD HH:MM:SS
  std::ostringstream oss;
  oss << std::put_time(std::gmtime(&time_t), "%Y-%m-%d %H:%M:%S");

  // Add milliseconds
  auto ms = unix_ms % 1000;
  oss << "." << std::setfill('0') << std::setw(3) << ms << " UTC";

  return oss.str();
}

TimePointMs CurrentTimePointMs() {
  auto now = std::chrono::system_clock::now();
  auto duration_since_epoch = now.time_since_epoch();
  return TimePointMs{
      std::chrono::duration_cast<std::chrono::milliseconds>(duration_since_epoch)};
}

}  // namespace iceberg
