36 #include <config-kmime.h>
40 #include <QtCore/QTextStream>
44 #include <kcalendarsystem.h>
46 using namespace KMime;
49 int DateFormatter::mDaylight = -1;
52 : mFormat( ftype ), mTodayOneSecondBeforeMidnight( 0 )
71 bool shortFormat,
bool includeSecs )
const
78 return localized( t, shortFormat, includeSecs, lang );
97 bool shortFormat,
bool includeSecs )
const
109 ret = tmp.toString( QLatin1String(
"ddd, dd MMM yyyy hh:mm:ss " ) );
110 ret += QLatin1String(
zone( t ) );
117 if ( mCustomFormat.isEmpty() ) {
121 int z = mCustomFormat.indexOf( QLatin1Char(
'Z' ) );
123 QString ret = mCustomFormat;
127 ret.replace( z, 1, QLatin1String(
zone( t ) ) );
130 ret = d.toString( ret );
143 return mCustomFormat;
148 #if defined(HAVE_TIMEZONE) || defined(HAVE_TM_GMTOFF)
149 struct tm *local = localtime( &t );
152 #if defined(HAVE_TIMEZONE)
155 int secs = abs( timezone );
156 int neg = ( timezone > 0 ) ? 1 : 0;
157 int hours = secs / 3600;
158 int mins = ( secs - hours * 3600 ) / 60;
161 if ( local->tm_isdst > 0 ) {
172 #elif defined(HAVE_TM_GMTOFF)
174 int secs = abs( local->tm_gmtoff );
175 int neg = ( local->tm_gmtoff < 0 ) ? 1 : 0;
176 int hours = secs / 3600;
177 int mins = ( secs - hours * 3600 ) / 60;
179 if ( local->tm_isdst > 0 ) {
187 QDateTime d1 = QDateTime::fromString( QString::fromLatin1( asctime( gmtime( &t ) ) ) );
188 QDateTime d2 = QDateTime::fromString( QString::fromLatin1( asctime( localtime( &t ) ) ) );
189 int secs = d1.secsTo( d2 );
190 int neg = ( secs < 0 ) ? 1 : 0;
192 int hours = secs / 3600;
193 int mins = ( secs - hours * 3600 ) / 60;
199 QTextStream s( &ret, QIODevice::WriteOnly );
200 s << ( neg ?
'-' :
'+' )
201 << qSetFieldWidth( 2 ) << qSetPadChar( QLatin1Char(
'0' ) ) << right << hours << mins;
209 QDateTime epoch( QDate( 1970, 1, 1 ), QTime( 00, 00, 00 ) );
213 QDateTime d1 = QDateTime::fromString( QLatin1String( asctime( gmtime( &t ) ) ) );
214 QDateTime d2 = QDateTime::fromString( QLatin1String( asctime( localtime( &t ) ) ) );
215 time_t drf = epoch.secsTo( dt ) - d1.secsTo( d2 );
222 KLocale *locale = KGlobal::locale();
225 return i18nc(
"invalid time specified",
"unknown" );
228 if ( mTodayOneSecondBeforeMidnight < time( 0 ) ) {
230 const QDateTime today( QDate::currentDate(), QTime( 23, 59, 59 ) );
231 mTodayOneSecondBeforeMidnight = today.toTime_t();
237 if ( mTodayOneSecondBeforeMidnight >= t ) {
238 const time_t diff = mTodayOneSecondBeforeMidnight - t;
239 if ( diff < 7 * 24 * 60 * 60 ) {
240 if ( diff < 24 * 60 * 60 ) {
241 return i18n(
"Today %1",
242 locale->formatTime( old.time(), true ) );
244 if ( diff < 2 * 24 * 60 * 60 ) {
245 return i18n(
"Yesterday %1",
246 locale->formatTime( old.time(), true ) );
248 for (
int i = 3; i < 8; i++ ) {
249 if ( diff < i * 24 * 60 * 60 ) {
250 return i18nc(
"1. weekday, 2. time",
"%1 %2" ,
251 locale->calendar()->weekDayName( old.date() ) ,
252 locale->formatTime( old.time(), true ) );
258 return locale->formatDateTime( old );
262 const QString &lang )
const
266 KLocale *locale = KGlobal::locale();
270 if ( !lang.isEmpty() ) {
271 locale =
new KLocale( lang, lang, lang );
272 ret = locale->formatDateTime( tmp, ( shortFormat ? KLocale::ShortDate : KLocale::LongDate ), includeSecs );
275 ret = locale->formatDateTime( tmp, ( shortFormat ? KLocale::ShortDate : KLocale::LongDate ), includeSecs );
283 return QString::fromLatin1( ctime( &t ) ).trimmed();
289 strftime( cstr, 63,
"%Y-%m-%d %H:%M:%S", localtime( &t ) );
290 return QLatin1String( cstr );
295 mTodayOneSecondBeforeMidnight = 0;
299 const QString &data,
bool shortFormat,
306 return f.
dateString( t, data, shortFormat, includeSecs );
310 bool shortFormat,
bool includeSecs )
316 return f.
dateString( time( 0 ), data, shortFormat, includeSecs );
321 if ( mDaylight == -1 ) {
322 time_t ntime = time( 0 );
323 struct tm *local = localtime( &ntime );
324 if ( local->tm_isdst > 0 ) {
331 }
else if ( mDaylight != 0 ) {