Ticket #13686: 0001-Use-formatters-from-locale-kit-to-format-time-remain.2.patch

File 0001-Use-formatters-from-locale-kit-to-format-time-remain.2.patch, 4.7 KB (added by akshay, 7 years ago)
  • src/apps/webpositive/DownloadProgressView.cpp

    From f741737bd1ad04cc2ab8aab1048ee66a6564a987 Mon Sep 17 00:00:00 2001
    From: Akshay Agarwal <agarwal.akshay.akshay8@gmail.com>
    Date: Tue, 29 Aug 2017 12:20:12 +0530
    Subject: [PATCH] Use formatters from locale kit to format time
     remaining/finish time.
    
    ---
     src/apps/webpositive/DownloadProgressView.cpp | 84 +++++++++------------------
     1 file changed, 27 insertions(+), 57 deletions(-)
    
    diff --git a/src/apps/webpositive/DownloadProgressView.cpp b/src/apps/webpositive/DownloadProgressView.cpp
    index 2e50c96..927461d 100644
    a b  
    1515#include <Catalog.h>
    1616#include <Clipboard.h>
    1717#include <Directory.h>
     18#include <DateTimeFormat.h>
     19#include <DurationFormat.h>
    1820#include <Entry.h>
    1921#include <FindDirectory.h>
    2022#include <GroupLayoutBuilder.h>
     
    2830#include <SpaceLayoutItem.h>
    2931#include <StatusBar.h>
    3032#include <StringView.h>
    31 #include <TimeUnitFormat.h>
     33#include <TimeFormat.h>
    3234
    3335#include "BrowserWindow.h"
    3436#include "WebDownload.h"
    const bigtime_t kShowEstimatedFinishInterval = 4000000LL;  
    5557
    5658bigtime_t DownloadProgressView::sLastEstimatedFinishSpeedToggleTime = -1;
    5759bool DownloadProgressView::sShowSpeed = true;
     60static const time_t kSecondsPerDay = 24 * 60 * 60;
     61static const time_t kSecondsPerHour = 60 * 60;
    5862
    5963
    6064class IconView : public BView {
    DownloadProgressView::_UpdateStatusText()  
    788792        time_t now = (time_t)real_time_clock();
    789793        time_t finishTime = (time_t)(now + secondsRemaining);
    790794
    791         tm _time;
    792         tm* time = localtime_r(&finishTime, &_time);
    793         int32 year = time->tm_year + 1900;
    794 
    795         char timeText[32];
    796         time_t secondsPerDay = 24 * 60 * 60;
    797         // TODO: Localization of time string...
    798         if (now < finishTime - secondsPerDay) {
    799             // process is going to take more than a day!
    800             sprintf(timeText, "%0*d:%0*d %0*d/%0*d/%" B_PRId32,
    801                 2, time->tm_hour, 2, time->tm_min,
    802                 2, time->tm_mon + 1, 2, time->tm_mday, year);
     795        BString timeText;
     796        if (finishTime - now > kSecondsPerDay) {
     797            BDateTimeFormat().Format(timeText, finishTime,
     798                B_MEDIUM_DATE_FORMAT, B_MEDIUM_TIME_FORMAT);
    803799        } else {
    804             sprintf(timeText, "%0*d:%0*d",
    805                 2, time->tm_hour, 2, time->tm_min);
     800            BTimeFormat().Format(timeText, finishTime,
     801                B_MEDIUM_TIME_FORMAT);
    806802        }
    807803
    808         BString buffer1(B_TRANSLATE_COMMENT("Finish: ", "Finishing time"));
    809         buffer1 << timeText;
    810         finishTime -= now;
    811         time = gmtime(&finishTime);
    812 
    813         BTimeUnitFormat timeFormat;
    814 
    815         BString buffer2;
    816         if (finishTime > secondsPerDay) {
    817             int64 days = finishTime / secondsPerDay;
    818 
    819             BString time;
    820             timeFormat.Format(time, days, B_TIME_UNIT_DAY);
    821 
    822             buffer2 << B_TRANSLATE("Over %days left");
    823             buffer2.ReplaceFirst("%days", time);
    824         } else if (finishTime > 60 * 60) {
    825             int64 hours = finishTime / (60 * 60);
    826             BString time;
    827             timeFormat.Format(time, hours, B_TIME_UNIT_HOUR);
    828 
    829             buffer2 << B_TRANSLATE("Over %hours left");
    830             buffer2.ReplaceFirst("%hours", time);
    831         } else if (finishTime > 60) {
    832             int64 minutes = finishTime / 60;
    833             if (minutes == 1)
    834                 buffer2 << B_TRANSLATE("Over 1 minute left");
    835             else
    836                 timeFormat.Format(buffer2, minutes, B_TIME_UNIT_MINUTE);
     804        BString statusString;
     805        BDurationFormat formatter;
     806        BString finishString;
     807        if (finishTime - now > kSecondsPerHour) {
     808            statusString.SetTo(B_TRANSLATE("(Finish: %date - Over %duration left)"));
     809            formatter.Format(finishString, now * 1000000LL, finishTime * 1000000LL);
    837810        } else {
    838             BString time;
    839             timeFormat.Format(time, finishTime, B_TIME_UNIT_SECOND);
    840 
    841             buffer2 << B_TRANSLATE("%seconds left");
    842             buffer2.ReplaceFirst("%seconds", time);
     811            statusString.SetTo(B_TRANSLATE("(Finish: %date - %duration left)"));
     812            formatter.Format(finishString, now * 1000000LL, finishTime * 1000000LL);
    843813        }
    844814
    845         buffer = "(";
    846         buffer << buffer1 << " - " << buffer2 << ")";
     815        statusString.ReplaceFirst("%date", timeText);
     816        statusString.ReplaceFirst("%duration", finishString);
    847817
    848         float stringWidth = fInfoView->StringWidth(buffer.String());
     818        float stringWidth = fInfoView->StringWidth(statusString.String());
    849819        if (stringWidth < fInfoView->Bounds().Width())
    850             fInfoView->SetText(buffer.String());
     820            fInfoView->SetText(statusString.String());
    851821        else {
    852822            // complete string too wide, try with shorter version
    853             buffer = "(";
    854             buffer << buffer1 << ")";
    855             stringWidth = fInfoView->StringWidth(buffer.String());
     823            statusString.SetTo(B_TRANSLATE("(Finish: %date)"));
     824            statusString.ReplaceFirst("%date", timeText);
     825            stringWidth = fInfoView->StringWidth(statusString.String());
    856826            if (stringWidth < fInfoView->Bounds().Width())
    857                 fInfoView->SetText(buffer.String());
     827                fInfoView->SetText(statusString.String());
    858828        }
    859829    }
    860830}