1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
// *****************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under *
// * GNU General Public License: https://www.gnu.org/licenses/gpl-3.0 *
// * Copyright (C) Zenju (zenju AT freefilesync DOT org) - All Rights Reserved *
// *****************************************************************************
#include "format_unit.h"
#include <ctime>
#include <cstdio>
#include "basic_math.h"
#include "i18n.h"
#include "time.h"
#include "globals.h"
#include "utf.h"
#include <clocale> //thousands separator
#include "utf.h" //
using namespace zen;
std::wstring zen::formatTwoDigitPrecision(double value)
{
//print two digits: 0,1 | 1,1 | 11
if (numeric::abs(value) < 9.95) //9.99 must not be formatted as "10.0"
return printNumber<std::wstring>(L"%.1f", value);
return numberTo<std::wstring>(numeric::round(value));
}
std::wstring zen::formatThreeDigitPrecision(double value)
{
//print three digits: 0,01 | 0,11 | 1,11 | 11,1 | 111
if (numeric::abs(value) < 9.995) //9.999 must not be formatted as "10.00"
return printNumber<std::wstring>(L"%.2f", value);
if (numeric::abs(value) < 99.95) //99.99 must not be formatted as "100.0"
return printNumber<std::wstring>(L"%.1f", value);
return numberTo<std::wstring>(numeric::round(value));
}
std::wstring zen::formatFilesizeShort(int64_t size)
{
//if (size < 0) return _("Error"); -> really?
if (numeric::abs(size) <= 999)
return _P("1 byte", "%x bytes", static_cast<int>(size));
double sizeInUnit = static_cast<double>(size);
auto formatUnit = [&](const std::wstring& unitTxt) { return replaceCpy(unitTxt, L"%x", formatThreeDigitPrecision(sizeInUnit)); };
sizeInUnit /= 1024;
if (numeric::abs(sizeInUnit) < 999.5)
return formatUnit(_("%x KB"));
sizeInUnit /= 1024;
if (numeric::abs(sizeInUnit) < 999.5)
return formatUnit(_("%x MB"));
sizeInUnit /= 1024;
if (numeric::abs(sizeInUnit) < 999.5)
return formatUnit(_("%x GB"));
sizeInUnit /= 1024;
if (numeric::abs(sizeInUnit) < 999.5)
return formatUnit(_("%x TB"));
sizeInUnit /= 1024;
return formatUnit(_("%x PB"));
}
namespace
{
enum class UnitRemTime
{
sec,
min,
hour,
day
};
std::wstring formatUnitTime(int val, UnitRemTime unit)
{
switch (unit)
{
case UnitRemTime::sec:
return _P("1 sec", "%x sec", val);
case UnitRemTime::min:
return _P("1 min", "%x min", val);
case UnitRemTime::hour:
return _P("1 hour", "%x hours", val);
case UnitRemTime::day:
return _P("1 day", "%x days", val);
}
assert(false);
return _("Error");
}
template <int M, int N>
std::wstring roundToBlock(double timeInHigh,
UnitRemTime unitHigh, const int (&stepsHigh)[M],
int unitLowPerHigh,
UnitRemTime unitLow, const int (&stepsLow)[N])
{
assert(unitLowPerHigh > 0);
const double granularity = 0.1;
const double timeInLow = timeInHigh * unitLowPerHigh;
const int blockSizeLow = granularity * timeInHigh < 1 ?
numeric::nearMatch(granularity * timeInLow, std::begin(stepsLow), std::end(stepsLow)):
numeric::nearMatch(granularity * timeInHigh, std::begin(stepsHigh), std::end(stepsHigh)) * unitLowPerHigh;
const int roundedtimeInLow = static_cast<int>(numeric::round(timeInLow / blockSizeLow) * blockSizeLow);
std::wstring output = formatUnitTime(roundedtimeInLow / unitLowPerHigh, unitHigh);
if (unitLowPerHigh > blockSizeLow)
output += L' ' + formatUnitTime(roundedtimeInLow % unitLowPerHigh, unitLow);
return output;
}
}
std::wstring zen::formatRemainingTime(double timeInSec)
{
const int steps10[] = { 1, 2, 5, 10 };
const int steps24[] = { 1, 2, 3, 4, 6, 8, 12, 24 };
const int steps60[] = { 1, 2, 5, 10, 15, 20, 30, 60 };
//determine preferred unit
double timeInUnit = timeInSec;
if (timeInUnit <= 60)
return roundToBlock(timeInUnit, UnitRemTime::sec, steps60, 1, UnitRemTime::sec, steps60);
timeInUnit /= 60;
if (timeInUnit <= 60)
return roundToBlock(timeInUnit, UnitRemTime::min, steps60, 60, UnitRemTime::sec, steps60);
timeInUnit /= 60;
if (timeInUnit <= 24)
return roundToBlock(timeInUnit, UnitRemTime::hour, steps24, 60, UnitRemTime::min, steps60);
timeInUnit /= 24;
return roundToBlock(timeInUnit, UnitRemTime::day, steps10, 24, UnitRemTime::hour, steps24);
//note: for 10% granularity steps10 yields a valid blocksize only up to timeInUnit == 100!
//for larger time sizes this results in a finer granularity than expected: 10 days -> should not be a problem considering "usual" remaining time for synchronization
}
//std::wstring zen::fractionToString1Dec(double fraction)
//{
// return printNumber<std::wstring>(L"%.1f", fraction * 100.0) + L'%'; //no need to internationalize fraction!?
//}
std::wstring zen::formatFraction(double fraction)
{
return printNumber<std::wstring>(L"%.2f", fraction * 100.0) + L'%'; //no need to internationalize fraction!?
}
std::wstring zen::formatNumber(int64_t n)
{
//::setlocale (LC_ALL, ""); -> see localization.cpp::wxWidgetsLocale
static_assert(sizeof(long long int) == sizeof(n));
return printNumber<std::wstring>(L"%'lld", n); //considers grouping (')
}
std::wstring zen::formatUtcToLocalTime(time_t utcTime)
{
auto errorMsg = [&] { return _("Error") + L" (time_t: " + numberTo<std::wstring>(utcTime) + L')'; };
TimeComp loc = getLocalTime(utcTime);
std::wstring dateString = utfTo<std::wstring>(formatTime(Zstr("%x %X"), loc));
return !dateString.empty() ? dateString : errorMsg();
}
|