QGIS API Documentation 3.41.0-Master (45a0abf3bec)
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
7 email : matthias@opengis.ch
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 "qgssettings.h"
19#include "qgsfield.h"
20#include "qgsvectorlayer.h"
21#include "qgsapplication.h"
22#include "qgsvariantutils.h"
23
24const QString QgsDateTimeFieldFormatter::DATE_FORMAT = QStringLiteral( "yyyy-MM-dd" );
25const QString QgsDateTimeFieldFormatter::TIME_FORMAT = QStringLiteral( "HH:mm:ss" );
26const QString QgsDateTimeFieldFormatter::DATETIME_FORMAT = QStringLiteral( "yyyy-MM-dd HH:mm:ss" );
27// we need to use Qt::ISODate rather than a string format definition in QDate::fromString
28const QString QgsDateTimeFieldFormatter::QT_ISO_FORMAT = QStringLiteral( "Qt ISO Date" );
29// but QDateTimeEdit::setDisplayFormat only accepts string formats, so use with time zone by default
30const QString QgsDateTimeFieldFormatter::DISPLAY_FOR_ISO_FORMAT = QStringLiteral( "yyyy-MM-dd HH:mm:ss+t" );
31QString QgsDateTimeFieldFormatter::DATE_DISPLAY_FORMAT = QStringLiteral( "yyyy-MM-dd" );
32QString QgsDateTimeFieldFormatter::DATETIME_DISPLAY_FORMAT = QStringLiteral( "yyyy-MM-dd HH:mm:ss" );
33
34
36{
37 return QStringLiteral( "DateTime" );
38}
39
40QString QgsDateTimeFieldFormatter::representValue( QgsVectorLayer *layer, int fieldIndex, const QVariantMap &config, const QVariant &cache, const QVariant &value ) const
41{
42 Q_UNUSED( cache )
43
44 QString result;
45
46 if ( QgsVariantUtils::isNull( value ) )
47 {
49 }
50
51 if ( fieldIndex < 0 || fieldIndex >= layer->fields().size() )
52 {
53 return value.toString();
54 }
55
56 const QgsField field = layer->fields().at( fieldIndex );
57 const bool fieldIsoFormat = config.value( QStringLiteral( "field_iso_format" ), false ).toBool();
58 const QString fieldFormat = config.value( QStringLiteral( "field_format" ), defaultFormat( field.type() ) ).toString();
59 const QString displayFormat = config.value( QStringLiteral( "display_format" ), defaultDisplayFormat( field.type() ) ).toString();
60
61 QDateTime date;
62 bool showTimeZone = false;
63 if ( static_cast<QMetaType::Type>( value.userType() ) == QMetaType::QDate )
64 {
65 date = value.toDateTime();
66 }
67 else if ( static_cast<QMetaType::Type>( value.userType() ) == QMetaType::QDateTime )
68 {
69 date = value.toDateTime();
70 // we always show time zones for datetime values
71 showTimeZone = true;
72 }
73 else if ( static_cast<QMetaType::Type>( value.userType() ) == QMetaType::QTime )
74 {
75 return value.toTime().toString( displayFormat );
76 }
77 else
78 {
79 if ( fieldIsoFormat )
80 {
81 date = QDateTime::fromString( value.toString(), Qt::ISODate );
82 }
83 else
84 {
85 date = QDateTime::fromString( value.toString(), fieldFormat );
86 }
87 }
88
89 if ( date.isValid() )
90 {
91 if ( showTimeZone && displayFormat == QgsDateTimeFieldFormatter::DATETIME_DISPLAY_FORMAT )
92 {
93 // using default display format for datetimes, so ensure we include the timezone
94 result = QStringLiteral( "%1 (%2)" ).arg( date.toString( displayFormat ), date.timeZoneAbbreviation() );
95 }
96 else
97 {
98 // Convert to UTC if the format string includes a Z, as QLocale::toString() doesn't do it
99 if ( displayFormat.indexOf( "Z" ) > 0 )
100 date = date.toUTC();
101
102 result = date.toString( displayFormat );
103 }
104 }
105 else
106 {
107 result = value.toString();
108 }
109
110 return result;
111}
112
113QString QgsDateTimeFieldFormatter::defaultFormat( QMetaType::Type type )
114{
115 switch ( type )
116 {
117 case QMetaType::Type::QDateTime:
119 case QMetaType::Type::QTime:
121 default:
123 }
124}
125
126QString QgsDateTimeFieldFormatter::defaultFormat( QVariant::Type type )
127{
129}
130
131
133{
134 switch ( type )
135 {
136 case QMetaType::Type::QDateTime:
138 case QMetaType::Type::QTime:
140 default:
142 }
143}
144
149
151{
152 QString dateFormat = QLocale().dateFormat( QLocale::FormatType::ShortFormat );
155}
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:53
QMetaType::Type type
Definition qgsfield.h:60
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 data sets.