Skip to content

Commit 0d4b0d8

Browse files
authored
Use _strptime and improve strftime (#1702)
* Use _strptime and improve strftime * Fix some tests * Add some tests
1 parent c82d419 commit 0d4b0d8

File tree

14 files changed

+269
-346
lines changed

14 files changed

+269
-346
lines changed

Src/IronPython.Modules/_datetime.cs

Lines changed: 99 additions & 81 deletions
Large diffs are not rendered by default.

Src/IronPython.Modules/time.cs

Lines changed: 75 additions & 196 deletions
Large diffs are not rendered by default.

Src/IronPython/Runtime/Operations/StringOps.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1761,7 +1761,7 @@ internal static string GetEncodingName(Encoding encoding, bool normalize = true,
17611761
case 65000: name = "utf-7"; break;
17621762
case 65001: name = "utf-8"; break;
17631763
}
1764-
#if !NETCOREAPP && !NETSTANDARD
1764+
#if NETFRAMEWORK
17651765
if (encoding.IsBrowserDisplay) {
17661766
name = encoding.WebName;
17671767
}
@@ -1996,7 +1996,7 @@ internal static partial class CodecsInfo {
19961996
internal static readonly IDictionary<string, Lazy<Encoding?>> Codecs;
19971997

19981998
static CodecsInfo() {
1999-
#if NETCOREAPP || NETSTANDARD
1999+
#if !NETFRAMEWORK
20002000
// This ensures that Encoding.GetEncoding(0) will return the default Windows ANSI code page
20012001
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
20022002
#endif

Src/IronPython/Runtime/PythonContext.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,6 +1129,15 @@ internal object GetCopyRegModule() {
11291129
return copyRegModule;
11301130
}
11311131

1132+
private object strptimeModule;
1133+
1134+
internal object GetStrptimeModule() {
1135+
if (strptimeModule is null) {
1136+
strptimeModule = Importer.ImportModule(SharedContext, new PythonDictionary(), "_strptime", false, 0);
1137+
}
1138+
return strptimeModule;
1139+
}
1140+
11321141
public object GetWarningsModule() {
11331142
object warnings = null;
11341143
try {

Src/IronPython/Runtime/Types/DocBuilder.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ private static string GetXmlDocLocation(Assembly assem) {
542542
return location;
543543
}
544544

545-
#if !NETCOREAPP && !NETSTANDARD
545+
#if NETFRAMEWORK
546546
private const string _frameworkReferencePath = @"Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0";
547547
#endif
548548

@@ -571,7 +571,7 @@ private static XPathDocument GetXPathDocument(Assembly asm) {
571571
if (!File.Exists(xml)) {
572572
xml = Path.Combine(baseDir, baseFile);
573573
if (!File.Exists(xml)) {
574-
#if !NETCOREAPP && !NETSTANDARD
574+
#if NETFRAMEWORK
575575
// On .NET 4.0 documentation is in the reference assembly location
576576
// for 64-bit processes, we need to look in Program Files (x86)
577577
xml = Path.Combine(

Src/IronPythonTest/Cases/CPythonCasesManifest.ini

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ RunCondition=$(IS_POSIX)
301301
Ignore=true
302302
Reason=unittest.case.SkipTest: No module named _curses
303303

304-
[CPython.test_datetime] # test_datetime_stdlib
304+
[CPython.test_datetime] # IronPython.test_datetime_stdlib
305305
Ignore=true
306306

307307
[CPython.test_dbm_dumb]
@@ -845,16 +845,10 @@ Ignore=true
845845
[CPython.test_statistics]
846846
Ignore=true
847847

848-
[CPython.test_strftime]
849-
Ignore=true
850-
851848
[CPython.test_stringprep]
852849
Ignore=true
853850
Reason=Requires unicode 3.2.0 data - https://github.com/IronLanguages/ironpython3/issues/691
854851

855-
[CPython.test_strptime] # IronPython.test_strptime_stdlib
856-
Ignore=true
857-
858852
[CPython.test_strtod]
859853
Ignore=true
860854
Reason=unittest.case.SkipTest: correctly-rounded string->float conversions not available on this system
@@ -950,7 +944,7 @@ RunCondition=$(IS_POSIX)
950944
Ignore=true
951945
Reason=unittest.case.SkipTest: Cant test signal on win32
952946

953-
[CPython.test_time]
947+
[CPython.test_time] # IronPython.test_time_stdlib
954948
Ignore=true
955949

956950
[CPython.test_timeout]

Src/StdLib/Lib/test/test_strftime.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ class Y1900Tests(unittest.TestCase):
185185
def test_y_before_1900(self):
186186
# Issue #13674, #19634
187187
t = (1899, 1, 1, 0, 0, 0, 0, 0, 0)
188-
if (sys.platform == "win32"
188+
if not sys.implementation.name == "ironpython" and (sys.platform == "win32"
189189
or sys.platform.startswith(("aix", "sunos", "solaris"))):
190190
with self.assertRaises(ValueError):
191191
time.strftime("%y", t)

Tests/modules/misc/test_datetime.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,13 +1038,13 @@ def test_time(self):
10381038
x = datetime.time(2,28,3,99,None)
10391039
self.assertEqual(x.tzname(), None)
10401040

1041-
@skipUnlessIronPython()
10421041
def test_tzinfo(self):
10431042
x = datetime.tzinfo()
10441043
self.assertRaises(NotImplementedError, x.utcoffset, None)
10451044
self.assertRaises(NotImplementedError, x.dst, None)
10461045
self.assertRaises(NotImplementedError, x.tzname, None)
1047-
self.assertRaises(NotImplementedError, x.fromutc, None)
1046+
self.assertRaises(TypeError, x.fromutc, None)
1047+
self.assertRaises(ValueError, x.fromutc, datetime.datetime.now())
10481048

10491049
def test_invariant(self):
10501050
for x in range(0, 10000, 99):

Tests/modules/system_related/test_time.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ def test_strftime(self):
5757
]
5858

5959
for dateTime, result in tests:
60-
self.assertEqual(time.strftime('%Y-%m-%d %H:%M:%S',time.struct_time(dateTime)), result)
60+
self.assertEqual(time.strftime('%Y-%m-%d %H:%M:%S', time.struct_time(dateTime)), result)
61+
62+
# https://github.com/IronLanguages/ironpython3/issues/1121
63+
self.assertTrue(time.strftime("%Z%z")) # make sure it doesn't produce an empty string
6164

6265
def test_strptime(self):
6366
import time
@@ -77,16 +80,10 @@ def test_strptime(self):
7780
self.assertEqual((1942, 1, 1, 0, 0, 0, 3, 1, -1), time.strptime("%1942", "%%%Y"))
7881
self.assertEqual((1900, 1, 6, 0, 0, 0, 5, 6, -1), time.strptime("%6", "%%%d"))
7982

80-
if is_cli: # https://github.com/IronLanguages/main/issues/239
81-
# TODO: day of the week does not work as expected
82-
import System.DateTime
83-
if System.DateTime(System.DateTime.Now.Year, 7, 9).DayOfWeek == System.DayOfWeek.Friday:
84-
self.assertEqual((1900, 7, 9, 19, 30, 0, 4, 190, -1), time.strptime('Fri, July 9 7:30 PM', '%a, %B %d %I:%M %p'))
85-
else:
86-
with self.assertRaises(ValueError):
87-
time.strptime('Fri, July 9 7:30 PM', '%a, %B %d %I:%M %p')
88-
else:
89-
self.assertEqual((1900, 7, 9, 19, 30, 0, 4, 190, -1), time.strptime('Fri, July 9 7:30 PM', '%a, %B %d %I:%M %p'))
83+
# https://github.com/IronLanguages/main/issues/239
84+
self.assertEqual(time.strptime('Fri', '%a'), (1900, 1, 1, 0, 0, 0, 4, 1, -1))
85+
for i, d in enumerate(('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun')):
86+
self.assertEqual((1900, 7, 9, 19, 30, 0, i, 190, -1), time.strptime(d + ', July 9 7:30 PM', '%a, %B %d %I:%M %p'))
9087

9188
self.assertEqual((1900, 7, 9, 19, 30, 0, 0, 190, -1), time.strptime('July 9 7:30 PM', '%B %d %I:%M %p'))
9289
# CPY & IPY differ on daylight savings time for this parse

0 commit comments

Comments
 (0)