Skip to content

Commit 0a43f59

Browse files
introfogyulian-gaponenko
authored andcommitted
Add CommonsProductData and move util methods from licensekey to core
DEVSIX-5233 Autoported commit. Original commit hash: [6c68ca54b] Manual files: commons/src/main/java/com/itextpdf/commons/utils/DateTimeUtil.java
1 parent 5fe7644 commit 0a43f59

File tree

7 files changed

+116
-13
lines changed

7 files changed

+116
-13
lines changed

itext.tests/itext.commons.tests/itext/commons/actions/producer/CurrentDatePlaceholderPopulatorTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public virtual void ValidComponentsComparisonTest() {
112112
// be generated at the beginning of the next hour.
113113
DateTime date = DateTimeUtil.GetCurrentUtcTime();
114114
String result = populator.Populate(null, "dd MM yy yyyy HH");
115-
String expectedResult = DateTimeUtil.FormatDate(date, "dd MM yy yyyy HH");
115+
String expectedResult = DateTimeUtil.Format(date, "dd MM yy yyyy HH");
116116
NUnit.Framework.Assert.AreEqual(expectedResult, result);
117117
}
118118

itext.tests/itext.commons.tests/itext/commons/actions/producer/ProducerBuilderTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,16 +90,16 @@ public virtual void CopyrightToProducerLineTest() {
9090
public virtual void CurrentDateProducerLineTest() {
9191
IList<ConfirmedEventWrapper> events = GetEvents("Created at ${currentDate:yyyy}", 1, 2, 3);
9292
String newProducerLine = ProducerBuilder.ModifyProducer(events, null);
93-
NUnit.Framework.Assert.AreEqual("Created at " + DateTimeUtil.FormatDate(DateTimeUtil.GetCurrentUtcTime(),
94-
"yyyy"), newProducerLine);
93+
NUnit.Framework.Assert.AreEqual("Created at " + DateTimeUtil.Format(DateTimeUtil.GetCurrentUtcTime(), "yyyy"
94+
), newProducerLine);
9595
}
9696

9797
[NUnit.Framework.Test]
9898
public virtual void CurrentDateComplexFormatProducerLineTest() {
9999
IList<ConfirmedEventWrapper> events = GetEvents("Created at ${currentDate:yyyy, '{\\'yes::yes\\'', yyyy}",
100100
1, 2, 3);
101101
String newProducerLine = ProducerBuilder.ModifyProducer(events, null);
102-
String currentYear = DateTimeUtil.FormatDate(DateTimeUtil.GetCurrentUtcTime(), "yyyy");
102+
String currentYear = DateTimeUtil.Format(DateTimeUtil.GetCurrentUtcTime(), "yyyy");
103103
NUnit.Framework.Assert.AreEqual("Created at " + currentYear + ", {'yes::yes', " + currentYear, newProducerLine
104104
);
105105
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
This file is part of the iText (R) project.
3+
Copyright (c) 1998-2021 iText Group NV
4+
Authors: iText Software.
5+
6+
This program is offered under a commercial and under the AGPL license.
7+
For commercial licensing, contact us at https://itextpdf.com/sales. For AGPL licensing, see below.
8+
9+
AGPL licensing:
10+
This program is free software: you can redistribute it and/or modify
11+
it under the terms of the GNU Affero General Public License as published by
12+
the Free Software Foundation, either version 3 of the License, or
13+
(at your option) any later version.
14+
15+
This program is distributed in the hope that it will be useful,
16+
but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
GNU Affero General Public License for more details.
19+
20+
You should have received a copy of the GNU Affero General Public License
21+
along with this program. If not, see <https://www.gnu.org/licenses/>.
22+
*/
23+
using System;
24+
using iText.Test;
25+
26+
namespace iText.Commons.Utils {
27+
public class DateTimeUtilTest : ExtendedITextTest {
28+
[NUnit.Framework.Test]
29+
public virtual void GetCurrentTest() {
30+
DateTime date = new DateTime();
31+
NUnit.Framework.Assert.AreEqual(date.ToString(), DateTimeUtil.GetCurrentUtcTime().ToString());
32+
}
33+
34+
[NUnit.Framework.Test]
35+
public virtual void IsInPastTest() {
36+
DateTime date = new DateTime(1);
37+
NUnit.Framework.Assert.IsTrue(DateTimeUtil.IsInPast(date));
38+
}
39+
}
40+
}

itext.tests/itext.sign.tests/itext/signatures/verify/OcspVerifierTest.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,18 +132,18 @@ public virtual void AuthorizedOCSPResponderTest() {
132132

133133
[NUnit.Framework.Test]
134134
public virtual void ExpiredAuthorizedOCSPResponderTest_atValidPeriod() {
135-
DateTime ocspResponderCertStartDate = DateTimeUtil.ParseSimpleFormat("15/10/2005", "dd/MM/yyyy");
136-
DateTime ocspResponderCertEndDate = DateTimeUtil.ParseSimpleFormat("15/10/2010", "dd/MM/yyyy");
137-
DateTime checkDate = DateTimeUtil.ParseSimpleFormat("15/10/2008", "dd/MM/yyyy");
135+
DateTime ocspResponderCertStartDate = DateTimeUtil.Parse("15/10/2005", "dd/MM/yyyy");
136+
DateTime ocspResponderCertEndDate = DateTimeUtil.Parse("15/10/2010", "dd/MM/yyyy");
137+
DateTime checkDate = DateTimeUtil.Parse("15/10/2008", "dd/MM/yyyy");
138138
bool verifyRes = VerifyAuthorizedOCSPResponderTest(ocspResponderCertStartDate, ocspResponderCertEndDate, checkDate
139139
);
140140
NUnit.Framework.Assert.IsTrue(verifyRes);
141141
}
142142

143143
[NUnit.Framework.Test]
144144
public virtual void ExpiredAuthorizedOCSPResponderTest_now() {
145-
DateTime ocspResponderCertStartDate = DateTimeUtil.ParseSimpleFormat("15/10/2005", "dd/MM/yyyy");
146-
DateTime ocspResponderCertEndDate = DateTimeUtil.ParseSimpleFormat("15/10/2010", "dd/MM/yyyy");
145+
DateTime ocspResponderCertStartDate = DateTimeUtil.Parse("15/10/2005", "dd/MM/yyyy");
146+
DateTime ocspResponderCertEndDate = DateTimeUtil.Parse("15/10/2010", "dd/MM/yyyy");
147147
DateTime checkDate = DateTimeUtil.GetCurrentUtcTime();
148148
NUnit.Framework.Assert.Catch(typeof(CertificateExpiredException), () => VerifyAuthorizedOCSPResponderTest(
149149
ocspResponderCertStartDate, ocspResponderCertEndDate, checkDate));
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
3+
namespace iText.Commons.Actions.Data {
4+
/// <summary>
5+
/// Stores an instance of
6+
/// <see cref="ProductData"/>
7+
/// related to iText commons module.
8+
/// </summary>
9+
public class CommonsProductData {
10+
private const String COMMONS_PUBLIC_PRODUCT_NAME = "Commons";
11+
12+
private const String COMMONS_PRODUCT_NAME = "commons";
13+
14+
private const String COMMONS_VERSION = "7.2.0-SNAPSHOT";
15+
16+
private const int COMMONS_COPYRIGHT_SINCE = 2000;
17+
18+
private const int COMMONS_COPYRIGHT_TO = 2021;
19+
20+
private static readonly ProductData COMMONS_PRODUCT_DATA = new ProductData(COMMONS_PUBLIC_PRODUCT_NAME, COMMONS_PRODUCT_NAME
21+
, COMMONS_VERSION, COMMONS_COPYRIGHT_SINCE, COMMONS_COPYRIGHT_TO);
22+
23+
/// <summary>
24+
/// Getter for an instance of
25+
/// <see cref="ProductData"/>
26+
/// related to iText commons module.
27+
/// </summary>
28+
/// <returns>iText commons product description</returns>
29+
public static ProductData GetInstance() {
30+
return COMMONS_PRODUCT_DATA;
31+
}
32+
}
33+
}

itext/itext.commons/itext/commons/actions/producer/CurrentDatePlaceholderPopulator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ private int ProcessDateComponent(int index, DateTime date, StringBuilder builder
127127
}
128128
String piece = peaceBuilder.ToString();
129129
if (ALLOWED_PATTERNS.Contains(piece)) {
130-
builder.Append(DateTimeUtil.FormatDate(date, piece));
130+
builder.Append(DateTimeUtil.Format(date, piece));
131131
}
132132
else {
133133
throw new ArgumentException(MessageFormatUtil.Format(CommonsExceptionMessageConstant.PATTERN_CONTAINS_UNEXPECTED_COMPONENT

itext/itext.commons/itext/commons/utils/DateTimeUtil.cs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ namespace iText.Commons.Utils {
5151
/// Be aware that its API and functionality may be changed in future.
5252
/// </summary>
5353
public static class DateTimeUtil {
54+
private static readonly String DEFAULT_PATTERN = "yyyy-MM-dd";
55+
5456
public static double GetUtcMillisFromEpoch(DateTime? dateTime) {
5557
if (dateTime == null) {
5658
dateTime = DateTime.Now;
@@ -69,14 +71,42 @@ public static DateTime GetCurrentTime() {
6971
public static DateTime GetCurrentUtcTime() {
7072
return DateTime.UtcNow;
7173
}
74+
75+
public static bool IsInPast(DateTime date) {
76+
return date.CompareTo(GetCurrentTime()) < 0;
77+
}
78+
79+
public static long GetRelativeTime(DateTime date) {
80+
return (long) (date - GetInitial()).TotalMilliseconds;
81+
}
7282

73-
public static DateTime ParseSimpleFormat(String date, String format) {
74-
return DateTime.ParseExact(date, format, null);
83+
/// <summary>
84+
/// Parses passing date with default {@code yyyy-MM-dd} pattern.
85+
/// </summary>
86+
/// <param name="date">date is date to be parse</param>
87+
/// <returns>parse date</returns>
88+
public static DateTime ParseWithDefaultPattern(String date) {
89+
return Parse(date, DEFAULT_PATTERN);
90+
}
91+
92+
public static DateTime Parse(String date, String format) {
93+
// The method is rarely called, so every time we create a new DateTimeFormatInfo (necessary for automatic testing)
94+
DateTimeFormatInfo dateTimeFormatInfo = new DateTimeFormatInfo();
95+
dateTimeFormatInfo.Calendar = new GregorianCalendar();
96+
return DateTime.ParseExact(date, format, dateTimeFormatInfo);
7597
}
7698

77-
public static String FormatDate(DateTime date, String pattern)
99+
public static String FormatWithDefaultPattern(DateTime date) {
100+
return Format(date, DEFAULT_PATTERN);
101+
}
102+
103+
public static String Format(DateTime date, String pattern)
78104
{
79105
return date.ToString(pattern, CultureInfo.InvariantCulture);
80106
}
107+
108+
private static DateTime GetInitial() {
109+
return new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
110+
}
81111
}
82112
}

0 commit comments

Comments
 (0)