Line data Source code
1 : /*
2 : * SPDX-License-Identifier: GPL-3.0-only
3 : * MuseScore-CLA-applies
4 : *
5 : * MuseScore Studio
6 : * Music Composition & Notation
7 : *
8 : * Copyright (C) 2021 MuseScore Limited and others
9 : *
10 : * This program is free software: you can redistribute it and/or modify
11 : * it under the terms of the GNU General Public License version 3 as
12 : * published by the Free Software Foundation.
13 : *
14 : * This program is distributed in the hope that it will be useful,
15 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 : * GNU General Public License for more details.
18 : *
19 : * You should have received a copy of the GNU General Public License
20 : * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 : */
22 : #include "dataformatter.h"
23 :
24 : #include "translation.h"
25 : #include "types/datetime.h"
26 :
27 : using namespace muse;
28 :
29 0 : double DataFormatter::roundDouble(const double& val, const int decimals)
30 : {
31 0 : return String::number(val, decimals).toDouble();
32 : }
33 :
34 22 : String DataFormatter::formatReal(double val, int prec)
35 : {
36 22 : return String::number(val, prec);
37 : }
38 :
39 179 : String DataFormatter::formatTimeSince(const Date& date)
40 : {
41 179 : Date currentDate = DateTime::currentDateTime().date();
42 179 : int days = date.daysTo(currentDate);
43 :
44 179 : if (days == 0) {
45 177 : return mtrc("global", "Today");
46 : }
47 :
48 2 : if (days == 1) {
49 0 : return mtrc("global", "Yesterday");
50 : }
51 :
52 2 : if (days < 7) {
53 0 : return mtrc("global", "%n day(s) ago", nullptr, days);
54 : }
55 :
56 2 : int weeks = days / 7;
57 :
58 2 : if (weeks == 1) {
59 0 : return mtrc("global", "Last week");
60 : }
61 :
62 2 : if (weeks <= 4) {
63 0 : return mtrc("global", "%n week(s) ago", nullptr, weeks);
64 : }
65 :
66 2 : constexpr int monthsInYear = 12;
67 :
68 2 : int months = (currentDate.year() - date.year()) * monthsInYear + (currentDate.month() - date.month());
69 :
70 2 : if (months == 1) {
71 0 : return mtrc("global", "Last month");
72 : }
73 :
74 2 : if (months < monthsInYear) {
75 2 : return mtrc("global", "%n month(s) ago", nullptr, months);
76 : }
77 :
78 0 : int years = currentDate.year() - date.year();
79 :
80 0 : return mtrc("global", "%n year(s) ago", nullptr, years);
81 : }
82 :
83 34 : String DataFormatter::formatFileSize(size_t size)
84 : {
85 34 : if (size >= 1024 * 1024 * 1024) {
86 0 : double gb = double(size) / (1024 * 1024 * 1024);
87 : //: Abbreviation of "gigabyte", used to indicate file size
88 0 : return mtrc("global", "%1 GB", "gigabyte").arg(formatReal(gb, 2));
89 : }
90 :
91 34 : if (size >= 1024 * 1024) {
92 12 : double mb = double(size) / (1024 * 1024);
93 : //: Abbreviation of "megabyte", used to indicate file size
94 12 : return mtrc("global", "%1 MB", "megabyte").arg(formatReal(mb, 1));
95 : }
96 :
97 22 : if (size >= 1024) {
98 10 : double kb = double(size) / 1024;
99 : //: Abbreviation of "kilobyte", used to indicate file size
100 10 : return mtrc("global", "%1 KB", "kilobyte").arg(formatReal(kb, 0));
101 : }
102 :
103 : //: Used to indicate file size. Ideally, keep the translation short; feel free to use an abbreviation.
104 12 : return mtrc("global", "%Ln byte(s)", nullptr, int(size));
105 : }
|