TimeRange¶
- class sunpy.time.TimeRange(a, b=None, format=None)[source]¶
Bases:
object
A class to create and handle time ranges.
Note
Regardless of how a
sunpy.time.TimeRange
is constructed it will always provide a positive time range where the start time is before the end time.__contains__
has been implemented which means you can check if a time is within the time range you have created. Please see the example section below.- Parameters
a (
tuple
,list
,str
,pandas.Timestamp
,pandas.Series
,pandas.DatetimeIndex
,datetime.datetime
,datetime.date
,numpy.datetime64
,numpy.ndarray
,astropy.time.Time
) – A time (the start time) specified as a parse_time-compatible time string, number, or a datetime object.b (
tuple
,list
,str
,pandas.Timestamp
,pandas.Series
,pandas.DatetimeIndex
,datetime.datetime
,datetime.date
,numpy.datetime64
,numpy.ndarray
,astropy.time.Time
) – Another time (the end time) specified as a parse_time-compatible time string, number, or a datetime object. May also be the size of the time range specified as a timedelta object, or aQuantity
.
Examples
>>> from sunpy.time import TimeRange >>> time_range = TimeRange('2010/03/04 00:10', '2010/03/04 00:20') >>> time_range = TimeRange(('2010/03/04 00:10', '2010/03/04 00:20')) >>> import astropy.units as u >>> time_range = TimeRange('2010/03/04 00:10', 400 * u.s) >>> TimeRange('2010/03/04 00:10', 400 * u.day) <sunpy.time.timerange.TimeRange object at ...> Start: 2010-03-04 00:10:00 End: 2011-04-08 00:10:00 Center:2010-09-20 00:10:00 Duration:400.0 days or 9600.0 hours or 576000.0 minutes or 34560000.0 seconds >>> time1 = '2014/5/5 12:11' >>> time2 = '2012/5/5 12:11' >>> time_range = TimeRange('2014/05/04 13:54', '2018/02/03 12:12') >>> time1 in time_range True >>> time2 in time_range False
Attributes Summary
Gets the center of the time range.
Gets the number of days elapsed.
Get the length of the time range.
Get the end time.
Get the number of hours elapsed.
Gets the number of minutes elapsed.
Gets the number of seconds elapsed.
Get the start time.
Methods Summary
extend
(dt_start, dt_end)Extend the time range forwards and backwards.
Return all partial days contained within the time range.
intersects
(other)Return
True
if this interval overlaps with other.next
()Shift the time range forward by the amount of time elapsed.
previous
()Shift the time range backward by the amount of time elapsed.
split
([n])Splits the time range into multiple equally sized parts.
window
(cadence, window)Split the time range up into a series of
TimeRange
that arewindow
long, with a cadence ofcadence
.Attributes Documentation
- center¶
Gets the center of the time range.
- Returns
astropy.time.Time
– The center time.
- days¶
Gets the number of days elapsed.
- Returns
astropy.units.Quantity
– The amount of days between the start and end time.
- dt¶
Get the length of the time range. Always a positive value.
- Returns
astropy.time.TimeDelta
– The difference between the start and the end time.
- end¶
Get the end time.
- Returns
astropy.time.Time
– The end time.
- hours¶
Get the number of hours elapsed.
- Returns
astropy.units.Quantity
– The amount of hours between the start and end time.
- minutes¶
Gets the number of minutes elapsed.
- Returns
astropy.units.Quantity
– The amount of minutes between the start and end time.
- seconds¶
Gets the number of seconds elapsed.
- Returns
astropy.units.Quantity
– The amount of seconds between the start and end time.
- start¶
Get the start time.
- Returns
astropy.time.Time
– The start time.
Methods Documentation
- extend(dt_start, dt_end)[source]¶
Extend the time range forwards and backwards.
- Parameters
dt_start (
astropy.time.TimeDelta
) – The amount to shift the start time.dt_end (
astropy.time.TimeDelta
) – The amount to shift the end time.
- intersects(other)[source]¶
Return
True
if this interval overlaps with other.- Parameters
other (sunpy.time.TimeRange) – Other
TimeRange
to check for intersection.
Notes
Both intervals are treated as closed, i.e., their endpoints are included.
- split(n=2)[source]¶
Splits the time range into multiple equally sized parts.
- Parameters
n (
int
, optional) – The number of times to split the time range (must >= 1). Defaults to 2.- Returns
list
– A list of equally sizedsunpy.time.TimeRange
between the start and end times.
- window(cadence, window)[source]¶
Split the time range up into a series of
TimeRange
that arewindow
long, with a cadence ofcadence
.- Parameters
cadence (
astropy.units.Quantity
,astropy.time.TimeDelta
) – Cadence.window (
astropy.units.quantity
,astropy.time.TimeDelta
) – The length of window.
- Returns
list
– A list ofTimeRange
, that arewindow
long and separated bycadence
.
Examples
>>> import astropy.units as u >>> from sunpy.time import TimeRange >>> time_range = TimeRange('2010/03/04 00:10', '2010/03/04 01:20') >>> time_range.window(60*60*u.s, window=12*u.s) [ <sunpy.time.timerange.TimeRange object at 0x7f0214bfc208> Start: 2010-03-04 00:10:00 End: 2010-03-04 00:10:12 Center:2010-03-04 00:10:06 Duration:0.0001388888888888889 days or 0.003333333333333333 hours or 0.2 minutes or 12.0 seconds, <sunpy.time.timerange.TimeRange object at 0x7f01fe43ac50> Start: 2010-03-04 01:10:00 End: 2010-03-04 01:10:12 Center:2010-03-04 01:10:06 Duration:0.0001388888888888889 days or 0.003333333333333333 hours or 0.2 minutes or 12.0 seconds, <sunpy.time.timerange.TimeRange object at 0x7f01fb90b898> Start: 2010-03-04 02:10:00 End: 2010-03-04 02:10:12 Center:2010-03-04 02:10:06 Duration:0.0001388888888888889 days or 0.003333333333333333 hours or 0.2 minutes or 12.0 seconds]