Skip to content

Commit

Permalink
Merge pull request beeware#739 from prithvisingh18/master
Browse files Browse the repository at this point in the history
added ctime() code in time.java
  • Loading branch information
eliasdorneles authored Mar 2, 2018
2 parents f943341 + 57fa681 commit ab570f5
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 5 deletions.
30 changes: 27 additions & 3 deletions python/common/python/time.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,34 @@ public static org.python.Object clock() {
}

@org.python.Method(
__doc__ = ""
__doc__ = "ctime(seconds) -> string\n\n" +
"Convert a time in seconds since the Epoch to a string in local time.\n" +
"This is equivalent to asctime(localtime(seconds)). When the time tuple is\n" +
"not present, current time as returned by localtime() is used.",
default_args = {"seconds"}
)
public static org.python.Object ctime() {
throw new org.python.exceptions.NotImplementedError("time.ctime() has not been implemented.");
public static org.python.Object ctime(org.python.Object seconds) {
java.util.Date date;
if (seconds == null) {
long currentTimeInMillis = System.currentTimeMillis();
date = new java.util.Date(currentTimeInMillis);
} else {
//Type checking the parameter
if (!(seconds instanceof org.python.types.Int) && !(seconds instanceof org.python.types.Float)) {
throw new org.python.exceptions.TypeError("an integer is required (got type " + seconds.typeName() + ")");
}
date = new java.util.Date(((org.python.types.Int) seconds.__int__()).value * 1000L);
}
java.text.SimpleDateFormat ft = new java.text.SimpleDateFormat("E MMM dd HH:mm:ss yyyy");
String padded_date = ft.format(date);
if (Character.toString(padded_date.charAt(8)).equals("0")) {
java.lang.StringBuilder sb = new StringBuilder();
sb.append(padded_date.substring(0, 7));
sb.append(" ");
sb.append(padded_date.substring(9, padded_date.length()));
padded_date = sb.toString();
}
return new org.python.types.Str(padded_date);
}

public static org.python.types.Int daylight;
Expand Down
25 changes: 23 additions & 2 deletions tests/stdlib/test_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,34 @@ def test_clock(self):

#######################################################
# ctime
@expectedFailure
def test_ctime(self):
self.assertCodeExecution("""
import time
print(time.ctime())
print(time.ctime()[:10], time.ctime()[-4:])
""")

def test_ctime_with_parameter(self):
self.assertCodeExecution("""
import time
print(time.ctime(0))
print(time.ctime(1000))
now = time.time()
print(time.ctime((now - (now % 3600))))
print(time.ctime(1000.67))
try:
time.ctime('today')
except Exception as e:
print(e)
try:
time.ctime([1,2])
except Exception as e:
print(e)
try:
time.ctime((1,2))
except Exception as e:
print(e)
""")

#######################################################
# daylight
@expectedFailure
Expand Down

0 comments on commit ab570f5

Please sign in to comment.