time: weather sunset and sunrise is now using the shell main time token format

This commit is contained in:
Lemmy
2026-05-09 11:09:53 -04:00
parent 78f90dc96b
commit 44e85bb630
3 changed files with 48 additions and 13 deletions
+9 -13
View File
@@ -4,6 +4,7 @@
#include "i18n/i18n.h"
#include "render/scene/effect_node.h"
#include "system/weather_service.h"
#include "time/time_format.h"
#include "ui/controls/flex.h"
#include "ui/controls/glyph.h"
#include "ui/controls/label.h"
@@ -25,15 +26,6 @@ namespace {
constexpr float kCurrentGlyphSize = Style::controlHeightLg * 2.2f;
std::string formatIsoClock(std::string_view isoTime) {
const auto pos = isoTime.find('T');
const std::size_t start = pos == std::string_view::npos ? 0 : pos + 1;
if (isoTime.size() >= start + 5) {
return std::string(isoTime.substr(start, 5));
}
return std::string(isoTime);
}
std::string windDirectionLabel(int degrees) {
static constexpr std::array<const char*, 8> kDirs = {"N", "NE", "E", "SE", "S", "SW", "W", "NW"};
const int normalized = ((degrees % 360) + 360) % 360;
@@ -662,12 +654,16 @@ void WeatherTab::sync(Renderer& renderer) {
windDirectionLabel(snapshot.current.windDirectionDeg)));
}
if (m_sunriseLabel != nullptr) {
m_sunriseLabel->setText(!snapshot.forecastDays.empty() ? formatIsoClock(snapshot.forecastDays.front().sunriseIso)
: std::string("--"));
const auto& fmt = m_config->config().shell.timeFormat;
m_sunriseLabel->setText(!snapshot.forecastDays.empty()
? formatIsoTime(snapshot.forecastDays.front().sunriseIso, fmt.c_str())
: std::string("--"));
}
if (m_sunsetLabel != nullptr) {
m_sunsetLabel->setText(!snapshot.forecastDays.empty() ? formatIsoClock(snapshot.forecastDays.front().sunsetIso)
: std::string("--"));
const auto& fmt = m_config->config().shell.timeFormat;
m_sunsetLabel->setText(!snapshot.forecastDays.empty()
? formatIsoTime(snapshot.forecastDays.front().sunsetIso, fmt.c_str())
: std::string("--"));
}
auto unit = m_weather->displayTemperatureUnit();
if (m_tempMaxLabel != nullptr) {
+36
View File
@@ -5,6 +5,7 @@
#include <algorithm>
#include <chrono>
#include <cstdint>
#include <cstdio>
#include <ctime>
#include <format>
#include <langinfo.h>
@@ -161,6 +162,41 @@ std::string formatLocalTime(const char* fmt) {
}
}
std::string formatIsoTime(std::string_view isoTime, const char* fmt) {
std::tm tm{};
int year = 0;
int month = 0;
int day = 0;
int hour = 0;
int minute = 0;
if (std::sscanf(std::string(isoTime).c_str(), "%d-%d-%dT%d:%d", &year, &month, &day, &hour, &minute) < 5) {
return std::string(isoTime);
}
tm.tm_year = year - 1900;
tm.tm_mon = month - 1;
tm.tm_mday = day;
tm.tm_hour = hour;
tm.tm_min = minute;
tm.tm_isdst = -1;
mktime(&tm);
const std::string normalizedFmt = normalizeFormatEscapes(fmt);
if (auto compat = formatStrftimeCompat(normalizedFmt, tm)) {
return *compat;
}
using namespace std::chrono;
const auto tp = sys_days{std::chrono::year{year} / std::chrono::month{static_cast<unsigned>(month)} /
std::chrono::day{static_cast<unsigned>(day)}} +
hours{hour} + minutes{minute};
const auto local = local_seconds{tp.time_since_epoch()};
try {
return std::vformat(std::locale(""), normalizedFmt, std::make_format_args(local));
} catch (...) {
return normalizedFmt;
}
}
std::string formatCurrentDate() {
std::string fmt = "%A, ";
fmt += nl_langinfo(D_FMT);
+3
View File
@@ -24,5 +24,8 @@ std::string formatTimeAgo(std::chrono::system_clock::time_point tp);
// numeric specifiers such as "%-I".
[[nodiscard]] std::string formatLocalTime(const char* fmt);
// Formats an ISO 8601 time string (e.g. "2026-05-09T06:23") using the given format.
[[nodiscard]] std::string formatIsoTime(std::string_view isoTime, const char* fmt);
// Formats a filesystem modification time as "YYYY-MM-DD HH:MM".
[[nodiscard]] std::string formatFileTime(const std::filesystem::file_time_type& time);