Skip to content

Commit

Permalink
set correct date when adding uenv and print date with image ls
Browse files Browse the repository at this point in the history
  • Loading branch information
bcumming committed Oct 30, 2024
1 parent 6e18d4e commit e480146
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 5 deletions.
6 changes: 5 additions & 1 deletion src/cli/add_remove.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,11 @@ int image_add(const image_add_args& args, const global_settings& settings) {
//
// add the uenv to the database
//
uenv::uenv_date date;
uenv::uenv_date date{*util::file_creation_date(sqfs)};
if (!date.validate()) {
spdlog::error("the date {} is invalid", date);
return 1;
}
uenv_record r{
*label->system, *label->uarch, *label->name,
*label->version, *label->tag, date,
Expand Down
8 changes: 4 additions & 4 deletions src/cli/ls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,13 @@ int image_ls(const image_ls_args& args, const global_settings& settings) {
++w_sys;
++w_arch;
if (!args.no_header) {
fmt::println("{:<{}}{:<{}}{:<{}}{:<18}", "uenv", w_name, "arch", w_arch,
"system", w_sys, "id");
fmt::println("{:<{}}{:<{}}{:<{}}{:<17}{:<10}", "uenv", w_name, "arch",
w_arch, "system", w_sys, "id", "date");
}
for (auto& r : *result) {
auto name = fmt::format("{}/{}:{}", r.name, r.version, r.tag);
fmt::println("{:<{}}{:<{}}{:<{}}{:<18}", name, w_name, r.uarch, w_arch,
r.system, w_sys, r.id.string());
fmt::println("{:<{}}{:<{}}{:<{}}{:<17}{:s}", name, w_name, r.uarch,
w_arch, r.system, w_sys, r.id.string(), r.date);
}

return 0;
Expand Down
5 changes: 5 additions & 0 deletions src/uenv/uenv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ std::optional<std::string> uenv_description::mount() const {
return mount_;
}

uenv_date::uenv_date(std::tm other)
: year(other.tm_year + 1900), month(other.tm_mon + 1), day(other.tm_mday),
hour(other.tm_hour), minute(other.tm_min), second(other.tm_sec) {
}

bool uenv_date::validate() const {
if (year < 2022 || year > 2050) {
return false;
Expand Down
2 changes: 2 additions & 0 deletions src/uenv/uenv.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <ctime>
#include <filesystem>
#include <optional>
#include <stdexcept>
Expand Down Expand Up @@ -43,6 +44,7 @@ struct uenv_date {
uenv_date(int_t y, int_t m, int_t d, int_t h, int_t min, int_t s)
: year(y), month(m), day(d), hour(h), minute(min), second(s) {
}
uenv_date(std::tm);

auto operator<=>(const uenv_date&) const = default;
};
Expand Down
20 changes: 20 additions & 0 deletions src/util/fs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,24 @@ unsquashfs_tmp(const std::filesystem::path& sqfs,
sqfs.string(), base.string());
return base;
}

util::expected<std::tm, std::string>
file_creation_date(const std::filesystem::path& path) {
namespace fs = std::filesystem;
namespace cr = std::chrono;

const auto creation_time = fs::last_write_time(path);

// convert file_time_type to system clock time_point
const auto sctp = cr::time_point_cast<cr::system_clock::duration>(
creation_time - fs::file_time_type::clock::now() +
cr::system_clock::now());

// convert to time_t for easy manipulation
std::time_t cftime = cr::system_clock::to_time_t(sctp);

// extract the date components
return *std::gmtime(&cftime);
}

} // namespace util
4 changes: 4 additions & 0 deletions src/util/fs.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <ctime>
#include <filesystem>
#include <string>

Expand All @@ -15,4 +16,7 @@ unsquashfs_tmp(const std::filesystem::path& sqfs,

void clear_temp_dirs();

util::expected<std::tm, std::string>
file_creation_date(const std::filesystem::path& path);

} // namespace util

0 comments on commit e480146

Please sign in to comment.