Skip to content

Commit

Permalink
LT-21703: Fix problem with time/date format changing (#396)
Browse files Browse the repository at this point in the history
* LT-21703: Fix problem with time/date format changing

Instead of using a hardcoded time/date format string use the
one from LCM.
https://jira.sil.org/browse/LT-21703
  • Loading branch information
mark-sil authored Apr 10, 2024
1 parent bfd2a98 commit fd0ea8a
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ void IPremerger.Premerge(IMergeEventListener listener, ref XmlNode ourDateTimeNo
// something else besides the timestamp changed. Set timestamp to current time.
newestDateTime = DateTimeProvider.Current.UtcNow;
}
var newestDateTimeString = newestDateTime.ToString("yyyy-M-d H:m:s.fff", CultureInfo.InvariantCulture);

string formatStr = SIL.LCModel.Utils.SilUtilsExtensions.LCMTimeFormatWithMillis;
var newestDateTimeString = newestDateTime.ToString(formatStr, CultureInfo.InvariantCulture);

UpdateDateTimeVal(newestDateTimeString, ourDateTimeNode);
UpdateDateTimeVal(newestDateTimeString, theirDateTimeNode);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static string GetXPathNodeFrom(string xml, string xpath)

public static DateTime _expectedUtcDateTime;

public static string ExpectedUtcDateTimeString => ExpectedDateTime.ToString("yyyy-M-d H:m:s.fff", CultureInfo.InvariantCulture);
public static string ExpectedUtcDateTimeString => ExpectedDateTime.ToString(SIL.LCModel.Utils.SilUtilsExtensions.LCMTimeFormatWithMillis, CultureInfo.InvariantCulture);

public static DateTime ExpectedDateTime
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2010-2016 SIL International
// Copyright (c) 2010-2016 SIL International
// This software is licensed under the MIT License (http://opensource.org/licenses/MIT)

using System;
Expand Down Expand Up @@ -41,7 +41,7 @@ public override void TestTearDown()

private const string CommonOwnSeqAncestor =
@"<ownseq class='PartOfSpeech' guid ='c1ed6db0-e382-11de-8a39-0800200c9a66'>
<DateModified val='2000-1-1 23:59:59.123' />
<DateModified val='2000-01-01 23:59:59.123' />
<Name>
<AUni
ws='en'>commonName</AUni>
Expand All @@ -51,7 +51,7 @@ public override void TestTearDown()
[Test]
public void OurOriginalTimestampRestoredToAncestorValueIfOnlyChangeWasTimestampAndTheyDeletedParent()
{
var ourContent = CommonOwnSeqAncestor.Replace("2000-1-1 23:59:59.123", "2002-1-1 23:59:59.123");
var ourContent = CommonOwnSeqAncestor.Replace("2000-01-01 23:59:59.123", "2002-01-01 23:59:59.123");

var ancestorNode = XmlUtilities.GetDocumentNodeFromRawXml(CommonOwnSeqAncestor, new XmlDocument());
var ancestorModPropNode = ancestorNode.SelectSingleNode("DateModified");
Expand All @@ -60,13 +60,13 @@ public void OurOriginalTimestampRestoredToAncestorValueIfOnlyChangeWasTimestampA
IPremerger premerger = new PreferMostRecentTimePreMerger();
premerger.Premerge(new ListenerForUnitTests(), ref ourModPropNode, null, ancestorModPropNode);

Assert.AreEqual("2000-1-1 23:59:59.123", ourModPropNode.Attributes["val"].Value);
Assert.AreEqual("2000-01-01 23:59:59.123", ourModPropNode.Attributes["val"].Value);
}

[Test]
public void TimestampUpdatedIfAnotherChangedWasMadeAndTheyDeletedParent()
{
var ourContent = CommonOwnSeqAncestor.Replace("2000-1-1 23:59:59.123", "2002-1-1 23:59:59.123").Replace("commonName", "ourModifiedName");
var ourContent = CommonOwnSeqAncestor.Replace("2000-01-01 23:59:59.123", "2002-01-01 23:59:59.123").Replace("commonName", "ourModifiedName");

var ancestorNode = XmlUtilities.GetDocumentNodeFromRawXml(CommonOwnSeqAncestor, new XmlDocument());
var ancestorModPropNode = ancestorNode.SelectSingleNode("DateModified");
Expand All @@ -81,7 +81,7 @@ public void TimestampUpdatedIfAnotherChangedWasMadeAndTheyDeletedParent()
[Test]
public void TheirOriginalTimestampRestoredToAncestorValueIfOnlyChangeWasTimestampAndWeDeletedParent()
{
var theirContent = CommonOwnSeqAncestor.Replace("2000-1-1 23:59:59.123", "2002-1-1 23:59:59.123");
var theirContent = CommonOwnSeqAncestor.Replace("2000-01-01 23:59:59.123", "2002-01-01 23:59:59.123");

var ancestorNode = XmlUtilities.GetDocumentNodeFromRawXml(CommonOwnSeqAncestor, new XmlDocument());
var ancestorModPropNode = ancestorNode.SelectSingleNode("DateModified");
Expand All @@ -91,13 +91,13 @@ public void TheirOriginalTimestampRestoredToAncestorValueIfOnlyChangeWasTimestam
XmlNode ourNode = null;
premerger.Premerge(new ListenerForUnitTests(), ref ourNode, theirModPropNode, ancestorModPropNode);

Assert.AreEqual("2000-1-1 23:59:59.123", theirModPropNode.Attributes["val"].Value);
Assert.AreEqual("2000-01-01 23:59:59.123", theirModPropNode.Attributes["val"].Value);
}

[Test]
public void TimestampUpdatedIfAnotherChangeWasMadeAndWeDeletedParent()
{
var theirContent = CommonOwnSeqAncestor.Replace("2000-1-1 23:59:59.123", "2002-1-1 23:59:59.123").Replace("commonName", "theirModifiedName");
var theirContent = CommonOwnSeqAncestor.Replace("2000-01-01 23:59:59.123", "2002-01-01 23:59:59.123").Replace("commonName", "theirModifiedName");

var ancestorNode = XmlUtilities.GetDocumentNodeFromRawXml(CommonOwnSeqAncestor, new XmlDocument());
var ancestorModPropNode = ancestorNode.SelectSingleNode("DateModified");
Expand Down Expand Up @@ -130,16 +130,16 @@ public void TimestampUpdatedIfAnotherChangeWasMadeAndWeDeletedParent()
</ReversalIndex>
</Root>";

[TestCase("<DateModified val='2000-1-1 23:59:59.123' />", "2002-1-1 23:59:59.123", "2001-1-1 23:59:59.123", "newModifiedName", 2, new[]{ typeof(XmlAttributeBothMadeSameChangeReport), typeof(XmlTextBothMadeSameChangeReport) }, TestName = "Timestamp updated")]
[TestCase("", "2002-1-1 23:59:59.123", "2001-1-1 23:59:59.123", "newModifiedName", 2, new[] { typeof(XmlAttributeBothAddedReport), typeof(XmlTextBothMadeSameChangeReport) }, TestName = "Timestamp updated - no ancestor timestamp")]
[TestCase("", "2002-1-1 23:59:59.123", "2001-1-1 23:59:59.123", "commonName", 1, new[] { typeof(XmlAttributeBothAddedReport) }, TestName = "DateModified-only change - Timestamp updated")]
[TestCase("<DateModified val='2000-1-1 23:59:59.123' />", "2001-1-1 23:59:59.123", "2002-1-1 23:59:59.123", "newModifiedName", 2, new[]{ typeof(XmlAttributeBothMadeSameChangeReport), typeof(XmlTextBothMadeSameChangeReport) })]
[TestCase("", "2001-1-1 23:59:59.123", "2002-1-1 23:59:59.123", "commonName", 1, new[] { typeof(XmlAttributeBothAddedReport) })]
[TestCase("<DateModified val='2000-01-01 23:59:59.123' />", "2002-01-01 23:59:59.123", "2001-01-01 23:59:59.123", "newModifiedName", 2, new[]{ typeof(XmlAttributeBothMadeSameChangeReport), typeof(XmlTextBothMadeSameChangeReport) }, TestName = "Timestamp updated")]
[TestCase("", "2002-01-01 23:59:59.123", "2001-01-01 23:59:59.123", "newModifiedName", 2, new[] { typeof(XmlAttributeBothAddedReport), typeof(XmlTextBothMadeSameChangeReport) }, TestName = "Timestamp updated - no ancestor timestamp")]
[TestCase("", "2002-01-01 23:59:59.123", "2001-01-01 23:59:59.123", "commonName", 1, new[] { typeof(XmlAttributeBothAddedReport) }, TestName = "DateModified-only change - Timestamp updated")]
[TestCase("<DateModified val='2000-01-01 23:59:59.123' />", "2001-01-01 23:59:59.123", "2002-01-01 23:59:59.123", "newModifiedName", 2, new[]{ typeof(XmlAttributeBothMadeSameChangeReport), typeof(XmlTextBothMadeSameChangeReport) })]
[TestCase("", "2001-01-01 23:59:59.123", "2002-01-01 23:59:59.123", "commonName", 1, new[] { typeof(XmlAttributeBothAddedReport) })]
public void MergeConflict_TimestampUpdated(string ancestorDate, string ourDate, string theirDate, string modification, int expectedChangeCount, Type[] expectedChangeTypes)
{
var ancestorContent = CommonPosAncestor.Replace("DateGoesHere", ancestorDate);
var ourContent = CommonPosAncestor.Replace("DateGoesHere", "<DateModified val='2002-1-1 23:59:59.123' />").Replace("commonName", modification);
var theirContent = CommonPosAncestor.Replace("DateGoesHere", "<DateModified val='2001-1-1 23:59:59.123' />").Replace("commonName", modification);
var ourContent = CommonPosAncestor.Replace("DateGoesHere", "<DateModified val='2002-01-01 23:59:59.123' />").Replace("commonName", modification);
var theirContent = CommonPosAncestor.Replace("DateGoesHere", "<DateModified val='2001-01-01 23:59:59.123' />").Replace("commonName", modification);

var results = FieldWorksTestServices.DoMerge(
FileHandler,
Expand All @@ -153,12 +153,12 @@ public void MergeConflict_TimestampUpdated(string ancestorDate, string ourDate,
Is.EqualTo(ExpectedUtcDateTimeString));
}

[TestCase("<DateModified val='2000-1-1 23:59:59.123' />", "commonName", 0, new Type[0], TestName = "DateModified-only change sets ancestor timestamp")]
[TestCase("<DateModified val='2000-01-01 23:59:59.123' />", "commonName", 0, new Type[0], TestName = "DateModified-only change sets ancestor timestamp")]
public void TimestampOnlyChange_TimestampKept(string ancestorDate, string modification, int expectedChangeCount, Type[] expectedChangeTypes)
{
var ancestorContent = CommonPosAncestor.Replace("DateGoesHere", ancestorDate);
var ourContent = CommonPosAncestor.Replace("DateGoesHere", "<DateModified val='2002-1-1 23:59:59.123' />").Replace("commonName", modification);
var theirContent = CommonPosAncestor.Replace("DateGoesHere", "<DateModified val='2001-1-1 23:59:59.123' />").Replace("commonName", modification);
var ourContent = CommonPosAncestor.Replace("DateGoesHere", "<DateModified val='2002-01-01 23:59:59.123' />").Replace("commonName", modification);
var theirContent = CommonPosAncestor.Replace("DateGoesHere", "<DateModified val='2001-01-01 23:59:59.123' />").Replace("commonName", modification);

var results = FieldWorksTestServices.DoMerge(
FileHandler,
Expand All @@ -169,7 +169,7 @@ public void TimestampOnlyChange_TimestampKept(string ancestorDate, string modifi
0, new List<Type>(),
expectedChangeCount, new List<Type>(expectedChangeTypes));
Assert.That(GetXPathNodeFrom(results, "Root/ReversalIndex/PartsOfSpeech/CmPossibilityList/Possibilities/ownseq/DateModified/@val"),
Is.EqualTo("2000-1-1 23:59:59.123"));
Is.EqualTo("2000-01-01 23:59:59.123"));
}

private static DateTime GetMergedTime(string filePath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ public void SampleMergeWithAtomicConflictWeWin()
<LexDb guid='06425922-3258-4094-a9ec-3c2fe5b52b39' >
<Introduction>
<StText guid='45b78bcf-2400-48d5-a9c1-973447d36d4e'>
<DateModified val='2011-2-2 19:39:28.829' />
<DateModified val='2011-02-02 19:39:28.829' />
<Paragraphs>
<ownseq class='StTxtPara' guid='9edbb6e1-2bdd-481c-b84d-26c69f22856c'>
<ParseIsCurrent val='False' />
Expand All @@ -298,8 +298,8 @@ public void SampleMergeWithAtomicConflictWeWin()
public void MergeHasNoReportsForDeepDateModifiedChangesAndKeepsMostRecent()
{
const string commonAncestor = baseDocument;
var ourContent = commonAncestor.Replace("2011-2-2 19:39:28.829", "2012-2-2 19:39:28.829").Replace("False", "True");
var theirContent = commonAncestor.Replace("2011-2-2 19:39:28.829", "2013-2-2 19:39:28.829").Replace("False", "True");
var ourContent = commonAncestor.Replace("2011-02-02 19:39:28.829", "2012-02-02 19:39:28.829").Replace("False", "True");
var theirContent = commonAncestor.Replace("2011-02-02 19:39:28.829", "2013-02-02 19:39:28.829").Replace("False", "True");

var results = FieldWorksTestServices.DoMerge(
FileHandler,
Expand All @@ -320,8 +320,8 @@ public void SampleMergeWithConflicts()
<AUni ws='en'>Original Dictionary</AUni>
</Name>");

var ourContent = commonAncestor.Replace("Original Dictionary", "My Dictionary").Replace("2011-2-2 19:39:28.829", "2012-2-2 19:39:28.829");
var theirContent = commonAncestor.Replace("Original Dictionary", "Their Dictionary").Replace("2011-2-2 19:39:28.829", "2013-2-2 19:39:28.829");
var ourContent = commonAncestor.Replace("Original Dictionary", "My Dictionary").Replace("2011-02-02 19:39:28.829", "2012-02-02 19:39:28.829");
var theirContent = commonAncestor.Replace("Original Dictionary", "Their Dictionary").Replace("2011-02-02 19:39:28.829", "2013-02-02 19:39:28.829");

var results = FieldWorksTestServices.DoMerge(
FileHandler,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) 2017 SIL International
// Copyright (c) 2017 SIL International
// This software is licensed under the MIT License (http://opensource.org/licenses/MIT)

using System.Xml;
Expand Down Expand Up @@ -139,7 +139,7 @@ public void PreMerge_WithAncestorOnlyTimestampChange_KeepsTimestamp()
var ours = CreateNode(CommonAncestor);

_preMerger.Premerge(null, ref ours, theirs, ancestor);
Assert.That(GetDateTimeValue(ours), Is.EqualTo("2010-1-3 3:4:5.678"));
Assert.That(GetDateTimeValue(ours), Is.EqualTo("2010-01-03 03:04:05.678"));
}

[Test]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<PackageReference Include="GitVersion.MsBuild" Version="5.10.3" PrivateAssets="all" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="SIL.Chorus.LibChorus.TestUtilities" Version="$(ChorusVersion)" />
<PackageReference Include="SIL.Chorus.Mercurial" Version="6.5.1.22" IncludeAssets="build" />
<PackageReference Include="SIL.Chorus.Mercurial" Version="6.5.1.25" IncludeAssets="build" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
<RnResearchNbk
guid="d739cbea-ea5e-11de-85be-0013722f8dec">
<DateCreated
val="2012-5-11 20:30:59.450" />
val="2012-05-11 20:30:59.450" />
<DateModified
val="2012-5-11 21:25:40.965" />
val="2012-05-11 21:25:40.965" />
<Name>
<AUni
ws="en">Data Notebook</AUni>
Expand All @@ -23,9 +23,9 @@
t="r" />
</AnthroCodes>
<DateCreated
val="2012-5-11 21:25:40.964" />
val="2012-05-11 21:25:40.964" />
<DateModified
val="2012-5-11 21:25:55.978" />
val="2012-05-11 21:25:55.978" />
<DateOfEvent
val="0" />
<Locations>
Expand All @@ -50,7 +50,7 @@
<StText
guid="f0392d21-e8eb-4c05-8bc7-783ce641223b">
<DateModified
val="2012-5-11 21:20:21.980" />
val="2012-05-11 21:20:21.980" />
<Paragraphs>
<ownseq
class="StTxtPara"
Expand All @@ -70,9 +70,9 @@
</StText>
</Contents>
<DateCreated
val="2012-5-11 21:20:21.80" />
val="2012-05-11 21:20:21.80" />
<DateModified
val="2012-5-11 21:25:40.965" />
val="2012-05-11 21:25:40.965" />
<Description>
<AStr
ws="fr">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
<RnResearchNbk
guid="d739cbea-ea5e-11de-85be-0013722f8dec">
<DateCreated
val="2012-5-11 20:30:59.450" />
val="2012-05-11 20:30:59.450" />
<DateModified
val="2012-5-11 21:25:40.965" />
val="2012-05-11 21:25:40.965" />
<Name>
<AUni
ws="en">Data Notebook</AUni>
Expand All @@ -23,9 +23,9 @@
t="r" />
</AnthroCodes>
<DateCreated
val="2012-5-11 21:25:40.964" />
val="2012-05-11 21:25:40.964" />
<DateModified
val="2012-5-11 21:25:55.978" />
val="2012-05-11 21:25:55.978" />
<DateOfEvent
val="0" />
<Locations>
Expand All @@ -50,7 +50,7 @@
<StText
guid="f0392d21-e8eb-4c05-8bc7-783ce641223b">
<DateModified
val="2012-5-11 21:20:21.980" />
val="2012-05-11 21:20:21.980" />
<Paragraphs>
<ownseq
class="StTxtPara"
Expand Down Expand Up @@ -339,9 +339,9 @@
</StText>
</Contents>
<DateCreated
val="2012-5-11 21:20:21.80" />
val="2012-05-11 21:20:21.80" />
<DateModified
val="2012-5-11 21:25:40.965" />
val="2012-05-11 21:25:40.965" />
<Description>
<AStr
ws="fr">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
<RnResearchNbk
guid="d739cbea-ea5e-11de-85be-0013722f8dec">
<DateCreated
val="2012-5-11 20:30:59.450" />
val="2012-05-11 20:30:59.450" />
<DateModified
val="2012-5-11 21:25:40.965" />
val="2012-05-11 21:25:40.965" />
<Name>
<AUni
ws="en">Data Notebook</AUni>
Expand All @@ -23,9 +23,9 @@
t="r" />
</AnthroCodes>
<DateCreated
val="2012-5-11 21:25:40.964" />
val="2012-05-11 21:25:40.964" />
<DateModified
val="2012-5-11 21:25:55.978" />
val="2012-05-11 21:25:55.978" />
<DateOfEvent
val="0" />
<Locations>
Expand All @@ -50,16 +50,16 @@
<StText
guid="f0392d21-e8eb-4c05-8bc7-783ce641223b">
<DateModified
val="2012-5-11 21:20:21.980" />
val="2012-05-11 21:20:21.980" />
<Paragraphs />
<RightToLeft
val="False" />
</StText>
</Contents>
<DateCreated
val="2012-5-11 21:20:21.80" />
val="2012-05-11 21:20:21.80" />
<DateModified
val="2012-5-11 21:25:40.965" />
val="2012-05-11 21:25:40.965" />
<Description>
<AStr
ws="fr">
Expand Down

0 comments on commit fd0ea8a

Please sign in to comment.