WIP Update lib to 2.37

This commit is contained in:
2017-10-20 20:02:52 +02:00
committed by BONNEVILLE Geoffroy
parent 9de9ae54da
commit d5b7845242
105 changed files with 9829 additions and 2410 deletions

View File

@@ -38,20 +38,43 @@ namespace ModernKeePassLib.Utility
/// </summary>
public const int PwTimeLength = 7;
public static readonly DateTime SafeMinValueUtc = new DateTime(
DateTime.MinValue.Ticks + TimeSpan.TicksPerDay, DateTimeKind.Utc);
public static readonly DateTime SafeMaxValueUtc = new DateTime(
DateTime.MaxValue.Ticks - TimeSpan.TicksPerDay, DateTimeKind.Utc);
#if !KeePassLibSD
private static string m_strDtfStd = null;
private static string m_strDtfDate = null;
#endif
// private static long m_lTicks2PowLess1s = 0;
private static DateTime? m_odtUnixRoot = null;
public static DateTime UnixRoot
{
get
{
if(m_odtUnixRoot.HasValue) return m_odtUnixRoot.Value;
DateTime dtRoot = new DateTime(1970, 1, 1, 0, 0, 0, 0,
DateTimeKind.Utc);
m_odtUnixRoot = dtRoot;
return dtRoot;
}
}
/// <summary>
/// Pack a <c>DateTime</c> object into 5 bytes. Layout: 2 zero bits,
/// year 12 bits, month 4 bits, day 5 bits, hour 5 bits, minute 6
/// bits, second 6 bits.
/// </summary>
/// <param name="dt"></param>
/// <returns></returns>
[Obsolete]
public static byte[] PackTime(DateTime dt)
{
dt = ToLocal(dt, true);
byte[] pb = new byte[5];
// Pack time to 5 byte structure:
@@ -73,6 +96,7 @@ namespace ModernKeePassLib.Utility
/// </summary>
/// <param name="pb">Packed time, 5 bytes.</param>
/// <returns>Unpacked <c>DateTime</c> object.</returns>
[Obsolete]
public static DateTime UnpackTime(byte[] pb)
{
Debug.Assert((pb != null) && (pb.Length == 5));
@@ -89,7 +113,8 @@ namespace ModernKeePassLib.Utility
int nMinute = ((n4 & 0x0000000F) << 2) | (n5 >> 6);
int nSecond = n5 & 0x0000003F;
return new DateTime(nYear, nMonth, nDay, nHour, nMinute, nSecond);
return (new DateTime(nYear, nMonth, nDay, nHour, nMinute,
nSecond, DateTimeKind.Local)).ToUniversalTime();
}
/// <summary>
@@ -97,10 +122,13 @@ namespace ModernKeePassLib.Utility
/// </summary>
/// <param name="dt">Object to be encoded.</param>
/// <returns>Packed time, 7 bytes (<c>PW_TIME</c>).</returns>
[Obsolete]
public static byte[] PackPwTime(DateTime dt)
{
Debug.Assert(PwTimeLength == 7);
dt = ToLocal(dt, true);
byte[] pb = new byte[7];
pb[0] = (byte)(dt.Year & 0xFF);
pb[1] = (byte)(dt.Year >> 8);
@@ -118,6 +146,7 @@ namespace ModernKeePassLib.Utility
/// </summary>
/// <param name="pb">Packed time, 7 bytes.</param>
/// <returns>Unpacked <c>DateTime</c> object.</returns>
[Obsolete]
public static DateTime UnpackPwTime(byte[] pb)
{
Debug.Assert(PwTimeLength == 7);
@@ -125,8 +154,8 @@ namespace ModernKeePassLib.Utility
Debug.Assert(pb != null); if(pb == null) throw new ArgumentNullException("pb");
Debug.Assert(pb.Length == 7); if(pb.Length != 7) throw new ArgumentException();
return new DateTime(((int)pb[1] << 8) | (int)pb[0], (int)pb[2], (int)pb[3],
(int)pb[4], (int)pb[5], (int)pb[6]);
return (new DateTime(((int)pb[1] << 8) | (int)pb[0], (int)pb[2], (int)pb[3],
(int)pb[4], (int)pb[5], (int)pb[6], DateTimeKind.Local)).ToUniversalTime();
}
/// <summary>
@@ -136,23 +165,32 @@ namespace ModernKeePassLib.Utility
/// <returns>String representing the specified <c>DateTime</c> object.</returns>
public static string ToDisplayString(DateTime dt)
{
return dt.ToString();
return ToLocal(dt, true).ToString();
}
public static string ToDisplayStringDateOnly(DateTime dt)
{
return dt.ToString("d");
return ToLocal(dt, true).ToString("d");
}
public static DateTime FromDisplayString(string strDisplay)
{
DateTime dt;
if(FromDisplayStringEx(strDisplay, out dt)) return dt;
return DateTime.Now;
}
public static bool FromDisplayStringEx(string strDisplay, out DateTime dt)
{
#if KeePassLibSD
try { dt = DateTime.Parse(strDisplay); return dt; }
try { dt = ToLocal(DateTime.Parse(strDisplay), true); return true; }
catch(Exception) { }
#else
if(DateTime.TryParse(strDisplay, out dt)) return dt;
if(DateTime.TryParse(strDisplay, out dt))
{
dt = ToLocal(dt, true);
return true;
}
// For some custom formats specified using the Control Panel,
// DateTime.ToString returns the correct string, but
@@ -160,19 +198,25 @@ namespace ModernKeePassLib.Utility
// https://sourceforge.net/p/keepass/discussion/329221/thread/3a225b29/?limit=25&page=1#c6ae
if((m_strDtfStd == null) || (m_strDtfDate == null))
{
DateTime dtUni = new DateTime(2111, 3, 4, 5, 6, 7);
DateTime dtUni = new DateTime(2111, 3, 4, 5, 6, 7, DateTimeKind.Local);
m_strDtfStd = DeriveCustomFormat(ToDisplayString(dtUni), dtUni);
m_strDtfDate = DeriveCustomFormat(ToDisplayStringDateOnly(dtUni), dtUni);
}
const DateTimeStyles dts = DateTimeStyles.AllowWhiteSpaces;
if(DateTime.TryParseExact(strDisplay, m_strDtfStd, null, dts, out dt))
return dt;
{
dt = ToLocal(dt, true);
return true;
}
if(DateTime.TryParseExact(strDisplay, m_strDtfDate, null, dts, out dt))
return dt;
{
dt = ToLocal(dt, true);
return true;
}
#endif
Debug.Assert(false);
return DateTime.Now;
return false;
}
#if !KeePassLibSD
@@ -262,8 +306,10 @@ namespace ModernKeePassLib.Utility
public static string SerializeUtc(DateTime dt)
{
string str = dt.ToUniversalTime().ToString("s");
if(str.EndsWith("Z") == false) str += "Z";
Debug.Assert(dt.Kind != DateTimeKind.Unspecified);
string str = ToUtc(dt, false).ToString("s");
if(!str.EndsWith("Z")) str += "Z";
return str;
}
@@ -274,37 +320,40 @@ namespace ModernKeePassLib.Utility
if(str.EndsWith("Z")) str = str.Substring(0, str.Length - 1);
bool bResult = StrUtil.TryParseDateTime(str, out dt);
if(bResult) dt = dt.ToLocalTime();
if(bResult) dt = ToUtc(dt, true);
return bResult;
}
private static DateTime? m_dtUnixRoot = null;
public static double SerializeUnix(DateTime dt)
{
return (ToUtc(dt, false) - TimeUtil.UnixRoot).TotalSeconds;
}
public static DateTime ConvertUnixTime(double dtUnix)
{
try
{
if(!m_dtUnixRoot.HasValue)
m_dtUnixRoot = (new DateTime(1970, 1, 1, 0, 0, 0, 0,
DateTimeKind.Utc)).ToLocalTime();
return m_dtUnixRoot.Value.AddSeconds(dtUnix);
}
try { return TimeUtil.UnixRoot.AddSeconds(dtUnix); }
catch(Exception) { Debug.Assert(false); }
return DateTime.Now;
return DateTime.UtcNow;
}
#if !KeePassLibSD
[Obsolete]
public static DateTime? ParseUSTextDate(string strDate)
{
return ParseUSTextDate(strDate, DateTimeKind.Unspecified);
}
private static string[] m_vUSMonths = null;
/// <summary>
/// Parse a US textual date string, like e.g. "January 02, 2012".
/// </summary>
public static DateTime? ParseUSTextDate(string strDate)
public static DateTime? ParseUSTextDate(string strDate, DateTimeKind k)
{
if(strDate == null) { Debug.Assert(false); return null; }
if(m_vUSMonths == null)
m_vUSMonths = new string[]{ "January", "February", "March",
m_vUSMonths = new string[] { "January", "February", "March",
"April", "May", "June", "July", "August", "September",
"October", "November", "December" };
@@ -314,14 +363,14 @@ namespace ModernKeePassLib.Utility
if(str.StartsWith(m_vUSMonths[i], StrUtil.CaseIgnoreCmp))
{
str = str.Substring(m_vUSMonths[i].Length);
string[] v = str.Split(new char[]{ ',', ';' });
string[] v = str.Split(new char[] { ',', ';' });
if((v == null) || (v.Length != 2)) return null;
string strDay = v[0].Trim().TrimStart('0');
int iDay, iYear;
if(int.TryParse(strDay, out iDay) &&
int.TryParse(v[1].Trim(), out iYear))
return new DateTime(iYear, i + 1, iDay);
return new DateTime(iYear, i + 1, iDay, 0, 0, 0, k);
else { Debug.Assert(false); return null; }
}
}
@@ -331,11 +380,13 @@ namespace ModernKeePassLib.Utility
#endif
private static readonly DateTime m_dtInvMin =
new DateTime(2999, 12, 27, 23, 59, 59);
new DateTime(2999, 12, 27, 23, 59, 59, DateTimeKind.Utc);
private static readonly DateTime m_dtInvMax =
new DateTime(2999, 12, 29, 23, 59, 59);
new DateTime(2999, 12, 29, 23, 59, 59, DateTimeKind.Utc);
public static int Compare(DateTime dtA, DateTime dtB, bool bUnkIsPast)
{
Debug.Assert(dtA.Kind == dtB.Kind);
if(bUnkIsPast)
{
// 2999-12-28 23:59:59 in KeePass 1.x means 'unknown';
@@ -368,5 +419,64 @@ namespace ModernKeePassLib.Utility
return Compare(tlA.LastModificationTime, tlB.LastModificationTime,
bUnkIsPast);
}
public static DateTime ToUtc(DateTime dt, bool bUnspecifiedIsUtc)
{
DateTimeKind k = dt.Kind;
if(k == DateTimeKind.Utc) return dt;
if(k == DateTimeKind.Local) return dt.ToUniversalTime();
Debug.Assert(k == DateTimeKind.Unspecified);
if(bUnspecifiedIsUtc)
return new DateTime(dt.Ticks, DateTimeKind.Utc);
return dt.ToUniversalTime(); // Unspecified = local
}
public static DateTime ToLocal(DateTime dt, bool bUnspecifiedIsLocal)
{
DateTimeKind k = dt.Kind;
if(k == DateTimeKind.Local) return dt;
if(k == DateTimeKind.Utc) return dt.ToLocalTime();
Debug.Assert(k == DateTimeKind.Unspecified);
if(bUnspecifiedIsLocal)
return new DateTime(dt.Ticks, DateTimeKind.Local);
return dt.ToLocalTime(); // Unspecified = UTC
}
/* internal static DateTime RoundToMultOf2PowLess1s(DateTime dt)
{
long l2Pow = m_lTicks2PowLess1s;
if(l2Pow == 0)
{
l2Pow = 1;
while(true)
{
l2Pow <<= 1;
if(l2Pow >= TimeSpan.TicksPerSecond) break;
}
l2Pow >>= 1;
m_lTicks2PowLess1s = l2Pow;
Debug.Assert(TimeSpan.TicksPerSecond == 10000000L); // .NET
Debug.Assert(l2Pow == (1L << 23)); // .NET
}
long l = dt.Ticks;
if((l % l2Pow) == 0L) return dt;
// Round down to full second
l /= TimeSpan.TicksPerSecond;
l *= TimeSpan.TicksPerSecond;
// Round up to multiple of l2Pow
long l2PowM1 = l2Pow - 1L;
l = (l + l2PowM1) & ~l2PowM1;
DateTime dtRnd = new DateTime(l, dt.Kind);
Debug.Assert((dtRnd.Ticks % l2Pow) == 0L);
Debug.Assert(dtRnd.ToString("u") == dt.ToString("u"));
return dtRnd;
} */
}
}