OSDN Git Service

Update version number to 1.2.1.0
[tortoisegit/TortoiseGitJp.git] / src / TortoiseGitBlame / TortoiseGitBlameAppUtils.cpp
index 752c1e7..93d49be 100644 (file)
 // 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 "TortoiseProc.h"\r
-//#include "PathUtils.h"\r
 #include "TortoiseGitBlameAppUtils.h"\r
-//#include "GitProperties.h"\r
-//#include "StringUtils.h"\r
-//#include "MessageBox.h"\r
 #include "Registry.h"\r
-//#include "TGitPath.h"\r
-//include "Git.h"\r
-//#include "RepositoryBrowser.h"\r
-//#include "BrowseFolder.h"\r
-//#include "UnicodeUtils.h"\r
-//#include "ExportDlg.h"\r
-//#include "ProgressDlg.h"\r
-//#include "GitAdminDir.h"\r
-//#include "ProgressDlg.h"\r
-//#include "BrowseFolder.h"\r
-//#include "DirFileEnum.h"\r
-//#include "MessageBox.h"\r
-//#include "GitStatus.h"\r
-//#include "CreateBranchTagDlg.h"\r
-//#include "GitSwitchDlg.h"\r
-//#include "ResetDlg.h"\r
-//#include "commctrl.h"\r
 \r
 CAppUtils::CAppUtils(void)\r
 {\r
@@ -54,45 +33,139 @@ 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
+       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 = ExpandRelativeTime( (int)ts.GetTotalDays()/365, IDS_YEAR_AGO, IDS_YEARS_AGO );\r
+       }\r
+       //Months\r
+       if(fabs(ts.GetTotalDays()) >= 60)\r
+       {\r
+               answer = ExpandRelativeTime( (int)ts.GetTotalDays()/30, IDS_MONTH_AGO, IDS_MONTHS_AGO );\r
+               return answer;\r
+       }\r
+       //Weeks\r
+       if(fabs(ts.GetTotalDays()) >= 14)\r
+       {\r
+               answer = ExpandRelativeTime( (int)ts.GetTotalDays()/7, IDS_WEEK_AGO, IDS_WEEKS_AGO );\r
+               return answer;\r
+       }\r
+       //Days\r
+       if(fabs(ts.GetTotalDays()) >= 2)\r
+       {\r
+               answer = ExpandRelativeTime( (int)ts.GetTotalDays(), IDS_DAY_AGO, IDS_DAYS_AGO );\r
+               return answer;\r
+       }\r
+       //hours\r
+       if(fabs(ts.GetTotalHours()) >= 2)\r
+       {\r
+               answer = ExpandRelativeTime( (int)ts.GetTotalHours(), IDS_HOUR_AGO, IDS_HOURS_AGO );\r
+               return answer;\r
+       }\r
+       //minutes\r
+       if(fabs(ts.GetTotalMinutes()) >= 2)\r
+       {\r
+               answer = ExpandRelativeTime( (int)ts.GetTotalMinutes(), IDS_MINUTE_AGO, IDS_MINUTES_AGO );\r
+               return answer;\r
+       }\r
+       //seconds\r
+               answer = ExpandRelativeTime( (int)ts.GetTotalSeconds(), IDS_SECOND_AGO, IDS_SECONDS_AGO );\r
+    return answer;\r
+}\r
+\r
+/** \r
+ * Passed a value and two resource string ids\r
+ * if count is 1 then FormatString is called with format_1 and the value\r
+ * otherwise format_2 is used\r
+ * the formatted string is returned\r
+*/\r
+CString CAppUtils::ExpandRelativeTime( int count, UINT format_1, UINT format_n )\r
+{\r
+       CString answer;\r
+       if ( count == 1 )\r
+       {\r
+               answer.FormatMessage( format_1, count );\r
+       }\r
+       else\r
+       {\r
+               answer.FormatMessage( format_n, count );\r
+       }\r
+       return answer;\r
+}\r
+\r