~ubuntu-branches/ubuntu/trusty/grantlee/trusty

« back to all changes in this revision

Viewing changes to defaultfilters/datetime.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Harald Sitter
  • Date: 2010-06-11 23:41:45 UTC
  • Revision ID: james.westby@ubuntu.com-20100611234145-oas7rhdrbwy8j55c
Tags: upstream-0.1.1
ImportĀ upstreamĀ versionĀ 0.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  This file is part of the Grantlee template system.
 
3
 
 
4
  Copyright (c) 2009,2010 Stephen Kelly <steveire@gmail.com>
 
5
 
 
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.
 
10
 
 
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.
 
15
 
 
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/>.
 
18
 
 
19
*/
 
20
 
 
21
#include "datetime.h"
 
22
 
 
23
#include <QtCore/QDateTime>
 
24
 
 
25
#include "util.h"
 
26
 
 
27
QVariant timeSince( QDateTime early, QDateTime late )
 
28
{
 
29
  Q_ASSERT( early.isValid() );
 
30
  Q_ASSERT( late.isValid() );
 
31
 
 
32
  int secsSince = early.secsTo( late );
 
33
 
 
34
  if ( secsSince < 0 )
 
35
    return SafeString( "0 minutes" );
 
36
 
 
37
  // TODO: i18n
 
38
  QStringList singularNames;
 
39
  singularNames << "year" << "month" << "week" << "day" << "hour" << "minute";
 
40
  QStringList pluralNames;
 
41
  pluralNames << "years" << "months" << "weeks" << "days" << "hours" << "minutes";
 
42
 
 
43
  QList<int> seconds;
 
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
 
49
          << ( 60 ); // minute
 
50
 
 
51
  int count = secsSince;
 
52
  int i = 0;
 
53
  while ( i < seconds.size() ) {
 
54
    count = ( int )( secsSince / seconds.at( i ) );
 
55
    ++i;
 
56
    if ( count != 0 )
 
57
      break;
 
58
  }
 
59
  QString firstChunk;
 
60
 
 
61
  if ( count != 1 )
 
62
    firstChunk.append( QString( "%1 %2" ).arg( count ).arg( pluralNames.at( i - 1 ) ) );
 
63
  else {
 
64
    firstChunk.append( QString( "%1 %2" ).arg( count ).arg( singularNames.at( i - 1 ) ) );
 
65
  }
 
66
  if ( seconds.size() > i ) {
 
67
    int count2 = ( secsSince - ( seconds.at( i - 1 ) * count ) ) / seconds.at( i );
 
68
    if ( count2 != 0 ) {
 
69
      if ( count2 > 1 )
 
70
        firstChunk.append( QString( ", %1 %2" ).arg( count2 ).arg( pluralNames.at( i ) ) );
 
71
      else
 
72
        firstChunk.append( QString( ", %1 %2" ).arg( count2 ).arg( singularNames.at( i ) ) );
 
73
    }
 
74
  }
 
75
  return firstChunk;
 
76
}
 
77
 
 
78
QVariant timeUntil( QDateTime dt, QDateTime now = QDateTime() )
 
79
{
 
80
  if ( !now.isValid() )
 
81
    now = QDateTime::currentDateTime();
 
82
 
 
83
  return timeSince( now, dt );
 
84
}
 
85
 
 
86
QVariant DateFilter::doFilter( const QVariant& input, const QVariant &argument, bool autoescape ) const
 
87
{
 
88
  Q_UNUSED( autoescape )
 
89
  QDateTime d = QDateTime::fromString( getSafeString( input ), "yyyy-MM-ddThh:mm:ss" );
 
90
 
 
91
  SafeString argString = getSafeString( argument );
 
92
 
 
93
  if ( !argString.get().isEmpty() )
 
94
    return d.toString( argString );
 
95
 
 
96
  return d.toString( "MMM. d, yyyy" );
 
97
}
 
98
 
 
99
QVariant TimeFilter::doFilter( const QVariant& input, const QVariant &argument, bool autoescape ) const
 
100
{
 
101
  Q_UNUSED( autoescape )
 
102
  SafeString argString = getSafeString( argument );
 
103
  return QDateTime::fromString( getSafeString( input ), "yyyy-MM-ddThh:mm:ss" ).toString( argString );
 
104
}
 
105
 
 
106
QVariant TimeSinceFilter::doFilter( const QVariant& input, const QVariant &argument, bool autoescape ) const
 
107
{
 
108
  Q_UNUSED( autoescape )
 
109
  QDateTime late;
 
110
  if ( argument.type() != QVariant::DateTime )
 
111
    late = QDateTime::currentDateTime();
 
112
  else
 
113
    late = argument.toDateTime();
 
114
 
 
115
  return timeSince( input.toDateTime(), late );
 
116
}
 
117
 
 
118
QVariant TimeUntilFilter::doFilter( const QVariant& input, const QVariant &argument, bool autoescape ) const
 
119
{
 
120
  Q_UNUSED( autoescape )
 
121
  QDateTime early;
 
122
  if ( argument.type() != QVariant::DateTime )
 
123
    early = QDateTime::currentDateTime();
 
124
  else
 
125
    early = argument.toDateTime();
 
126
 
 
127
  return timeSince( early, input.toDateTime() );
 
128
}
 
129