2
This file is part of the Grantlee template system.
4
Copyright (c) 2009,2010 Stephen Kelly <steveire@gmail.com>
6
This library is free software; you can redistribute it and/or
7
modify it under the terms of the GNU Lesser General Public
8
License as published by the Free Software Foundation; either version
9
2 of the Licence, or (at your option) any later version.
11
This library is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
Library General Public License for more details.
16
You should have received a copy of the GNU Lesser General Public
17
License along with this library. If not, see <http://www.gnu.org/licenses/>.
23
#include <QtCore/QDateTime>
27
QVariant timeSince( QDateTime early, QDateTime late )
29
Q_ASSERT( early.isValid() );
30
Q_ASSERT( late.isValid() );
32
int secsSince = early.secsTo( late );
35
return SafeString( "0 minutes" );
38
QStringList singularNames;
39
singularNames << "year" << "month" << "week" << "day" << "hour" << "minute";
40
QStringList pluralNames;
41
pluralNames << "years" << "months" << "weeks" << "days" << "hours" << "minutes";
44
seconds << ( 60 * 60 * 24 * 365 ) // year
45
<< ( 60 * 60 * 24 * 30 ) // month
46
<< ( 60 * 60 * 24 * 7 ) // week
47
<< ( 60 * 60 * 24 ) // day
48
<< ( 60 * 60 ) // hour
51
int count = secsSince;
53
while ( i < seconds.size() ) {
54
count = ( int )( secsSince / seconds.at( i ) );
62
firstChunk.append( QString( "%1 %2" ).arg( count ).arg( pluralNames.at( i - 1 ) ) );
64
firstChunk.append( QString( "%1 %2" ).arg( count ).arg( singularNames.at( i - 1 ) ) );
66
if ( seconds.size() > i ) {
67
int count2 = ( secsSince - ( seconds.at( i - 1 ) * count ) ) / seconds.at( i );
70
firstChunk.append( QString( ", %1 %2" ).arg( count2 ).arg( pluralNames.at( i ) ) );
72
firstChunk.append( QString( ", %1 %2" ).arg( count2 ).arg( singularNames.at( i ) ) );
78
QVariant timeUntil( QDateTime dt, QDateTime now = QDateTime() )
81
now = QDateTime::currentDateTime();
83
return timeSince( now, dt );
86
QVariant DateFilter::doFilter( const QVariant& input, const QVariant &argument, bool autoescape ) const
88
Q_UNUSED( autoescape )
89
QDateTime d = QDateTime::fromString( getSafeString( input ), "yyyy-MM-ddThh:mm:ss" );
91
SafeString argString = getSafeString( argument );
93
if ( !argString.get().isEmpty() )
94
return d.toString( argString );
96
return d.toString( "MMM. d, yyyy" );
99
QVariant TimeFilter::doFilter( const QVariant& input, const QVariant &argument, bool autoescape ) const
101
Q_UNUSED( autoescape )
102
SafeString argString = getSafeString( argument );
103
return QDateTime::fromString( getSafeString( input ), "yyyy-MM-ddThh:mm:ss" ).toString( argString );
106
QVariant TimeSinceFilter::doFilter( const QVariant& input, const QVariant &argument, bool autoescape ) const
108
Q_UNUSED( autoescape )
110
if ( argument.type() != QVariant::DateTime )
111
late = QDateTime::currentDateTime();
113
late = argument.toDateTime();
115
return timeSince( input.toDateTime(), late );
118
QVariant TimeUntilFilter::doFilter( const QVariant& input, const QVariant &argument, bool autoescape ) const
120
Q_UNUSED( autoescape )
122
if ( argument.type() != QVariant::DateTime )
123
early = QDateTime::currentDateTime();
125
early = argument.toDateTime();
127
return timeSince( early, input.toDateTime() );