OSDN Git Service

Add SVN DCommit Command
[tortoisegit/TortoiseGitJp.git] / src / TortoiseGitBlame / TortoiseGitBlameAppUtils.cpp
1 // TortoiseGit - a Windows shell extension for easy version control\r
2 \r
3 // Copyright (C) 2003-2008 - TortoiseGit\r
4 \r
5 // This program is free software; you can redistribute it and/or\r
6 // modify it under the terms of the GNU General Public License\r
7 // as published by the Free Software Foundation; either version 2\r
8 // of the License, or (at your option) any later version.\r
9 \r
10 // This program is distributed in the hope that it will be useful,\r
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 // GNU General Public License for more details.\r
14 \r
15 // You should have received a copy of the GNU General Public License\r
16 // along with this program; if not, write to the Free Software Foundation,\r
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\r
18 //\r
19 #include "StdAfx.h"\r
20 #include "math.h"\r
21 #include "resource.h"\r
22 #include "TortoiseGitBlameAppUtils.h"\r
23 #include "Registry.h"\r
24 \r
25 CAppUtils::CAppUtils(void)\r
26 {\r
27 }\r
28 \r
29 CAppUtils::~CAppUtils(void)\r
30 {\r
31 }\r
32 \r
33 /**\r
34  * FUNCTION    :   FormatDateAndTime\r
35  * DESCRIPTION :   Generates a displayable string from a CTime object in\r
36  *                 system short or long format  or as a relative value\r
37  *                                 cTime - the time\r
38  *                                 option - DATE_SHORTDATE or DATE_LONGDATE\r
39  *                                 bIncluedeTime - whether to show time as well as date\r
40  *                                 bRelative - if true then relative time is shown if reasonable \r
41  *                                 If HKCU\Software\TortoiseGit\UseSystemLocaleForDates is 0 then use fixed format\r
42  *                                 rather than locale\r
43  * RETURN      :   CString containing date/time\r
44  */\r
45 CString CAppUtils::FormatDateAndTime( const CTime& cTime, DWORD option, bool bIncludeTime /*=true*/,\r
46         bool bRelative /*=false*/)\r
47 {\r
48         CString datetime;\r
49         if ( bRelative )\r
50         {\r
51                 datetime = ToRelativeTimeString( cTime );\r
52         }\r
53         else\r
54         {\r
55                 // should we use the locale settings for formatting the date/time?\r
56                 if (CRegDWORD(_T("Software\\TortoiseGit\\UseSystemLocaleForDates"), TRUE))\r
57                 {\r
58                         // yes\r
59                         SYSTEMTIME sysTime;\r
60                         cTime.GetAsSystemTime( sysTime );\r
61                         \r
62                         TCHAR buf[100];\r
63                         \r
64                         GetDateFormat(LOCALE_USER_DEFAULT, option, &sysTime, NULL, buf, \r
65                                 sizeof(buf)/sizeof(TCHAR)-1);\r
66                         datetime = buf;\r
67                         if ( bIncludeTime )\r
68                         {\r
69                                 datetime += _T(" ");\r
70                                 GetTimeFormat(LOCALE_USER_DEFAULT, 0, &sysTime, NULL, buf, sizeof(buf)/sizeof(TCHAR)-1);\r
71                                 datetime += buf;\r
72                         }\r
73                 }\r
74                 else\r
75                 {\r
76                         // no, so fixed format\r
77                         if ( bIncludeTime )\r
78                         {\r
79                                 datetime = cTime.Format(_T("%Y-%m-%d %H:%M:%S"));\r
80                         }\r
81                         else\r
82                         {\r
83                                 datetime = cTime.Format(_T("%Y-%m-%d"));\r
84                         }\r
85                 }\r
86         }\r
87         return datetime;\r
88 }\r
89 \r
90 /**\r
91  *      Converts a given time to a relative display string (relative to current time)\r
92  *      Given time must be in local timezone\r
93  */\r
94 CString CAppUtils::ToRelativeTimeString(CTime time)\r
95 {\r
96     CString answer;\r
97         // convert to COleDateTime\r
98         SYSTEMTIME sysTime;\r
99         time.GetAsSystemTime( sysTime );\r
100         COleDateTime oleTime( sysTime );\r
101         answer = ToRelativeTimeString(oleTime, COleDateTime::GetCurrentTime());\r
102         return answer;\r
103 }\r
104 \r
105 /**\r
106  *      Generates a display string showing the relative time between the two given times as COleDateTimes\r
107  */\r
108 CString CAppUtils::ToRelativeTimeString(COleDateTime time,COleDateTime RelativeTo)\r
109 {\r
110     CString answer;\r
111         COleDateTimeSpan ts = RelativeTo - time;\r
112     //years\r
113         if(fabs(ts.GetTotalDays()) >= 3*365)\r
114     {\r
115                 answer = ExpandRelativeTime( (int)ts.GetTotalDays()/365, IDS_YEAR_AGO, IDS_YEARS_AGO );\r
116         }\r
117         //Months\r
118         if(fabs(ts.GetTotalDays()) >= 60)\r
119         {\r
120                 answer = ExpandRelativeTime( (int)ts.GetTotalDays()/30, IDS_MONTH_AGO, IDS_MONTHS_AGO );\r
121                 return answer;\r
122         }\r
123         //Weeks\r
124         if(fabs(ts.GetTotalDays()) >= 14)\r
125         {\r
126                 answer = ExpandRelativeTime( (int)ts.GetTotalDays()/7, IDS_WEEK_AGO, IDS_WEEKS_AGO );\r
127                 return answer;\r
128         }\r
129         //Days\r
130         if(fabs(ts.GetTotalDays()) >= 2)\r
131         {\r
132                 answer = ExpandRelativeTime( (int)ts.GetTotalDays(), IDS_DAY_AGO, IDS_DAYS_AGO );\r
133                 return answer;\r
134         }\r
135         //hours\r
136         if(fabs(ts.GetTotalHours()) >= 2)\r
137         {\r
138                 answer = ExpandRelativeTime( (int)ts.GetTotalHours(), IDS_HOUR_AGO, IDS_HOURS_AGO );\r
139                 return answer;\r
140         }\r
141         //minutes\r
142         if(fabs(ts.GetTotalMinutes()) >= 2)\r
143         {\r
144                 answer = ExpandRelativeTime( (int)ts.GetTotalMinutes(), IDS_MINUTE_AGO, IDS_MINUTES_AGO );\r
145                 return answer;\r
146         }\r
147         //seconds\r
148                 answer = ExpandRelativeTime( (int)ts.GetTotalSeconds(), IDS_SECOND_AGO, IDS_SECONDS_AGO );\r
149     return answer;\r
150 }\r
151 \r
152 /** \r
153  * Passed a value and two resource string ids\r
154  * if count is 1 then FormatString is called with format_1 and the value\r
155  * otherwise format_2 is used\r
156  * the formatted string is returned\r
157 */\r
158 CString CAppUtils::ExpandRelativeTime( int count, UINT format_1, UINT format_n )\r
159 {\r
160         CString answer;\r
161         if ( count == 1 )\r
162         {\r
163                 answer.FormatMessage( format_1, count );\r
164         }\r
165         else\r
166         {\r
167                 answer.FormatMessage( format_n, count );\r
168         }\r
169         return answer;\r
170 }\r
171 \r