From bafc52afd1da7cd19a699fbb7a5f52dcc66eb00e Mon Sep 17 00:00:00 2001 From: poire-z Date: Sat, 29 Aug 2020 18:25:34 +0200 Subject: [PATCH] util: adds util.secondsToHour(), util.secondsToDate() --- frontend/apps/reader/modules/readerfooter.lua | 14 +------- frontend/ui/widget/touchmenu.lua | 15 ++------- frontend/util.lua | 32 +++++++++++++++++++ 3 files changed, 35 insertions(+), 26 deletions(-) diff --git a/frontend/apps/reader/modules/readerfooter.lua b/frontend/apps/reader/modules/readerfooter.lua index 83aba1e71..e0f4d7648 100644 --- a/frontend/apps/reader/modules/readerfooter.lua +++ b/frontend/apps/reader/modules/readerfooter.lua @@ -158,19 +158,7 @@ local footerTextGeneratorMap = { time = function(footer) local symbol_type = footer.settings.item_prefix or "icons" local prefix = symbol_prefix[symbol_type].time - local clock - if footer.settings.time_format == "12" then - if os.date("%p") == "AM" then - -- @translators This is the time in the morning in the 12-hour clock (%I is the hour, %M the minute). - clock = os.date(_("%I:%M AM")) - else - -- @translators This is the time in the afternoon in the 12-hour clock (%I is the hour, %M the minute). - clock = os.date(_("%I:%M PM")) - end - else - -- @translators This is the time in the 24-hour clock (%H is the hour, %M the minute). - clock = os.date(_("%H:%M")) - end + local clock = util.secondsToHour(os.time(), footer.settings.time_format == "12") if not prefix then return clock else diff --git a/frontend/ui/widget/touchmenu.lua b/frontend/ui/widget/touchmenu.lua index 175329f97..fb0f969fb 100644 --- a/frontend/ui/widget/touchmenu.lua +++ b/frontend/ui/widget/touchmenu.lua @@ -27,6 +27,7 @@ local UIManager = require("ui/uimanager") local UnderlineContainer = require("ui/widget/container/underlinecontainer") local VerticalGroup = require("ui/widget/verticalgroup") local VerticalSpan = require("ui/widget/verticalspan") +local util = require("util") local getMenuText = require("ui/widget/menu").getMenuText local _ = require("gettext") local T = require("ffi/util").template @@ -620,19 +621,7 @@ function TouchMenu:updateItems() self.page_info_left_chev:enableDisable(self.page > 1) self.page_info_right_chev:enableDisable(self.page < self.page_num) - local time_info_txt - if G_reader_settings:nilOrTrue("twelve_hour_clock") then - if os.date("%p") == "AM" then - -- @translators This is the time in the morning in the 12-hour clock (%I is the hour, %M the minute). - time_info_txt = os.date(_("%I:%M AM")) - else - -- @translators This is the time in the afternoon in the 12-hour clock (%I is the hour, %M the minute). - time_info_txt = os.date(_("%I:%M PM")) - end - else - -- @translators This is the time in the 24-hour clock (%H is the hour, %M the minute). - time_info_txt = os.date(_("%H:%M")) - end + local time_info_txt = util.secondsToHour(os.time(), G_reader_settings:nilOrTrue("twelve_hour_clock")) local powerd = Device:getPowerDevice() local batt_lvl = powerd:getCapacity() local batt_symbol diff --git a/frontend/util.lua b/frontend/util.lua index f6ba194f6..ea7a4d689 100644 --- a/frontend/util.lua +++ b/frontend/util.lua @@ -194,6 +194,38 @@ function util.secondsToHClock(seconds, withoutSeconds, hmsFormat) end end +--- Converts timestamp to an hour string +---- @int seconds number of seconds +---- @bool twelve_hour_clock +---- @treturn string hour string +function util.secondsToHour(seconds, twelve_hour_clock) + local time + if twelve_hour_clock then + if os.date("%p", seconds) == "AM" then + -- @translators This is the time in the morning in the 12-hour clock (%I is the hour, %M the minute). + time = os.date(_("%I:%M AM"), seconds) + else + -- @translators This is the time in the afternoon in the 12-hour clock (%I is the hour, %M the minute). + time = os.date(_("%I:%M PM"), seconds) + end + else + -- @translators This is the time in the 24-hour clock (%H is the hour, %M the minute). + time = os.date(_("%H:%M"), seconds) + end + return time +end + +--- Converts timestamp to a date string +---- @int seconds number of seconds +---- @bool twelve_hour_clock +---- @treturn string date string +function util.secondsToDate(seconds, twelve_hour_clock) + local BD = require("ui/bidi") + local time = util.secondsToHour(seconds, twelve_hour_clock) + -- @translators This is the date (%Y is the year, %m the month, %d the day) + local day = os.date(_("%Y-%m-%d"), seconds) + return BD.wrap(day) .. " " .. BD.wrap(time) +end --[[-- Compares values in two different tables.