Skip to content
This repository has been archived by the owner on Sep 14, 2018. It is now read-only.

Commit

Permalink
Allow date/datetime to convert to DateTime.
Browse files Browse the repository at this point in the history
Part of #306.
  • Loading branch information
jdhardy committed Apr 26, 2015
1 parent 7beadc2 commit 31f5c88
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Languages/IronPython/IronPython.Modules/datetime.cs
Expand Up @@ -433,6 +433,10 @@ public class date : ICodeFormattable {
set { _dateTime = value; }
}

public static implicit operator DateTime(date self) {
return self._dateTime;
}

// supported operations
public static date operator +([NotNull]date self, [NotNull]timedelta other) {
try {
Expand Down
20 changes: 20 additions & 0 deletions Languages/IronPython/Tests/test_datetime.py
Expand Up @@ -14,11 +14,15 @@
#
#####################################################################################

import clr

import unittest
import datetime
import time
from test import test_support

from System import DateTime, TimeSpan

class TestDatetime(unittest.TestCase):

def test_strptime_1(self):
Expand Down Expand Up @@ -136,6 +140,22 @@ def test_fromtimestamp(self):
ts = 5399410716.777882
self.assertEqual(datetime.datetime.fromtimestamp(ts).microsecond, 777882)

def test_System_DateTime_conversion(self):
example = datetime.datetime(2015, 4, 25, 8, 39, 54)
result = clr.Convert(example, DateTime)
self.assertIsInstance(result, DateTime)

expected = DateTime(2015, 4, 25, 8, 39, 54)
self.assertEqual(expected, result)

def test_System_DateTime_binding(self):
pydt = datetime.datetime(2015, 4, 25, 8, 39, 54)
netdt = DateTime(2015, 4, 25, 9, 39, 54)

result = netdt.Subtract(pydt)
expected = TimeSpan(1, 0, 0)

self.assertEqual(expected, result)

def test_main():
from unittest import main
Expand Down
11 changes: 11 additions & 0 deletions Test/IronPython.tests
Expand Up @@ -108,6 +108,17 @@
<NotParallelSafe>false</NotParallelSafe>
<WorkingDirectory>%DLR_ROOT%\Languages\IronPython\Tests</WorkingDirectory>
</Test>
<Test>
<Name>test_datetime_20</Name>
<Filename>%DLR_ROOT%\Languages\IronPython\Internal\ipy.bat</Filename>
<Arguments>test_datetime.py</Arguments>
<MaxDuration>600000</MaxDuration>
<LongRunning>false</LongRunning>
<Disabled>false</Disabled>
<RequiresAdmin>false</RequiresAdmin>
<NotParallelSafe>false</NotParallelSafe>
<WorkingDirectory>%DLR_ROOT%\Languages\IronPython\Tests</WorkingDirectory>
</Test>
<Test>
<Name>test_dict_20</Name>
<Filename>%DLR_ROOT%\Languages\IronPython\Internal\ipy.bat</Filename>
Expand Down

0 comments on commit 31f5c88

Please sign in to comment.