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:

Examples

>>> import numpy as np
>>> from astropy.time import TimeDelta
>>> import astropy.units as u
>>> 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'))
>>> 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
>>> time_range = TimeRange('2014/05/05 12:00', '2014/05/10 12:00')
>>> time_arr = np.arange(time_range.start, time_range.end, TimeDelta(24*60*60, format = "sec"))
>>> time_arr
array([<Time object: scale='utc' format='isot' value=2014-05-05T12:00:00.000>,
   <Time object: scale='utc' format='isot' value=2014-05-06T12:00:00.000>,
   <Time object: scale='utc' format='isot' value=2014-05-07T12:00:00.000>,
   <Time object: scale='utc' format='isot' value=2014-05-08T12:00:00.000>,
   <Time object: scale='utc' format='isot' value=2014-05-09T12:00:00.000>],
  dtype=object)
>>> time_arr[np.logical_and(time_arr > time_range.start, time_arr < time_range.end)]
array([<Time object: scale='utc' format='isot' value=2014-05-06T12:00:00.000>,
   <Time object: scale='utc' format='isot' value=2014-05-07T12:00:00.000>,
   <Time object: scale='utc' format='isot' value=2014-05-08T12:00:00.000>,
   <Time object: scale='utc' format='isot' value=2014-05-09T12:00:00.000>],
  dtype=object)

Attributes Summary

center

Gets the center of the time range.

days

Gets the number of days elapsed.

dt

Get the length of the time range.

end

Get the end time.

hours

Get the number of hours elapsed.

minutes

Gets the number of minutes elapsed.

seconds

Gets the number of seconds elapsed.

start

Get the start time.

Methods Summary

extend(dt_start, dt_end)

Extend the time range forwards and backwards.

get_dates()

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 are window long, with a cadence of cadence.

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:
get_dates()[source]#

Return all partial days contained within the time range.

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.

next()[source]#

Shift the time range forward by the amount of time elapsed.

previous()[source]#

Shift the time range backward by the amount of time elapsed.

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 sized sunpy.time.TimeRange between the start and end times.

window(cadence, window)[source]#

Split the time range up into a series of TimeRange that are window long, with a cadence of cadence.

Parameters:
Returns:

list – A list of TimeRange, that are window long and separated by cadence.

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]