mirror of
https://github.com/wismna/ModernKeePass.git
synced 2025-10-04 08:00:16 -04:00
ModernKeePassLib custom PCL version
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
KeePass Password Safe - The Open-Source Password Manager
|
||||
Copyright (C) 2003-2012 Dominik Reichl <dominik.reichl@t-online.de>
|
||||
Copyright (C) 2003-2014 Dominik Reichl <dominik.reichl@t-online.de>
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
@@ -18,8 +18,13 @@
|
||||
*/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Globalization;
|
||||
using System.Diagnostics;
|
||||
|
||||
using ModernKeePassLib.Interfaces;
|
||||
|
||||
namespace ModernKeePassLib.Utility
|
||||
{
|
||||
/// <summary>
|
||||
@@ -33,6 +38,11 @@ namespace ModernKeePassLib.Utility
|
||||
/// </summary>
|
||||
public const int PwTimeLength = 7;
|
||||
|
||||
#if !KeePassLibSD
|
||||
private static string m_strDtfStd = null;
|
||||
private static string m_strDtfDate = null;
|
||||
#endif
|
||||
|
||||
/// <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
|
||||
@@ -138,17 +148,118 @@ namespace ModernKeePassLib.Utility
|
||||
{
|
||||
DateTime dt;
|
||||
|
||||
#if !KeePassLibSD
|
||||
if(DateTime.TryParse(strDisplay, out dt)) return dt;
|
||||
#else
|
||||
#if KeePassLibSD
|
||||
try { dt = DateTime.Parse(strDisplay); return dt; }
|
||||
catch(Exception) { }
|
||||
#else
|
||||
if(DateTime.TryParse(strDisplay, out dt)) return dt;
|
||||
|
||||
// For some custom formats specified using the Control Panel,
|
||||
// DateTime.ToString returns the correct string, but
|
||||
// DateTime.TryParse fails (e.g. for "//dd/MMM/yyyy");
|
||||
// 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);
|
||||
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;
|
||||
if(DateTime.TryParseExact(strDisplay, m_strDtfDate, null, dts, out dt))
|
||||
return dt;
|
||||
#endif
|
||||
|
||||
Debug.Assert(false);
|
||||
return DateTime.Now;
|
||||
}
|
||||
|
||||
#if !KeePassLibSD
|
||||
private static string DeriveCustomFormat(string strDT, DateTime dt)
|
||||
{
|
||||
string[] vPlh = new string[] {
|
||||
// Names, sorted by length
|
||||
"MMMM", "dddd",
|
||||
"MMM", "ddd",
|
||||
"gg", "g",
|
||||
|
||||
// Numbers, the ones with prefix '0' first
|
||||
"yyyy", "yyy", "yy", "y",
|
||||
"MM", "M",
|
||||
"dd", "d",
|
||||
"HH", "hh", "H", "h",
|
||||
"mm", "m",
|
||||
"ss", "s",
|
||||
|
||||
"tt", "t"
|
||||
};
|
||||
|
||||
List<string> lValues = new List<string>();
|
||||
foreach(string strPlh in vPlh)
|
||||
{
|
||||
string strEval = strPlh;
|
||||
if(strEval.Length == 1) strEval = @"%" + strPlh; // Make custom
|
||||
|
||||
lValues.Add(dt.ToString(strEval));
|
||||
}
|
||||
|
||||
StringBuilder sbAll = new StringBuilder();
|
||||
sbAll.Append("dfFghHKmMstyz:/\"\'\\%");
|
||||
sbAll.Append(strDT);
|
||||
foreach(string strVEnum in lValues) { sbAll.Append(strVEnum); }
|
||||
|
||||
List<char> lCodes = new List<char>();
|
||||
for(int i = 0; i < vPlh.Length; ++i)
|
||||
{
|
||||
char ch = StrUtil.GetUnusedChar(sbAll.ToString());
|
||||
lCodes.Add(ch);
|
||||
sbAll.Append(ch);
|
||||
}
|
||||
|
||||
string str = strDT;
|
||||
for(int i = 0; i < vPlh.Length; ++i)
|
||||
{
|
||||
string strValue = lValues[i];
|
||||
if(string.IsNullOrEmpty(strValue)) continue;
|
||||
|
||||
str = str.Replace(strValue, new string(lCodes[i], 1));
|
||||
}
|
||||
|
||||
StringBuilder sbFmt = new StringBuilder();
|
||||
bool bInLiteral = false;
|
||||
foreach(char ch in str)
|
||||
{
|
||||
int iCode = lCodes.IndexOf(ch);
|
||||
|
||||
// The escape character doesn't work correctly (e.g.
|
||||
// "dd\\.MM\\.yyyy\\ HH\\:mm\\:ss" doesn't work, but
|
||||
// "dd'.'MM'.'yyyy' 'HH':'mm':'ss" does); use '' instead
|
||||
|
||||
// if(iCode >= 0) sbFmt.Append(vPlh[iCode]);
|
||||
// else // Literal
|
||||
// {
|
||||
// sbFmt.Append('\\');
|
||||
// sbFmt.Append(ch);
|
||||
// }
|
||||
|
||||
if(iCode >= 0)
|
||||
{
|
||||
if(bInLiteral) { sbFmt.Append('\''); bInLiteral = false; }
|
||||
sbFmt.Append(vPlh[iCode]);
|
||||
}
|
||||
else // Literal
|
||||
{
|
||||
if(!bInLiteral) { sbFmt.Append('\''); bInLiteral = true; }
|
||||
sbFmt.Append(ch);
|
||||
}
|
||||
}
|
||||
if(bInLiteral) sbFmt.Append('\'');
|
||||
|
||||
return sbFmt.ToString();
|
||||
}
|
||||
#endif
|
||||
|
||||
public static string SerializeUtc(DateTime dt)
|
||||
{
|
||||
string str = dt.ToUniversalTime().ToString("s");
|
||||
@@ -218,5 +329,44 @@ namespace ModernKeePassLib.Utility
|
||||
return null;
|
||||
}
|
||||
#endif
|
||||
|
||||
private static readonly DateTime m_dtInvMin =
|
||||
new DateTime(2999, 12, 27, 23, 59, 59);
|
||||
private static readonly DateTime m_dtInvMax =
|
||||
new DateTime(2999, 12, 29, 23, 59, 59);
|
||||
public static int Compare(DateTime dtA, DateTime dtB, bool bUnkIsPast)
|
||||
{
|
||||
if(bUnkIsPast)
|
||||
{
|
||||
// 2999-12-28 23:59:59 in KeePass 1.x means 'unknown';
|
||||
// expect time zone corruption (twice)
|
||||
// bool bInvA = ((dtA.Year == 2999) && (dtA.Month == 12) &&
|
||||
// (dtA.Day >= 27) && (dtA.Day <= 29) && (dtA.Minute == 59) &&
|
||||
// (dtA.Second == 59));
|
||||
// bool bInvB = ((dtB.Year == 2999) && (dtB.Month == 12) &&
|
||||
// (dtB.Day >= 27) && (dtB.Day <= 29) && (dtB.Minute == 59) &&
|
||||
// (dtB.Second == 59));
|
||||
// Faster due to internal implementation of DateTime:
|
||||
bool bInvA = ((dtA >= m_dtInvMin) && (dtA <= m_dtInvMax) &&
|
||||
(dtA.Minute == 59) && (dtA.Second == 59));
|
||||
bool bInvB = ((dtB >= m_dtInvMin) && (dtB <= m_dtInvMax) &&
|
||||
(dtB.Minute == 59) && (dtB.Second == 59));
|
||||
|
||||
if(bInvA) return (bInvB ? 0 : -1);
|
||||
if(bInvB) return 1;
|
||||
}
|
||||
|
||||
return dtA.CompareTo(dtB);
|
||||
}
|
||||
|
||||
internal static int CompareLastMod(ITimeLogger tlA, ITimeLogger tlB,
|
||||
bool bUnkIsPast)
|
||||
{
|
||||
if(tlA == null) { Debug.Assert(false); return ((tlB == null) ? 0 : -1); }
|
||||
if(tlB == null) { Debug.Assert(false); return 1; }
|
||||
|
||||
return Compare(tlA.LastModificationTime, tlB.LastModificationTime,
|
||||
bUnkIsPast);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user