KCoreAddons

formats.h
1/*
2 SPDX-FileCopyrightText: 2014 Bhushan Shah <bhush94@gmail.com>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5*/
6
7#ifndef FORMATS_H
8#define FORMATS_H
9
10#include <KFormat>
11#include <QObject>
12
13class Formats : public QObject
14{
16
17public:
18 /**
19 * Converts size from bytes to the appropriate string representation
20 */
21 Q_INVOKABLE QString formatByteSize(double size, int precision = 1) const;
22
23 /**
24 * Given a number of milliseconds, converts that to a string containing
25 * the localized equivalent, e.g. 1:23:45
26 */
27 Q_INVOKABLE QString formatDuration(quint64 msecs, KFormat::DurationFormatOptions options = KFormat::DefaultDuration) const;
28
29 /**
30 * Given a number of milliseconds, converts that to a string containing
31 * the localized equivalent to the requested decimal places.
32 *
33 * e.g. given formatDuration(60000), returns "1.0 minutes"
34 */
35 Q_INVOKABLE QString formatDecimalDuration(quint64 msecs, int decimalPlaces = 2) const;
36
37 /**
38 * Given a number of milliseconds, converts that to a spell-out string containing
39 * the localized equivalent.
40 *
41 * e.g. given formatSpelloutDuration(60001) returns "1 minute"
42 * given formatSpelloutDuration(62005) returns "1 minute and 2 seconds"
43 * given formatSpelloutDuration(90060000) returns "1 day and 1 hour"
44 *
45 * Units not interesting to the user, for example seconds or minutes when the first
46 * unit is day, are not returned because they are irrelevant. The same applies for
47 * seconds when the first unit is hour.
48 *
49 */
50 Q_INVOKABLE QString formatSpelloutDuration(quint64 msecs) const;
51
52 /**
53 * Returns a string formatted to a relative date style.
54 *
55 * If the date falls within one week before or after the current date
56 * then a relative date string will be returned, such as:
57 * * Yesterday
58 * * Today
59 * * Tomorrow
60 * * Last Tuesday
61 * * Next Wednesday
62 *
63 * If the date falls outside this period then the format is used
64 */
65 Q_INVOKABLE QString formatRelativeDate(const QDate &date, QLocale::FormatType format) const;
66
67 /**
68 * Returns a string formatted to a relative datetime style.
69 *
70 * If the dateTime falls within one week before or after the current date
71 * then a relative date string will be returned, such as:
72 * * Yesterday, 3:00pm
73 * * Today, 3:00pm
74 * * Tomorrow, 3:00pm
75 * * Last Tuesday, 3:00pm
76 * * Next Wednesday, 3:00pm
77 *
78 * If the datetime falls outside this period then the format is used
79 */
80 Q_INVOKABLE QString formatRelativeDateTime(const QDateTime &dateTime, QLocale::FormatType format) const;
81
82private:
83 const KFormat m_format;
84};
85
86#endif
KFormat provides support for formatting numbers and datetimes in formats that are not supported by QL...
Definition kformat.h:59
@ DefaultDuration
Default formatting in localized 1:23:45 format.
Definition kformat.h:184
Q_INVOKABLEQ_INVOKABLE
Q_OBJECTQ_OBJECT
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:08:22 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.