Use correct length for buffer
[TortoiseGit.git] / src / TortoiseProc / LoglistUtils.cpp
blobaf71f05811aef1c655d789ac331dfc5782a253fc
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2008-2012, 2014, 2016 - TortoiseGit
4 // Copyright (C) 2003-2008 - TortoiseSVN
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software Foundation,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "stdafx.h"
21 #include <math.h>
22 #include "..\Resources\LoglistCommonResource.h"
23 #include "LoglistUtils.h"
24 #include "registry.h"
26 CLoglistUtils::CLoglistUtils(void)
30 CLoglistUtils::~CLoglistUtils(void)
34 /**
35 * FUNCTION : FormatDateAndTime
36 * DESCRIPTION : Generates a displayable string from a CTime object in
37 * system short or long format or as a relative value
38 * cTime - the time
39 * option - DATE_SHORTDATE or DATE_LONGDATE
40 * bIncluedeTime - whether to show time as well as date
41 * bRelative - if true then relative time is shown if reasonable
42 * If HKCU\Software\TortoiseGit\UseSystemLocaleForDates is 0 then use fixed format
43 * rather than locale
44 * RETURN : CString containing date/time
46 CString CLoglistUtils::FormatDateAndTime(const CTime& cTime, DWORD option, bool bIncludeTime /*=true*/, bool bRelative /*=false*/)
48 if (bRelative)
50 return ToRelativeTimeString(cTime);
52 else
54 static bool useSystemLocales = CRegDWORD(L"Software\\TortoiseGit\\UseSystemLocaleForDates", TRUE) != FALSE;
55 // should we use the locale settings for formatting the date/time?
56 if (useSystemLocales)
58 // yes
59 SYSTEMTIME sysTime;
60 cTime.GetAsSystemTime(sysTime);
62 TCHAR buf[100] = { 0 };
64 GetDateFormat(LOCALE_USER_DEFAULT, option, &sysTime, nullptr, buf, _countof(buf) - 1);
65 CString datetime = buf;
66 if (bIncludeTime)
68 datetime += _T(" ");
69 GetTimeFormat(LOCALE_USER_DEFAULT, 0, &sysTime, nullptr, buf, _countof(buf) - 1);
70 datetime += buf;
72 return datetime;
74 else
76 // no, so fixed format
77 if (bIncludeTime)
78 return cTime.Format(_T("%Y-%m-%d %H:%M:%S"));
79 else
80 return cTime.Format(_T("%Y-%m-%d"));
85 /**
86 * Converts a given time to a relative display string (relative to current time)
87 * Given time must be in local timezone
89 CString CLoglistUtils::ToRelativeTimeString(CTime time)
91 // convert to COleDateTime
92 SYSTEMTIME sysTime;
93 time.GetAsSystemTime(sysTime);
94 COleDateTime oleTime(sysTime);
95 return ToRelativeTimeString(oleTime, COleDateTime::GetCurrentTime());
98 /**
99 * Generates a display string showing the relative time between the two given times as COleDateTimes
101 CString CLoglistUtils::ToRelativeTimeString(COleDateTime time,COleDateTime RelativeTo)
103 COleDateTimeSpan ts = RelativeTo - time;
105 //years
106 if(fabs(ts.GetTotalDays()) >= 3 * 365)
107 return ExpandRelativeTime((int)ts.GetTotalDays()/365, IDS_YEAR_AGO, IDS_YEARS_AGO);
109 //Months
110 if(fabs(ts.GetTotalDays()) >= 60)
111 return ExpandRelativeTime((int)ts.GetTotalDays()/30, IDS_MONTH_AGO, IDS_MONTHS_AGO);
113 //Weeks
114 if(fabs(ts.GetTotalDays()) >= 14)
115 return ExpandRelativeTime((int)ts.GetTotalDays()/7, IDS_WEEK_AGO, IDS_WEEKS_AGO);
117 //Days
118 if(fabs(ts.GetTotalDays()) >= 2)
119 return ExpandRelativeTime((int)ts.GetTotalDays(), IDS_DAY_AGO, IDS_DAYS_AGO);
121 //hours
122 if(fabs(ts.GetTotalHours()) >= 2)
123 return ExpandRelativeTime((int)ts.GetTotalHours(), IDS_HOUR_AGO, IDS_HOURS_AGO);
125 //minutes
126 if(fabs(ts.GetTotalMinutes()) >= 2)
127 return ExpandRelativeTime((int)ts.GetTotalMinutes(), IDS_MINUTE_AGO, IDS_MINUTES_AGO);
129 //seconds
130 return ExpandRelativeTime((int)ts.GetTotalSeconds(), IDS_SECOND_AGO, IDS_SECONDS_AGO);
134 * Passed a value and two resource string ids
135 * if count is 1 then FormatString is called with format_1 and the value
136 * otherwise format_2 is used
137 * the formatted string is returned
139 CString CLoglistUtils::ExpandRelativeTime(int count, UINT format_1, UINT format_n)
141 CString answer;
142 if (count == 1)
143 answer.FormatMessage(format_1, count);
144 else
145 answer.FormatMessage(format_n, count);
147 return answer;