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