Skip to content

Commit

Permalink
Merge 1f6ec31 into fce2650
Browse files Browse the repository at this point in the history
  • Loading branch information
jswhit authored Apr 23, 2020
2 parents fce2650 + 1f6ec31 commit e57fd33
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
29 changes: 23 additions & 6 deletions cftime/_cftime.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1353,12 +1353,29 @@ Gregorial calendar.
str(self))

def __str__(self):
second = '{:02d}'.format(self.second)
if self.microsecond:
second += '.{:06d}'.format(self.microsecond)

return "{:04d}-{:02d}-{:02d} {:02d}:{:02d}:{}".format(
self.year, self.month, self.day, self.hour, self.minute, second)
return self.isoformat()

def isoformat(self,sep=' ',timespec='auto'):
second = ":%02i" %self.second
if (timespec == 'auto' and self.microsecond) or timespec == 'microseconds':
second += ".%06i" % self.microsecond
if timespec == 'milliseconds':
millisecs = self.microsecond/1000
second += ".%03i" % millisecs
if timespec in ['auto', 'microseconds', 'milliseconds']:
return "%04i-%02i-%02i%s%02i:%02i%s" %\
(self.year, self.month, self.day, sep, self.hour, self.minute, second)
elif timespec == 'seconds':
return "%04i-%02i-%02i%s%02i:%02i:%02i" %\
(self.year, self.month, self.day, sep, self.hour, self.minute, self.second)
elif timespec == 'minutes':
return "%04i-%02i-%02i%s%02i:%02i" %\
(self.year, self.month, self.day, sep, self.hour, self.minute)
elif timespec == 'hours':
return "%04i-%02i-%02i%s%02i" %\
(self.year, self.month, self.day, sep, self.hour)
else:
raise ValueError('illegal timespec')

def __hash__(self):
try:
Expand Down
7 changes: 7 additions & 0 deletions test/test_cftime.py
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,13 @@ def test_tz_naive(self):
# issue #165: make sure python datetime returned
d=num2date(0,units="seconds since 2000-01-01 00:00:00",only_use_cftime_datetimes=False)
assert isinstance(d, datetime)
# issue #152 add isoformat()
assert(d.isoformat()[0:24] == '2009-12-22 00:00:00.0156')
assert(d.isoformat(sep=' ')[0:24] == '2009-12-22 00:00:00.0156')
assert(d.isoformat(sep=' ',timespec='milliseconds') == '2009-12-22 00:00:00.015')
assert(d.isoformat(sep=' ',timespec='seconds') == '2009-12-22 00:00:00')
assert(d.isoformat(sep=' ',timespec='minutes') == '2009-12-22 00:00')
assert(d.isoformat(sep=' ',timespec='hours') == '2009-12-22 00')

class TestDate2index(unittest.TestCase):

Expand Down

0 comments on commit e57fd33

Please sign in to comment.