[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-core-devel
Subject:    [PATCH] Extend KDateTime with a fancyString()
From:       Tom Albers <tomalbers () kde ! nl>
Date:       2007-04-05 17:58:59
Message-ID: 2190889.zV8NSAgOLR () kde ! nl
[Download RAW message or body]

Hi, 

I extracted the fancy date handling from kmime's date formatter and added it to \
KDateTime. I think it belongs to this class and not to kmime.

I would appreciate if someone looked if it's ok, so I can commit. 

gr.

Toma


["KDateTime-Fancy.diff" (text/x-diff)]

Index: kdecore/date/kdatetime.cpp
===================================================================
--- kdecore/date/kdatetime.cpp	(revision 650460)
+++ kdecore/date/kdatetime.cpp	(working copy)
@@ -1364,6 +1364,53 @@
     return d->toUtc() < other.d->toUtc();
 }
 
+QString KDateTime::fancyString() const
+{
+    if (!isValid())
+        return QString();
+
+    KLocale *locale = KGlobal::locale();
+
+    const time_t currentTime = QDateTime::currentDateTime().toTime_t();
+    
+    QDateTime dateTime;
+    dateTime.setTime_t( currentTime );
+
+    const time_t toConvert = toTime_t();
+
+    // not more than an hour in the future
+    if ( currentTime + 60 * 60 >= toConvert ) {
+         time_t diff = currentTime - toConvert;
+
+         if ( diff < 24 * 60 * 60 ) {
+              if ( d->dt().date().year() == dateTime.date().year() &&
+                   d->dt().date().dayOfYear() == dateTime.date().dayOfYear() )
+                   return i18n( "Today %1", 
+                                locale->formatTime( d->dt().time(), true ) );
+         }
+
+         if ( diff < 2 * 24 * 60 * 60 ) {
+              QDateTime yesterday( dateTime.addDays( -1 ) );
+              if ( d->dt().date().year() == yesterday.date().year() &&
+                   d->dt().date().dayOfYear() == yesterday.date().dayOfYear() )
+                   return i18n( "Yesterday %1", 
+                                locale->formatTime( d->dt().time(), true) );
+         }
+
+         for ( int i = 3; i < 7; i++ ) {
+              if ( diff < i * 24 * 60 * 60 ) {
+                   QDateTime weekday( dateTime.addDays( -i + 1 ) );
+                   if ( d->dt().date().year() == weekday.date().year() &&
+                        d->dt().date().dayOfYear() == weekday.date().dayOfYear() )
+                        return i18nc( "1. weekday, 2. time", "%1 %2" ,
+                             locale->calendar()->weekDayName( d->dt().date() ) ,
+                             locale->formatTime( d->dt().time(), true) );
+              }
+         }
+    }
+    return locale->formatDateTime( d->dt() );
+}
+
 QString KDateTime::toString(const QString &format) const
 {
     if (!isValid())
Index: kdecore/date/kdatetime.h
===================================================================
--- kdecore/date/kdatetime.h	(revision 650460)
+++ kdecore/date/kdatetime.h	(working copy)
@@ -1132,6 +1132,13 @@
      */
     static KDateTime currentDateTime(const Spec &spec);
 
+    /*
+     * Returns the date/tima as a fancy string. This means that it returns,
+     * things like 'Today 11:22' or 'Wednesday 22:33' for recent dates and the 
+     * normal date according to the users settings for older dates.
+     */
+    QString fancyString() const;
+
     /**
      * Returns the date/time as a string. The @p format parameter determines the
      * format of the result string. The @p format codes used for the date and time


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic