Skip to content

Commit

Permalink
Merge ee5bb21 into 3fd650b
Browse files Browse the repository at this point in the history
  • Loading branch information
gunner272 committed Jun 2, 2014
2 parents 3fd650b + ee5bb21 commit 31abcfa
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
4 changes: 2 additions & 2 deletions sunpy/time/tests/test_time.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ def test_parse_time_tuple():
assert parse_time((1966, 2, 3)) == LANDING

def test_parse_time_int():
assert parse_time(765548612.0) == datetime(2003, 4, 5, 12, 23, 32)
assert parse_time(1009685652.0) == datetime(2010, 12, 30, 4, 14, 12)
assert parse_time(765548612.0,'utime') == datetime(2003, 4, 5, 12, 23, 32)
assert parse_time(1009685652.0,'utime') == datetime(2010, 12, 30, 4, 14, 12)

def test_parse_time_ISO():
assert parse_time('1966-02-03') == LANDING
Expand Down
46 changes: 23 additions & 23 deletions sunpy/time/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,34 +136,34 @@ def extract_time(string):
return bestmatch


def parse_time(time_string):
def parse_time(time_string, time_format=basestring):
"""Given a time string will parse and return a datetime object.
Similar to the anytim function in IDL.
utime -- Time since epoch 1 Jan 1979
Parameters
----------
time_string : string
Datestring to parse
time_string : [ int, float, time_string, datetime ]
Date to parse which can be either time_string, int, datetime object.
format : [ basestring, utime, datetime ]
Specifies the format user has provided the time_string in.
Returns
-------
out : datetime
DateTime corresponding to input date string
Note:
If time_string is an instance of float, then it is assumed to be in unix time format.
Examples
--------
>>> sunpy.time.parse_time('2012/08/01')
>>> sunpy.time.parse_time('2005-08-04T00:01:02.000Z')
.. todo::
add ability to parse tai (International Atomic Time seconds since
Jan 1, 1958)
"""
if isinstance(time_string, datetime):
if isinstance(time_string, datetime) or time_format == 'datetime':
return time_string
elif isinstance(time_string, tuple):
return datetime(*time_string)
elif isinstance(time_string, int) or isinstance(time_string, float):
elif time_format == 'utime' or ( isinstance(time_string, int) or isinstance(time_string, float) ) :
return datetime(1979, 1, 1) + timedelta(0, time_string)
else:
# remove trailing zeros and the final dot to allow any
Expand All @@ -185,37 +185,37 @@ def parse_time(time_string):
raise ValueError("%s is not a valid time string!" % time_string)


def is_time(time_string):
def is_time(time_string, time_format=basestring):
"""
Returns true if the input is a valid date/time representation
Parameters
----------
time_string : string
Datestring to parse
time_string : [ int, float, time_string, datetime ]
Date to parse which can be either time_string, int, datetime object.
format : [ basestring, utime, datetime ]
Specifies the format user has provided the time_string in.
Returns
-------
out : bool
True if can be parsed by parse_time
Note:
If time_string is an instance of float, then it is assumed to be in unix time format.
Examples
--------
>>> sunpy.time.parse_time('2012/08/01')
>>> sunpy.time.parse_time('2005-08-04T00:01:02.000Z')
.. todo::
add ability to parse tai (International Atomic Time seconds since Jan 1, 1958)
"""
if time_string is None:
return False
elif isinstance(time_string, datetime):
return True

try:
parse_time(time_string)
parse_time(time_string,time_format)
except ValueError:
return False
else:
Expand Down Expand Up @@ -250,16 +250,16 @@ def day_of_year(time_string):
time_diff = time - datetime(time.year, 1, 1, 0, 0, 0)
return time_diff.days + time_diff.seconds / SECONDS_IN_DAY + 1

def break_time(t=None):
def break_time(t=None, time_format=basestring):
"""Given a time returns a string. Useful for naming files."""
#TODO: should be able to handle a time range
return parse_time(t).strftime("%Y%m%d_%H%M%S")
return parse_time(t, time_format).strftime("%Y%m%d_%H%M%S")

def get_day(dt):
""" Return datetime for the beginning of the day of given datetime. """
return datetime(dt.year, dt.month, dt.day)

def is_time_in_given_format(time_string, time_format):
def is_time_in_given_format(time_string, time_format=basestring):
"""Tests whether a time string is formatted according to the given time format."""
try:
datetime.strptime(time_string, time_format)
Expand Down

0 comments on commit 31abcfa

Please sign in to comment.