summaryrefslogtreecommitdiff
path: root/kmime/kmime_dateformatter.cpp
blob: c9a471f8f9cd5cacb8489bd71d877dc1adeec40b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
/*
  kmime_dateformatter.cpp

  KMime, the KDE Internet mail/usenet news message library.
  Copyright (c) 2001 the KMime authors.
  See file AUTHORS for details

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Library General Public
  License as published by the Free Software Foundation; either
  version 2 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Library General Public License for more details.

  You should have received a copy of the GNU Library General Public License
  along with this library; see the file COPYING.LIB.  If not, write to
  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  Boston, MA 02110-1301, USA.
*/
/**
  @file
  This file is part of the API for handling @ref MIME data and
  defines the DateFormatter class.

  @brief
  Defines the DateFormatter class.

  @authors the KMime authors (see AUTHORS file)
*/

#include "kmime_dateformatter.h"

#include <config-kmime.h>

#include <stdlib.h> // for abs()

#include <QtCore/QTextStream>

#include <kglobal.h>
#include <klocale.h>
#include <kcalendarsystem.h>

using namespace KMime;

//@cond PRIVATE
int DateFormatter::mDaylight = -1;
//@endcond
DateFormatter::DateFormatter( FormatType ftype )
  : mFormat( ftype ), mTodayOneSecondBeforeMidnight( 0 )
{
}

DateFormatter::~DateFormatter()
{
}

DateFormatter::FormatType DateFormatter::format() const
{
  return mFormat;
}

void DateFormatter::setFormat( FormatType ftype )
{
  mFormat = ftype;
}

QString DateFormatter::dateString( time_t t , const QString &lang ,
                                   bool shortFormat, bool includeSecs ) const
{
  switch ( mFormat ) {
  case Fancy:
    return fancy( t );
    break;
  case Localized:
    return localized( t, shortFormat, includeSecs, lang );
    break;
  case CTime:
    return cTime( t );
    break;
  case Iso:
    return isoDate( t );
    break;
  case Rfc:
    return rfc2822( t );
    break;
  case Custom:
    return custom( t );
    break;
  }
  return QString();
}

QString DateFormatter::dateString( const QDateTime &dt, const QString &lang,
                                   bool shortFormat, bool includeSecs ) const
{
  return dateString( qdateToTimeT( dt ), lang, shortFormat, includeSecs );
}

QString DateFormatter::rfc2822( time_t t ) const
{
  QDateTime tmp;
  QString ret;

  tmp.setTime_t( t );

  ret = tmp.toString( QLatin1String( "ddd, dd MMM yyyy hh:mm:ss " ) );
  ret += QLatin1String( zone( t ) );

  return ret;
}

QString DateFormatter::custom( time_t t ) const
{
  if ( mCustomFormat.isEmpty() ) {
    return QString();
  }

  int z = mCustomFormat.indexOf( QLatin1Char( 'Z' ) );
  QDateTime d;
  QString ret = mCustomFormat;

  d.setTime_t( t );
  if ( z != -1 ) {
    ret.replace( z, 1, QLatin1String( zone( t ) ) );
  }

  ret = d.toString( ret );

  return ret;
}

void DateFormatter::setCustomFormat( const QString &format )
{
  mCustomFormat = format;
  mFormat = Custom;
}

QString DateFormatter::customFormat() const
{
  return mCustomFormat;
}

QByteArray DateFormatter::zone( time_t t ) const
{
#if defined(HAVE_TIMEZONE) || defined(HAVE_TM_GMTOFF)
  struct tm *local = localtime( &t );
#endif

#if defined(HAVE_TIMEZONE)

  //hmm, could make hours & mins static
  int secs = abs( timezone );
  int neg  = (timezone > 0) ? 1 : 0;
  int hours = secs / 3600;
  int mins  = (secs - hours*3600) / 60;

  // adjust to daylight
  if ( local->tm_isdst > 0 ) {
    mDaylight = 1;
    if ( neg ) {
      --hours;
    } else {
      ++hours;
    }
  } else {
    mDaylight = 0;
  }

#elif defined(HAVE_TM_GMTOFF)

  int secs = abs( local->tm_gmtoff );
  int neg  = (local->tm_gmtoff < 0) ? 1 : 0;
  int hours = secs / 3600;
  int mins  = (secs - hours * 3600) / 60;

  if ( local->tm_isdst > 0 ) {
    mDaylight = 1;
  } else {
    mDaylight = 0;
  }

#else

  QDateTime d1 = QDateTime::fromString( QString::fromAscii( asctime( gmtime( &t ) ) ) );
  QDateTime d2 = QDateTime::fromString( QString::fromAscii( asctime( localtime( &t ) ) ) );
  int secs = d1.secsTo( d2 );
  int neg = ( secs < 0 ) ? 1 : 0;
  secs = abs( secs );
  int hours = secs / 3600;
  int mins  = (secs - hours * 3600) / 60;
  // daylight should be already taken care of here

#endif /* HAVE_TIMEZONE */

  QByteArray ret;
  QTextStream s( &ret, QIODevice::WriteOnly );
  s << ( neg ? '-' : '+' )
    << qSetFieldWidth( 2 ) << qSetPadChar( QLatin1Char( '0' ) ) << right << hours << mins;
  //old code: ret.sprintf( "%c%.2d%.2d", (neg) ? '-' : '+', hours, mins );

  return ret;
}

time_t DateFormatter::qdateToTimeT( const QDateTime &dt ) const
{
  QDateTime epoch( QDate( 1970, 1, 1 ), QTime( 00, 00, 00 ) );
  time_t t;
  time( &t );

  QDateTime d1 = QDateTime::fromString( QLatin1String( asctime( gmtime( &t ) ) ) );
  QDateTime d2 = QDateTime::fromString( QLatin1String( asctime( localtime( &t ) ) ) );
  time_t drf = epoch.secsTo( dt ) - d1.secsTo( d2 );

  return drf;
}

QString DateFormatter::fancy( time_t t ) const
{
  KLocale *locale = KGlobal::locale();

  if ( t <= 0 ) {
    return i18nc( "invalid time specified", "unknown" );
  }

  if ( mTodayOneSecondBeforeMidnight < time( 0 ) ) {
    // determine time_t value of today 23:59:59
    const QDateTime today( QDate::currentDate(), QTime( 23, 59, 59 ) );
    mTodayOneSecondBeforeMidnight = today.toTime_t();
  }

  QDateTime old;
  old.setTime_t( t );

  if ( mTodayOneSecondBeforeMidnight >= t ) {
    const time_t diff = mTodayOneSecondBeforeMidnight - t;
    if ( diff < 7 * 24 * 60 * 60 ) {
      if ( diff < 24 * 60 * 60 ) {
        return i18n( "Today %1",
                     locale->formatTime( old.time(), true ) );
      }
      if ( diff < 2 * 24 * 60 * 60 ) {
        return i18n( "Yesterday %1",
                     locale->formatTime( old.time(), true ) );
      }
      for ( int i = 3; i < 8; i++ ) {
        if ( diff < i * 24 * 60 * 60 ) {
          return i18nc( "1. weekday, 2. time", "%1 %2" ,
                        locale->calendar()->weekDayName( old.date() ) ,
                        locale->formatTime( old.time(), true ) );
        }
      }
    }
  }

  return locale->formatDateTime( old );
}

QString DateFormatter::localized( time_t t, bool shortFormat, bool includeSecs,
                                  const QString &lang ) const
{
  QDateTime tmp;
  QString ret;
  KLocale *locale = KGlobal::locale();

  tmp.setTime_t( t );

  if ( !lang.isEmpty() ) {
    locale = new KLocale( lang, lang, lang);
    ret = locale->formatDateTime( tmp, (shortFormat ? KLocale::ShortDate : KLocale::LongDate), includeSecs );
    delete locale;
  } else {
    ret = locale->formatDateTime( tmp, (shortFormat ? KLocale::ShortDate : KLocale::LongDate), includeSecs );
  }

  return ret;
}

QString DateFormatter::cTime( time_t t ) const
{
  return QString::fromLatin1( ctime(  &t ) ).trimmed();
}

QString DateFormatter::isoDate( time_t t ) const
{
  char cstr[64];
  strftime( cstr, 63, "%Y-%m-%d %H:%M:%S", localtime( &t ) );
  return QLatin1String( cstr );
}

void DateFormatter::reset()
{
  mTodayOneSecondBeforeMidnight = 0;
}

QString DateFormatter::formatDate( FormatType ftype, time_t t,
                                   const QString &data, bool shortFormat,
                                   bool includeSecs )
{
  DateFormatter f( ftype );
  if ( ftype == Custom ) {
    f.setCustomFormat( data );
  }
  return f.dateString( t, data, shortFormat, includeSecs );
}

QString DateFormatter::formatCurrentDate( FormatType ftype, const QString &data,
                                          bool shortFormat, bool includeSecs )
{
  DateFormatter f( ftype );
  if ( ftype == Custom ) {
    f.setCustomFormat( data );
  }
  return f.dateString( time( 0 ), data, shortFormat, includeSecs );
}

bool DateFormatter::isDaylight()
{
  if ( mDaylight == -1 ) {
    time_t ntime = time( 0 );
    struct tm *local = localtime( &ntime );
    if ( local->tm_isdst > 0 ) {
      mDaylight = 1;
      return true;
    } else {
      mDaylight = 0;
      return false;
    }
  } else if ( mDaylight != 0 ) {
    return true;
  } else {
    return false;
  }
}