OSDN Git Service

Make relative time format available for GitBlame
authorColin Law <colin@clanlaw.org.uk>
Sat, 31 Jan 2009 10:52:19 +0000 (10:52 +0000)
committerFrank Li <lznuaa@gmail.com>
Sun, 1 Feb 2009 08:11:16 +0000 (16:11 +0800)
src/TortoiseGitBlame/TortoiseGitBlameAppUtils.cpp
src/TortoiseGitBlame/TortoiseGitBlameAppUtils.h
src/TortoiseProc/AppUtils.cpp
src/TortoiseProc/AppUtils.h

index 0102526..f1067bf 100644 (file)
@@ -17,6 +17,7 @@
 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\r
 //\r
 #include "StdAfx.h"\r
+#include "math.h"\r
 #include "resource.h"\r
 #include "TortoiseGitBlameAppUtils.h"\r
 #include "Registry.h"\r
@@ -32,45 +33,120 @@ CAppUtils::~CAppUtils(void)
 /**\r
  * FUNCTION    :   FormatDateAndTime\r
  * DESCRIPTION :   Generates a displayable string from a CTime object in\r
- *                 system short or long format dependant on setting of option\r
- *                                as DATE_SHORTDATE or DATE_LONGDATE\r
+ *                 system short or long format  or as a relative value\r
+ *                                cTime - the time\r
+ *                                option - DATE_SHORTDATE or DATE_LONGDATE\r
+ *                                bIncluedeTime - whether to show time as well as date\r
+ *                                bRelative - if true then relative time is shown if reasonable \r
  *                                If HKCU\Software\TortoiseGit\UseSystemLocaleForDates is 0 then use fixed format\r
  *                                rather than locale\r
  * RETURN      :   CString containing date/time\r
  */\r
-CString CAppUtils::FormatDateAndTime( const CTime& cTime, DWORD option, bool bIncludeTime /*=true*/  )\r
+CString CAppUtils::FormatDateAndTime( const CTime& cTime, DWORD option, bool bIncludeTime /*=true*/,\r
+       bool bRelative /*=false*/)\r
 {\r
        CString datetime;\r
-    // should we use the locale settings for formatting the date/time?\r
-       if (CRegDWORD(_T("Software\\TortoiseGit\\UseSystemLocaleForDates"), TRUE))\r
+       if ( bRelative )\r
        {\r
-               // yes\r
-               SYSTEMTIME sysTime;\r
-               cTime.GetAsSystemTime( sysTime );\r
-               \r
-               TCHAR buf[100];\r
-               \r
-               GetDateFormat(LOCALE_USER_DEFAULT, option, &sysTime, NULL, buf, \r
-                       sizeof(buf)/sizeof(TCHAR)-1);\r
-               datetime = buf;\r
-               if ( bIncludeTime )\r
-               {\r
-                       datetime += _T(" ");\r
-                       GetTimeFormat(LOCALE_USER_DEFAULT, 0, &sysTime, NULL, buf, sizeof(buf)/sizeof(TCHAR)-1);\r
-                       datetime += buf;\r
-               }\r
+               datetime = ToRelativeTimeString( cTime );\r
        }\r
        else\r
        {\r
-               // no, so fixed format\r
-               if ( bIncludeTime )\r
+               // should we use the locale settings for formatting the date/time?\r
+               if (CRegDWORD(_T("Software\\TortoiseGit\\UseSystemLocaleForDates"), TRUE))\r
                {\r
-                       datetime = cTime.Format(_T("%Y-%m-%d %H:%M:%S"));\r
+                       // yes\r
+                       SYSTEMTIME sysTime;\r
+                       cTime.GetAsSystemTime( sysTime );\r
+                       \r
+                       TCHAR buf[100];\r
+                       \r
+                       GetDateFormat(LOCALE_USER_DEFAULT, option, &sysTime, NULL, buf, \r
+                               sizeof(buf)/sizeof(TCHAR)-1);\r
+                       datetime = buf;\r
+                       if ( bIncludeTime )\r
+                       {\r
+                               datetime += _T(" ");\r
+                               GetTimeFormat(LOCALE_USER_DEFAULT, 0, &sysTime, NULL, buf, sizeof(buf)/sizeof(TCHAR)-1);\r
+                               datetime += buf;\r
+                       }\r
                }\r
                else\r
                {\r
-                       datetime = cTime.Format(_T("%Y-%m-%d"));\r
+                       // no, so fixed format\r
+                       if ( bIncludeTime )\r
+                       {\r
+                               datetime = cTime.Format(_T("%Y-%m-%d %H:%M:%S"));\r
+                       }\r
+                       else\r
+                       {\r
+                               datetime = cTime.Format(_T("%Y-%m-%d"));\r
+                       }\r
                }\r
        }\r
        return datetime;\r
 }\r
+\r
+/**\r
+ *     Converts a given time to a relative display string (relative to current time)\r
+ *     Given time must be in local timezone\r
+ */\r
+CString CAppUtils::ToRelativeTimeString(CTime time)\r
+{\r
+    CString answer;\r
+       // convert to COleDateTime\r
+       SYSTEMTIME sysTime;\r
+       time.GetAsSystemTime( sysTime );\r
+       COleDateTime oleTime( sysTime );\r
+       answer = ToRelativeTimeString(oleTime, COleDateTime::GetCurrentTime());\r
+       // change this to return answer when happy\r
+       return answer;\r
+}\r
+\r
+/**\r
+ *     Generates a display string showing the relative time between the two given times as COleDateTimes\r
+ */\r
+CString CAppUtils::ToRelativeTimeString(COleDateTime time,COleDateTime RelativeTo)\r
+{\r
+    CString answer;\r
+       COleDateTimeSpan ts = RelativeTo - time;\r
+    //years\r
+       if(fabs(ts.GetTotalDays()) >= 3*365)\r
+    {\r
+               answer .FormatMessage(_T("%1!d! Years ago"), (int)(ts.GetTotalDays()/365));\r
+       }\r
+       //Months\r
+       if(fabs(ts.GetTotalDays()) >= 60)\r
+       {\r
+               answer.FormatMessage( _T("%1!d! Months ago"), (int)(ts.GetTotalDays()/30) );\r
+               return answer;\r
+       }\r
+       //Weeks\r
+       if(fabs(ts.GetTotalDays()) >= 14)\r
+       {\r
+               answer.FormatMessage(_T("%1!d! Weeks ago"), (int)(ts.GetTotalDays()/7) );\r
+               return answer;\r
+       }\r
+       //Days\r
+       if(fabs(ts.GetTotalDays()) >= 2)\r
+       {\r
+               answer.FormatMessage(_T("%1!d! Days ago"), (int)(ts.GetTotalDays()) );\r
+               return answer;\r
+       }\r
+       //hours\r
+       if(fabs(ts.GetTotalHours()) >= 2)\r
+       {\r
+               answer.FormatMessage(_T("%1!d! Hours ago"), (int)(ts.GetTotalHours()) );\r
+               return answer;\r
+       }\r
+       //minutes\r
+       if(fabs(ts.GetTotalMinutes()) >= 2)\r
+       {\r
+               answer.FormatMessage(_T("%1!d! Minutes ago"), (int)(ts.GetTotalMinutes()) );\r
+               return answer;\r
+       }\r
+       //seconds\r
+       answer.FormatMessage(_T("%1!d! Seconds ago"), (int)(ts.GetTotalSeconds()) );\r
+    return answer;\r
+}\r
+\r
index ca38bac..1a49752 100644 (file)
@@ -30,12 +30,27 @@ public:
        /**\r
         * FUNCTION    :   FormatDateAndTime\r
         * DESCRIPTION :   Generates a displayable string from a CTime object in\r
-        *                 system short or long format dependant on setting of option\r
-        *                                 as DATE_SHORTDATE or DATE_LONGDATE. bIncludeTime (optional) includes time.\r
+        *                 system short or long format  or as a relative value\r
+        *                                 cTime - the time\r
+        *                                 option - DATE_SHORTDATE or DATE_LONGDATE\r
+        *                                 bIncluedeTime - whether to show time as well as date\r
+        *                                 bRelative - if true then relative time is shown if reasonable \r
+        *                                 If HKCU\Software\TortoiseGit\UseSystemLocaleForDates is 0 then use fixed format\r
+        *                                 rather than locale\r
         * RETURN      :   CString containing date/time\r
         */\r
-       static CString FormatDateAndTime( const CTime& cTime, DWORD option, bool bIncludeTime=true );\r
+       static CString FormatDateAndTime( const CTime& cTime, DWORD option, bool bIncludeTime=true,\r
+               bool bRelative=false );\r
+       /**\r
+        *      Converts a given time to a relative display string (relative to current time)\r
+        *      Given time must be in local timezone\r
+        */\r
+       static CString ToRelativeTimeString(CTime time);\r
 \r
        \r
 private:\r
+       /**\r
+        *      Generates a display string showing the relative time between the two given times as COleDateTimes\r
+        */\r
+       static CString ToRelativeTimeString(COleDateTime time,COleDateTime RelativeTo);\r
 };\r
index 5c6c335..a45a15c 100644 (file)
@@ -1613,7 +1613,6 @@ CString CAppUtils::FormatDateAndTime( const CTime& cTime, DWORD option, bool bIn
 /**\r
  *     Converts a given time to a relative display string (relative to current time)\r
  *     Given time must be in local timezone\r
- *  If more than a year ago or in the future then normal date/time is shown\r
  */\r
 CString CAppUtils::ToRelativeTimeString(CTime time)\r
 {\r
@@ -1629,8 +1628,6 @@ CString CAppUtils::ToRelativeTimeString(CTime time)
 \r
 /**\r
  *     Generates a display string showing the relative time between the two given times as COleDateTimes\r
- *     time must be earlier than RelativeTo\r
- *  If more than a year ago or time > RelativeTo then an empty string is returned\r
  */\r
 CString CAppUtils::ToRelativeTimeString(COleDateTime time,COleDateTime RelativeTo)\r
 {\r
@@ -1639,7 +1636,7 @@ CString CAppUtils::ToRelativeTimeString(COleDateTime time,COleDateTime RelativeT
     //years\r
        if(fabs(ts.GetTotalDays()) >= 3*365)\r
     {\r
-               answer .FormatMessage(_T("%1!d! Years ago"), (int)(ts.GetTotalDays()/365));\r
+               answer.FormatMessage(_T("%1!d! Years ago"), (int)(ts.GetTotalDays()/365));\r
        }\r
        //Months\r
        if(fabs(ts.GetTotalDays()) >= 60)\r
index 4d9afda..17e34e9 100644 (file)
@@ -164,8 +164,13 @@ public:
        /**\r
         * FUNCTION    :   FormatDateAndTime\r
         * DESCRIPTION :   Generates a displayable string from a CTime object in\r
-        *                 system short or long format dependant on setting of option\r
-        *                                 as DATE_SHORTDATE or DATE_LONGDATE. bIncludeTime (optional) includes time.\r
+        *                 system short or long format  or as a relative value\r
+        *                                 cTime - the time\r
+        *                                 option - DATE_SHORTDATE or DATE_LONGDATE\r
+        *                                 bIncluedeTime - whether to show time as well as date\r
+        *                                 bRelative - if true then relative time is shown if reasonable \r
+        *                                 If HKCU\Software\TortoiseGit\UseSystemLocaleForDates is 0 then use fixed format\r
+        *                                 rather than locale\r
         * RETURN      :   CString containing date/time\r
         */\r
        static CString FormatDateAndTime( const CTime& cTime, DWORD option, bool bIncludeTime=true,\r
@@ -173,7 +178,6 @@ public:
        /**\r
         *      Converts a given time to a relative display string (relative to current time)\r
         *      Given time must be in local timezone\r
-        *  If more than a year ago or in the future then normal date/time is shown\r
         */\r
        static CString ToRelativeTimeString(CTime time);\r
 \r
@@ -183,8 +187,6 @@ private:
        static bool GetMimeType(const CTGitPath& file, CString& mimetype);\r
        /**\r
         *      Generates a display string showing the relative time between the two given times as COleDateTimes\r
-        *      time must be earlier than RelativeTo\r
-        *  If more than a year ago or time > RelativeTo then an empty string is returned\r
         */\r
        static CString ToRelativeTimeString(COleDateTime time,COleDateTime RelativeTo);\r
 };\r