QGIS API Documentation 3.99.0-Master (d270888f95f)
Loading...
Searching...
No Matches
qgsdatetimefieldformatter.cpp
Go to the documentation of this file.
1/***************************************************************************
2 qgsdatetimefieldformatter.cpp - QgsDateTimeFieldFormatter
3
4 ---------------------
5 begin : 2.12.2016
6 copyright : (C) 2016 by Matthias Kuhn
8 ***************************************************************************
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 as published by *
12 * the Free Software Foundation; either version 2 of the License, or *
13 * (at your option) any later version. *
14 * *
15 ***************************************************************************/
17
18#include "qgsapplication.h"
19#include "qgsfield.h"
20#include "qgssettings.h"
21#include "qgsvariantutils.h"
22#include "qgsvectorlayer.h"
23
24#include <QString>
25
26using namespace Qt::StringLiterals;
27
28const QString QgsDateTimeFieldFormatter::DATE_FORMAT = u"yyyy-MM-dd"_s;
29const QString QgsDateTimeFieldFormatter::TIME_FORMAT = u"HH:mm:ss"_s;
30const QString QgsDateTimeFieldFormatter::DATETIME_FORMAT = u"yyyy-MM-dd HH:mm:ss"_s;
31// we need to use Qt::ISODate rather than a string format definition in QDate::fromString
32const QString QgsDateTimeFieldFormatter::QT_ISO_FORMAT = u"Qt ISO Date"_s;
33// but QDateTimeEdit::setDisplayFormat only accepts string formats, so use with time zone by default
34const QString QgsDateTimeFieldFormatter::DISPLAY_FOR_ISO_FORMAT = u"yyyy-MM-dd HH:mm:ss+t"_s;
36QString QgsDateTimeFieldFormatter::DATETIME_DISPLAY_FORMAT = u"yyyy-MM-dd HH:mm:ss"_s;
37
38
40{
41 return u"DateTime"_s;
42}
43
44QString QgsDateTimeFieldFormatter::representValue( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config, const QVariant &cache, const QVariant &value ) const
45{
46 Q_UNUSED( cache )
47
48 QString result;
49
50 if ( QgsVariantUtils::isNull( value ) )
51 {
53 }
54
55 if ( fieldIndex < 0 || fieldIndex >= layer->fields().size() )
56 {
57 return value.toString();
58 }
59
60 const QgsField field = layer->fields().at( fieldIndex );
61 const bool fieldIsoFormat = config.value( u"field_iso_format"_s, false ).toBool();
62 const QString fieldFormat = config.value( u"field_format"_s, defaultFormat( field.type() ) ).toString();
63 const QString displayFormat = config.value( u"display_format"_s, defaultDisplayFormat( field.type() ) ).toString();
64
65 QDateTime date;
66 bool showTimeZone = false;
67 if ( static_cast<QMetaType::Type>( value.userType() ) == QMetaType::QDate )
68 {
69 date = value.toDateTime();
70 }
71 else if ( static_cast<QMetaType::Type>( value.userType() ) == QMetaType::QDateTime )
72 {
73 date = value.toDateTime();
74 // we always show time zones for datetime values
75 showTimeZone = true;
76 }
77 else if ( static_cast<QMetaType::Type>( value.userType() ) == QMetaType::QTime )
78 {
79 return value.toTime().toString( displayFormat );
80 }
81 else
82 {
83 if ( fieldIsoFormat )
84 {
85 date = QDateTime::fromString( value.toString(), Qt::ISODate );
86 }
87 else
88 {
89 date = QDateTime::fromString( value.toString(), fieldFormat );
90 }
91 }
92
93 if ( date.isValid() )
94 {
95 if ( showTimeZone && displayFormat == QgsDateTimeFieldFormatter::DATETIME_DISPLAY_FORMAT )
96 {
97 // using default display format for datetimes, so ensure we include the timezone
98 result = u"%1 (%2)"_s.arg( date.toString( displayFormat ), date.timeZoneAbbreviation() );
99 }
100 else
101 {
102 // Convert to UTC if the format string includes a Z, as QLocale::toString() doesn't do it
103 if ( displayFormat.indexOf( "Z" ) > 0 )
104 date = date.toUTC();
105
106 result = date.toString( displayFormat );
107 }
108 }
109 else
110 {
111 result = value.toString();
112 }
113
114 return result;
115}
116
117QString QgsDateTimeFieldFormatter::defaultFormat( QMetaType::Type type )
118{
119 switch ( type )
120 {
121 case QMetaType::Type::QDateTime:
123 case QMetaType::Type::QTime:
125 default:
127 }
128}
129
130QString QgsDateTimeFieldFormatter::defaultFormat( QVariant::Type type )
131{
133}
134
135
137{
138 switch ( type )
139 {
140 case QMetaType::Type::QDateTime:
142 case QMetaType::Type::QTime:
144 default:
146 }
147}
148
153
155{
156 QString dateFormat = QLocale().dateFormat( QLocale::FormatType::ShortFormat );
159}
static QString nullRepresentation()
Returns the string used to represent the value NULL throughout QGIS.
static void applyLocaleChange()
Adjusts the date time display formats according to locale.
QString representValue(QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config, const QVariant &cache, const QVariant &value) const override
Create a pretty String representation of the value.
static const QString QT_ISO_FORMAT
Date time format was localized by applyLocaleChange before QGIS 3.30.
QString id() const override
Returns a unique id for this field formatter.
static const QString DISPLAY_FOR_ISO_FORMAT
static QString defaultDisplayFormat(QMetaType::Type type)
Gets the default display format in function of the type.
static QString defaultFormat(QMetaType::Type type)
Gets the default format in function of the type.
static QString DATETIME_DISPLAY_FORMAT
Date display format is localized by applyLocaleChange.
static const QString TIME_FORMAT
Date format was localized by applyLocaleChange before QGIS 3.30.
Encapsulate a field in an attribute table or data source.
Definition qgsfield.h:56
QMetaType::Type type
Definition qgsfield.h:63
int size() const
Returns number of items.
QgsField at(int i) const
Returns the field at particular index (must be in range 0..N-1).
static QMetaType::Type variantTypeToMetaType(QVariant::Type variantType)
Converts a QVariant::Type to a QMetaType::Type.
static bool isNull(const QVariant &variant, bool silenceNullWarnings=false)
Returns true if the specified variant should be considered a NULL value.
Represents a vector layer which manages a vector based dataset.