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

List:       kmail-devel
Subject:    Re: Abstracting date handling.
From:       Zack Rusin <zackrat () att ! net>
Date:       2002-03-31 7:24:07
[Download RAW message or body]

On Saturday 30 March 2002 09:31, Marc Mutz wrote:
> > - it's not commit ready, it introduces klocale dependency on
> > kmime_util.h
>
> That's no problem.

Fixed (although not a problem :) )

> > which is why we probably should wait till the merger with
> > the Aegypten branch is done, besides this is not exactly a tiny
> > patch,
>
> <snip>
>
> Well, you can put the DateFormatter class into libkdenetwork right
> away. Write a test program in libkdenetwork/tests and wait for the
> Merger if you so want before actually using it in KMail. In the
> meantime, you could make KNode use it ;-)

I'll make it work in KNode on Monday. I rewrote almost the whole class 
to work as you suggested, so I'll write a new KMail patch early this 
week ( I want it to use the custom date formatting feature )

> Now on to the design and features:
> - - Please make the now KMime-NS-global functions static members of
> DateFormatter,

Fixed.

> - - use the "mVar" naming convention for data members,

Fixed.

> - - include header files in order of increasing generality (ie.:
> kmime,libkdenetwork,kde,qt,stdlib),

Fixed.

> - - don't use a pointer to QDateTime as member var, use QDateTime
> directly (or a time_t only)

Fixed.

> - - Why do you hold a pointer to a KLocale? I don't see a point in
> using anything than KGlobal::locale().

When replying we used to create a local KLocale and change it's language 
to match the language of the specific message, it's neat, especially if 
you're bilingual. I cached the recently used locale, that was pretty 
stupid, since the KLocale is not exactly small and I was introducing 
run-time construction with that pointer. Now the dependency on the 
KLocale is gone, it's not constructed anywhere in the class anymore, 
but the language can be switch for the localization function if one 
wishes to do so :)

> - - with the above two, you also don't need to disable copying.

Point well taken, but I still don't see why anyone would want to be 
passing this one around.

> - - overload the dateString method to take time_t, as well as
> QDateTime args. - - add a method that returns a rfc2822-formatted
> date as QCString, so we can use it later to construct headers in
> KMime.

Both fixed/implemented.

> - - don't allow access to the individual formats: The usage should be
> something like:
> DateFormatter f( config->readNumEntry("...", DateFormatter::Fancy )
> ); QString dateStr = f.dateString( QDateTime::currentDateTime() ); or
> with the static convenience function
> QString dateStr = DateFormatter::formatDate( DateFormatter::Fancy,
> QDateTime::currentDateTime() );
> or
> QString dateStr = DateFormatter::formatCurrentDate(
> DateFormatter::Fancy );

Fixed.

> If you have fun with this, you can also implement the "customized"
> format, where the user can create his own format (also a wishlist
> item).

Fixed/implemented. Which comes down to the fact that every single item 
from your list has been fixed/implemented.

>
> Sorry for so much criticism :-(

It was awesome. That's exactly what I need before I really get going. I 
was wondering could we put a HACKING file on CVS so that people wanting 
to start coding would at least have easy access to informations about 
coding conventions that we're using? I know that such file would remove 
at least two items from your list above. Oh, could we maybe also decide 
on a consitant indention style and member variable naming scheme. I 
know that people hate having other's style pressured on them, but 
having one consistant style across application is good. 
Going back to the class I think it's now commit ready, I could do it as 
soon as David will give me cvs access, but if anyone wants to do that 
before you're free to do so, just email me to send you the test case 
file.

Zack

-- 
Failure is not an option. (It comes bundled with Windows.)

["xtestdate.diff" (text/x-diff)]

? .deps
? semantic.cache
Index: kmime_util.cpp
===================================================================
RCS file: /home/kdecvs/kde/kdenetwork/libkdenetwork/kmime_util.cpp,v
retrieving revision 1.9
diff -u -3 -p -u -r1.9 kmime_util.cpp
--- kmime_util.cpp	27 Mar 2002 19:02:51 -0000	1.9
+++ kmime_util.cpp	31 Mar 2002 07:43:37 -0000
@@ -514,6 +514,253 @@ void addQuotes(QCString &str, bool force
 }
 
 
+DateFormatter::DateFormatter(FormatType fType)
+  : mFormat( fType ), mCurrentTime( 0 )
+{
 
+}
+  
+DateFormatter::~DateFormatter()
+{/*empty*/}
+  
+DateFormatter::FormatType 
+DateFormatter::getFormat() const
+{
+  return mFormat;
+}
+ 
+void       
+DateFormatter::setFormat( FormatType t )
+{
+  mFormat = t;
+}
+
+QString 
+DateFormatter::dateString( time_t otime ) const
+{
+  switch (mFormat) {
+  case Fancy:
+    return fancy(otime);
+    break;
+  case Localized:
+    return localized(otime);
+    break;
+  case CTime:
+    return cTime(otime);
+    break;
+  case Iso:
+    return isoDate(otime);
+    break;
+  case Custom:
+    return custom(otime);
+    break;
+  }
+  return QString::null;
+}
+
+QString
+DateFormatter::dateString(const QDateTime& dtime) const
+{
+  return DateFormatter::dateString((qdateToTimeT(dtime)));
+}
+
+QCString 
+DateFormatter::rfc2822(time_t otime) const
+{
+  QDateTime tmp;
+  QCString  ret;
+
+  tmp.setTime_t(otime);
+
+  ret = tmp.toString("ddd, dd MMM yyyy hh:mm:ss ").latin1();
+  ret += zone(otime);
+  
+  return ret;
+}
+
+QString
+DateFormatter::custom(time_t t) const
+{
+  if ( mCustomFormat.isNull() )
+    return QString::null;
+  
+  int z = mCustomFormat.find("Z");
+  QDateTime d;
+  QString ret = mCustomFormat;
+  
+  d.setTime_t(t);
+  if ( z != -1 ) {
+    ret.replace(z,1,zone(t));
+  }
+  
+  ret = d.toString(ret);
+  
+  return ret;
+}
+
+void    
+DateFormatter::setCustomFormat(const QString& format)
+{
+  mCustomFormat = format;
+  mFormat = Custom;
+}
+
+QString 
+DateFormatter::getCustomFormat() const
+{
+  return mCustomFormat;
+}
+
+QCString 
+DateFormatter::zone(time_t otime) const
+{
+  QCString ret;
+  QDateTime d1 = QDateTime::fromString( asctime(gmtime(&otime)) );
+  QDateTime d2 = QDateTime::fromString( asctime(localtime(&otime)) );
+  int secs = d1.secsTo(d2);
+  int neg = (secs<0)?1:0;
+  secs = abs(secs);
+  int hours = secs/3600;
+  int mins  = (secs - hours*3600)/60;
+
+  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 otime;
+  time(&otime);
+
+  QDateTime d1 = QDateTime::fromString( asctime(gmtime(&otime)) );
+  QDateTime d2 = QDateTime::fromString( asctime(localtime(&otime)) );
+  time_t drf = epoch.secsTo(dt) - d1.secsTo(d2);
+
+  return drf; 
+}
+
+QString 
+DateFormatter::fancy(time_t otime) const 
+{
+  KLocale *locale = KGlobal::locale();
+  
+  if ( otime <= 0 )
+    return i18n( "unknown" );
+
+  if ( !mCurrentTime ) {
+    time( &mCurrentTime );
+    mDate.setTime_t( mCurrentTime );
+  }
+
+  QDateTime old;
+  old.setTime_t( otime );
+  
+  // not more than an hour in the future
+  if ( mCurrentTime + 60 * 60 >= otime ) {
+    time_t diff = mCurrentTime - otime;
+    
+    if ( diff < 24 * 60 * 60 ) {
+      if ( old.date().year() == mDate.date().year() &&
+	   old.date().dayOfYear() == mDate.date().dayOfYear() )
+	return i18n( "Today %1" ).arg( locale->
+				       formatTime( old.time(), true ) );
+    }
+    if ( diff < 2 * 24 * 60 * 60 ) {
+      QDateTime yesterday( mDate.addDays( -1 ) );
+      if ( old.date().year() == yesterday.date().year() &&
+	   old.date().dayOfYear() == yesterday.date().dayOfYear() )
+	return i18n( "Yesterday %1" ).arg( locale->
+					   formatTime( old.time(), true) );
+    }
+    for ( int i = 3; i < 7; i++ )
+      if ( diff < i * 24 * 60 * 60 ) {
+	QDateTime weekday( mDate.addDays( -i + 1 ) );
+	if ( old.date().year() == weekday.date().year() &&
+	     old.date().dayOfYear() == weekday.date().dayOfYear() )
+	  return i18n( "1. weekday, 2. time", "%1 %2" ).
+	    arg( locale->weekDayName( old.date().dayOfWeek() ) ).
+	    arg( locale->formatTime( old.time(), true) );
+      }
+  }
+  
+  return locale->formatDateTime( old );
+  
+}
+
+QString 
+DateFormatter::localized(time_t otime, bool shortFormat, bool includeSecs, 
+			 const QString& localeLanguage  = QString::null ) const
+{
+  QDateTime tmp;
+  QString ret;
+  KLocale *locale = KGlobal::locale();
+
+  tmp.setTime_t( otime );
+
+
+  if ( !localeLanguage.isNull() ) {
+    QString olang = locale->language();
+    locale->setLanguage( localeLanguage );
+    ret = locale->formatDateTime( tmp, shortFormat, includeSecs );
+    locale->setLanguage( olang );
+  } else {
+    ret = locale->formatDateTime( tmp, shortFormat, includeSecs );
+  }
+  
+  return ret;
+}
+
+QString 
+DateFormatter::cTime(time_t otime) const
+{
+  return QString::fromLatin1( ctime(  &otime ) ).stripWhiteSpace() ;
+}
+
+QString 
+DateFormatter::isoDate(time_t otime) const
+{
+  char cstr[64];
+  strftime(cstr, 63, "%Y-%m-%d %H:%M:%S", localtime(&otime));
+  return QString(cstr);
+}
+
+
+void 
+DateFormatter::reset()
+{
+  mCurrentTime = 0;
+}
+
+QString 
+DateFormatter::formatDate(DateFormatter::FormatType t, time_t otime,
+			  const QString& format = QString::null)
+{
+  DateFormatter f(t);
+  if ( t == DateFormatter::Custom ) {
+    f.setCustomFormat(format);
+  }
+  return f.dateString(otime);
+}
+
+QString
+DateFormatter::formatCurrentDate( DateFormatter::FormatType t,
+				  const QString& format = QString::null )
+{
+  DateFormatter f(t);
+  if ( t == DateFormatter::Custom ) {
+    f.setCustomFormat(format);
+  }
+  return f.dateString(time(0));
+}
+
+QCString 
+DateFormatter::rfc2822FormatDate( time_t t )
+{
+  DateFormatter f;
+  return f.rfc2822( t );
+}
 
 } // namespace KMime
Index: kmime_util.h
===================================================================
RCS file: /home/kdecvs/kde/kdenetwork/libkdenetwork/kmime_util.h,v
retrieving revision 1.4
diff -u -3 -p -u -r1.4 kmime_util.h
--- kmime_util.h	28 Mar 2002 12:48:20 -0000	1.4
+++ kmime_util.h	31 Mar 2002 07:43:38 -0000
@@ -16,9 +16,11 @@
 #ifndef __KMIME_UTIL_H__
 #define __KMIME_UTIL_H__
 
+#include "qdatetime.h"
 #include "qstring.h"
 #include "qcstring.h"
 #include "qvaluelist.h"
+#include "time.h"
 
 typedef QValueList<QCString> QCStringList;
 
@@ -168,8 +170,141 @@ namespace KMime {
   extern void addQuotes(QCString &str, bool forceQuotes);
 
 
-
-
+  /** 
+   * DateFormatter deals with different kinds of date
+   * display formats. The formats supported by the lass include:
+   * <ul>
+   *     <li> fancy "Today 02:08:35"
+   *     <li> ctime "Sun Mar 31 02:08:35 2002"
+   *     <li> localized "2002-03-31 02:08"
+   *     <li> iso  "2002-03-31 02:08:35"
+   *     <li> rfc2822 "Sun, 31 Mar 2002 02:08:35 -0500"
+   *     <li> custom "whatever you like"
+   * </ul>
+   *
+   *
+   * @short class abstracting date formatting 
+   */
+  class DateFormatter {
+  public:
+    enum FormatType {
+      CTime,
+      Localized,
+      Fancy,
+      Iso,
+      Custom
+    };
+    
+    /** 
+     * constructor 
+     * @param fType default format used by the class
+     */
+    DateFormatter(FormatType fType = DateFormatter::Fancy);
+    
+    ~DateFormatter();
+    
+    /**
+     * returns the currently set format
+     */
+    FormatType getFormat() const;
+    /**
+     * sets the currently used format
+     */
+    void setFormat(FormatType t);
+      
+    /**
+     * returns formatted date string in a currently
+     * set format.
+     * @param otime time to format
+     */
+    QString dateString(time_t otime) const;
+    /**
+     * overloaded, does exactly what @ref #dateString does
+     */
+    QString dateString(const QDateTime& dtime) const;    
+
+
+    /**
+     * makes the class use the custom format for 
+     * date to string conversions.
+     * Method accepts the same arguments 
+     * as @ref QDateTime::toString method and adds
+     * "Z" expression which is substituted with the
+     * RFC-822 style numeric timezone (-0500)
+     * @param format the custom format
+     */
+    void    setCustomFormat(const QString& format);
+    QString getCustomFormat() const;
+    
+    /**
+     * returns rfc2822 formatted string
+     * @param otime time to use for formatting
+     */
+    QCString rfc2822(time_t otime) const;
+    /**
+     * resets the internal clock
+     */
+    void reset();
+    
+    //statics
+    /** convenience function @see dateString */
+    static QString  formatDate( DateFormatter::FormatType t, time_t time,
+				const QString& format = QString::null);
+    /** convenience function, same as @ref #formatDate
+     * but returns the current time formatted */
+    static QString  formatCurrentDate( DateFormatter::FormatType t,
+				       const QString& format = QString::null);
+    /** convenience function, same as @ref #rfc2822 */
+    static QCString rfc2822FormatDate( time_t time );
+  protected:
+    /**
+     * returns fancy formatted date string
+     * @param otime time to format
+     * @internal
+     */
+    QString fancy(time_t otime) const ;
+    /**
+     * returns localized formatted date string
+     * @param otime time to format
+     * @param shortFormat 
+     * @param includeSecs
+     * @param localeLanguage language used for formatting
+     * @internal
+     */
+    QString localized(time_t otime, bool shortFormat = true, bool includeSecs = false, 
+		      const QString& localeLanguage=QString::null ) const;
+    /**
+     * returns string as formatted with ctime function
+     * @internal
+     */
+    QString cTime(time_t otime) const;
+    /**
+     * returns a string in the "%Y-%m-%d %H:%M:%S" format
+     * @internal
+     */
+    QString isoDate(time_t otime) const;
+
+    /**
+     * returns date formatted with the earlier
+     * given custom format
+     * @param t time used for formatting
+     * @internal
+     */
+    QString custom(time_t t) const;
+    /**
+     * returns a string identifying the timezone (eg."-0500")
+     * @internal
+     */
+    QCString zone(time_t otime) const;
+
+    time_t qdateToTimeT(const QDateTime& dt) const;
+  private:
+    FormatType 		mFormat;
+    mutable time_t 	mCurrentTime;
+    mutable QDateTime 	mDate;
+    QString 		mCustomFormat;
+  };
+  
 } // namespace KMime
 
 #endif /* __KMIME_UTIL_H__ */
? tests/test_dates
? tests/test_dates.cpp

_______________________________________________
KMail Developers mailing list
kmail@mail.kde.org
http://mail.kde.org/mailman/listinfo/kmail

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

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