### Install and Import tooltime Python Library Source: https://context7.com/sslivkoff/tooltime/llms.txt Instructions for installing the tooltime Python package using pip and importing the library into a Python script. This is the initial setup required before using any of the library's functions. ```python pip install tooltime ``` ```python import tooltime ``` -------------------------------- ### Create Timeperiods using Start/End or Start/Length in Python Source: https://context7.com/sslivkoff/tooltime/llms.txt Shows how to create time periods using the tooltime library. It covers creating timeperiods from a start timestamp and a duration (length), or from start and end timestamps. It also demonstrates creating a TimeperiodPair tuple. ```python import tooltime # Create timeperiod from start and length timeperiod = tooltime.create_timeperiod( start=1600000000, length=3600 # 1 hour in seconds ) print(timeperiod) # {'start': 1600000000, 'end': 1600003600} # Create timeperiod from start and end timeperiod2 = tooltime.create_timeperiod( start=1600000000, end=1600003600 ) print(timeperiod2) # {'start': 1600000000, 'end': 1600003600} # Create timeperiod with end and length timeperiod3 = tooltime.create_timeperiod( end=1600003600, length='1h' # Using timelength label ) print(timeperiod3) # {'start': 1600000000, 'end': 1600003600} # Create as tuple (TimeperiodPair) instead of dict timeperiod_pair = tooltime.create_timeperiod_pair( start=1600000000, length=3600 ) print(timeperiod_pair) # (1600000000, 1600003600) ``` -------------------------------- ### Create Time Period with Start and Length Source: https://context7.com/sslivkoff/tooltime/llms.txt Creates a time period object using a starting timestamp and a time length. The output includes the start and end timestamps in seconds since the epoch. Dependencies: tooltime library. ```python import tooltime timeperiod4 = tooltime.create_timeperiod( start='20200913_122640Z', length='2h' ) print(timeperiod4) # {'start': 1600000000, 'end': 1600007200} ``` -------------------------------- ### Create Time Periods Source: https://github.com/sslivkoff/tooltime/blob/main/README.md Functions for creating time periods defined by a start time and a length. Supported output formats include dictionaries, maps, and pairs (lists). These functions do not require external dependencies. ```Python def create_timeperiod(start, length): """Creates a time period as a dictionary with start and end times.""" pass def create_timeperiod_map(start, length): """Creates a time period as a map (dictionary) with start and end times.""" pass def create_timeperiod_pair(start, length): """Creates a time period as a pair (list) of start and end times.""" pass ``` -------------------------------- ### Generate Multiple Standardized Intervals Source: https://context7.com/sslivkoff/tooltime/llms.txt Generates a series of standardized time intervals. Can be used to create a sequence of future intervals starting from a given time, or intervals within a time window ending at a specific time. Dependencies: tooltime library. ```python import tooltime # Generate multiple standardized intervals intervals = tooltime.get_standard_intervals( interval_size='1h', start_time=1600000000, n_intervals=5 ) print([tooltime.timestamp_to_label(t) for t in intervals]) # ['20200913_120000Z', '20200913_130000Z', '20200913_140000Z', '20200913_150000Z', '20200913_160000Z'] # Generate intervals for a time window intervals_window = tooltime.get_standard_intervals( interval_size='15m', end_time=1600000000, window_size='1h' ) print([tooltime.timestamp_to_label(t) for t in intervals_window]) # ['20200913_110000Z', '20200913_111500Z', '20200913_113000Z', '20200913_114500Z', '20200913_120000Z'] ``` -------------------------------- ### Convert Timelength Representations in Python Source: https://github.com/sslivkoff/tooltime/blob/main/README.md Demonstrates various ways to convert `Timelength` representations to seconds, labels, clock format, and phrases using the `tooltime` library. It also shows the interconvertibility of different `Timelength` formats. ```python import tooltime tooltime.timelength_to_seconds('10m') > 600 tooltime.timelength_to_label(600) > '10m' tooltime.timelength_to_clock('10m') > '0:10:00' tooltime.timelength_to_phrase('10m') > '10 minutes' ( tooltime.timelength_to_seconds(600) == tooltime.timelength_to_seconds('10m') == tooltime.timelength_to_seconds('0:10:00') == tooltime.timelength_to_seconds('10 minutes') ) > True ``` -------------------------------- ### Convert Timelengths to Seconds and Various Formats using Python Source: https://context7.com/sslivkoff/tooltime/llms.txt Illustrates converting time durations (timelengths) between seconds, labels, clock format, human phrases, and timedelta objects. It showcases conversions from different input formats to seconds and vice versa. ```python import tooltime import datetime # Convert various timelength representations to seconds seconds = tooltime.timelength_to_seconds('10m') print(seconds) # 600 seconds_from_clock = tooltime.timelength_to_seconds('0:10:00') print(seconds_from_clock) # 600 seconds_from_phrase = tooltime.timelength_to_seconds('10 minutes') print(seconds_from_phrase) # 600 # Convert seconds to various formats label = tooltime.timelength_to_label(600) print(label) # '10m' clock = tooltime.timelength_to_clock(600) print(clock) # '0:10:00' phrase = tooltime.timelength_to_phrase(600) print(phrase) # '10 minutes' # Complex durations with multiple units complex_seconds = 90061 # 1 day + 1 hour + 1 minute + 1 second clock_phrase = tooltime.timelength_to_clock_phrase(complex_seconds) print(clock_phrase) # '1 days, 1:01:01' phrase_full = tooltime.timelength_to_phrase(complex_seconds) print(phrase_full) # '1 days, 1 hours, 1 minutes, 1 seconds' # Convert to timedelta timedelta = tooltime.timelength_to_timedelta(600) print(timedelta) # datetime.timedelta(seconds=600) # All representations are interconvertible assert ( tooltime.timelength_to_seconds(600) == tooltime.timelength_to_seconds('10m') == tooltime.timelength_to_seconds('0:10:00') == tooltime.timelength_to_seconds('10 minutes') == tooltime.timelength_to_seconds(datetime.timedelta(minutes=10)) ) ``` -------------------------------- ### Identify Time Frequency Types with tooltime Source: https://context7.com/sslivkoff/tooltime/llms.txt Demonstrates how to identify different types of time frequencies using tooltime, including interval-based and count-per-based frequencies. This is valuable for analyzing data collection rates. ```python import tooltime print(tooltime.is_timefrequency({'interval': 0.2})) # True print(tooltime.is_timefrequency_interval({'interval': 0.2})) # True print(tooltime.is_timefrequency_count_per({'count': 5, 'per': '1s'})) # True ``` -------------------------------- ### Convert Timestamps to Various Formats using Python Source: https://context7.com/sslivkoff/tooltime/llms.txt Demonstrates converting epoch seconds into various timestamp representations like labels, ISO strings, datetime objects, and date components using the tooltime library. It also shows conversion back to seconds from labels and ISO formats. ```python import tooltime import datetime # Convert epoch seconds to various formats timestamp_seconds = 1600000000 label = tooltime.timestamp_to_label(timestamp_seconds) print(label) # '20200913_122640Z' iso = tooltime.timestamp_to_iso(timestamp_seconds) print(iso) # '2020-09-13T12:26:40Z' iso_pretty = tooltime.timestamp_to_iso_pretty(timestamp_seconds) print(iso_pretty) # '2020-09-13 12:26:40Z' dt = tooltime.timestamp_to_datetime(timestamp_seconds) print(dt) # datetime.datetime(2020, 9, 13, 12, 26, 40, tzinfo=datetime.timezone.utc) # Convert label back to seconds seconds = tooltime.timestamp_to_seconds('20200913_122640Z') print(seconds) # 1600000000 # Convert ISO format to seconds seconds_from_iso = tooltime.timestamp_to_seconds('2020-09-13T12:26:40Z') print(seconds_from_iso) # 1600000000 # Extract date and time components date = tooltime.timestamp_to_date(timestamp_seconds) print(date) # '2020-09-13' year = tooltime.timestamp_to_year(timestamp_seconds) print(year) # '2020' month = tooltime.timestamp_to_month(timestamp_seconds) print(month) # '2020-09' # Generic conversion with explicit target representation result = tooltime.convert_timestamp( timestamp_seconds, to_representation='TimestampLabel' ) print(result) # '20200913_122640Z' ``` -------------------------------- ### Date Arithmetic with DateDelta for Months and Years Source: https://context7.com/sslivkoff/tooltime/llms.txt Shows how to perform date arithmetic involving months, quarters, and years using the DateDelta class from tooltime. This addresses limitations of datetime.timedelta for such calculations, correctly handling month ends. ```python import datetime from tooltime.timelength_utils import DateDelta start_date = datetime.datetime(2020, 1, 31, 12, 0, 0) # Add 1 month (handles end-of-month correctly) delta = DateDelta(months=1) new_date = start_date + delta print(new_date) # 2020-02-29 12:00:00 (Feb has only 29 days in 2020) # Add 3 months delta_3m = DateDelta(months=3) result = start_date + delta_3m print(result) # 2020-04-30 12:00:00 (April has only 30 days) # Add 1 year delta_year = DateDelta(years=1) result_year = start_date + delta_year print(result_year) # 2021-01-31 12:00:00 # Add quarters (1 quarter = 3 months) delta_quarter = DateDelta(quarters=2) result_quarter = start_date + delta_quarter print(result_quarter) # 2020-07-31 12:00:00 # Subtract DateDelta past_date = datetime.datetime(2020, 5, 31, 15, 30, 0) delta_sub = DateDelta(months=2) earlier = past_date - delta_sub print(earlier) # 2020-03-31 15:30:00 # Combine years, quarters, and months combined_delta = DateDelta(years=1, quarters=1, months=2) # Total: 1*12 + 1*3 + 2 = 17 months result_combined = start_date + combined_delta print(result_combined) # 2021-06-30 12:00:00 ``` -------------------------------- ### Analyze Timestamp Sequences Source: https://context7.com/sslivkoff/tooltime/llms.txt Provides tools to analyze sequences of timestamps, including generating comprehensive summaries of statistics like duration, resolution, and identifying missing or outlier timestamps. Dependencies: tooltime library. ```python import tooltime # Example timestamp sequence (simulated data collection) timestamps = [ 1610914720, # Every ~4 minutes 1610914960, 1610915200, 1610915440, 1610915680, 1610915920, 1610916160, 1610916400, ] # Print comprehensive summary tooltime.print_timestamp_summary(timestamps=timestamps) # Output: # n_t: 8 # n_unique: 8 # extent: # start: 20210117_201840Z (1610914720) # end: 20210117_205320Z (1610916400) # duration: 28 minutes (1680 s) # resolution: # median_dt: 4m # mean_dt: 240s # missing timestamps: 0 (if median_dt maintained) # outlier_dts: 0 # small: 0 dt < median_dt / (1 + outlier_rtol) # large: 0 dt > median_dt * (1 + outlier_rtol) # (outlier_rtol = 0.5) # Get summary as dict for programmatic use summary = tooltime.summarize_timestamps(timestamps) print(summary['duration_label']) # '28 minutes' print(summary['resolution']['label']) # '4m' print(summary['n_missing']) # 0 ``` -------------------------------- ### Summarize Timestamp Sequence in Python Source: https://github.com/sslivkoff/tooltime/blob/main/README.md Illustrates how to load timestamp data and then use `tooltime.print_timestamp_summary` to analyze and display statistics about the sequence of timestamps, including counts, extent, duration, and resolution. ```python # load data timestamps = load_data(...) # summarize sequence tooltime.print_timestamp_summary(timestamps) ``` -------------------------------- ### Detect Timelength Representation with tooltime Source: https://context7.com/sslivkoff/tooltime/llms.txt Shows how to detect the specific representation format of a given timelength string using the tooltime library. This is helpful for parsing and standardizing duration inputs. ```python import tooltime timelength_repr = tooltime.detect_timelength_representation('10m') print(timelength_repr) # 'TimelengthLabel' ``` -------------------------------- ### Generate Standardized Time Periods Source: https://context7.com/sslivkoff/tooltime/llms.txt Generates standardized time periods aligned to specific time units, useful for bucketing timestamps. Supports specifying block size and unit, or using a timelength label. Dependencies: tooltime library. ```python import tooltime # Get the 5-minute standardized period containing a timestamp timestamp = 1600000000 # 20200913_122640Z timeperiod = tooltime.get_standard_timeperiod( timestamp=timestamp, block_size=5, block_unit='minute' ) start_label = tooltime.timestamp_to_label(timeperiod['start']) end_label = tooltime.timestamp_to_label(timeperiod['end']) print(f"[{start_label}, {end_label}]") # ['20200913_122500Z', '20200913_122959Z'] # Get the 3-day standardized period timeperiod_3day = tooltime.get_standard_timeperiod( timestamp=timestamp, block_size=3, block_unit='day' ) start = tooltime.timestamp_to_label(timeperiod_3day['start']) end = tooltime.timestamp_to_label(timeperiod_3day['end']) print(f"[{start}, {end}]") # ['20200913_000000Z', '20200915_235959Z'] # Get the 1-hour standardized period using timelength_label timeperiod_1h = tooltime.get_standard_timeperiod( timestamp=timestamp, timelength_label='1h' ) print(timeperiod_1h) # {'start': 1600000000, 'end': 1600003599} ``` -------------------------------- ### Create Timestamps Source: https://github.com/sslivkoff/tooltime/blob/main/README.md Functions for creating timestamps in various formats like seconds, labels, ISO strings, and datetime objects. These functions do not require external dependencies. ```Python def create_timestamp_seconds(): """Returns the current timestamp in seconds.""" pass def create_timestamp_label(): """Returns the current timestamp as a label string (YYYYMMDD_HHMMSSZ).""" pass def create_timestamp_iso(): """Returns the current timestamp in ISO 8601 format.""" pass def create_timestamp_datetime(): """Returns the current timestamp as a datetime object.""" pass ``` -------------------------------- ### Identify Timelength Types with tooltime Source: https://context7.com/sslivkoff/tooltime/llms.txt Demonstrates the usage of tooltime functions to identify different representations of timelengths, such as labels, clock formats, and phrases. These functions are useful for validating input data that represents durations. ```python import tooltime print(tooltime.is_timelength('10m')) # True print(tooltime.is_timelength_label('10m')) # True print(tooltime.is_timelength_clock('0:10:00')) # True print(tooltime.is_timelength_phrase('10 minutes')) # True ``` -------------------------------- ### Create Standardized Timeperiods in Python Source: https://github.com/sslivkoff/tooltime/blob/main/README.md Shows how to generate time periods that are aligned to integer multiples of a specified `block_unit` (e.g., day, hour, minute) using `tooltime.get_standard_timeperiod`. This is useful for binning irregularly distributed timestamps. ```python timestamp = 1600000000 block_size = 3 print('timestamp:', tooltime.timestamp_to_label(timestamp)) print() print('standardized timeperiods containing timestamp:') for block_unit in ['day', 'hour', 'minute']: timeperiod = tooltime.get_standard_timeperiod( timestamp=timestamp, block_size=block_size, block_unit=block_unit, ) block_str = str(block_size) + ' ' + block_unit timeperiod_str = tooltime.timeperiod_to_str(timeperiod) print(' %8s' % block_str + ':', timeperiod_str) ``` -------------------------------- ### Identify Time Period Types with tooltime Source: https://context7.com/sslivkoff/tooltime/llms.txt Illustrates the use of tooltime functions for identifying different types of time periods, including dictionary-based periods and tuple pairs. This is useful for validating time ranges in data. ```python import tooltime period = {'start': 1600000000, 'end': 1600000001} print(tooltime.is_timeperiod(period)) # True print(tooltime.is_timeperiod_map(period)) # True period_pair = (1600000000, 1600000001) print(tooltime.is_timeperiod_pair(period_pair)) # True ``` -------------------------------- ### Convert Between Timefrequency Representations Source: https://context7.com/sslivkoff/tooltime/llms.txt Facilitates conversion between different representations of rates and frequencies, such as cycles per second (Hz), counts per interval, and intervals between occurrences. Dependencies: tooltime library. ```python import tooltime # Frequency: cycles per second (Hz) frequency = 5.0 # Convert frequency to count per interval count_per = tooltime.convert_timefrequency( frequency, to_representation='TimefrequencyCountPer', from_representation='TimefrequencyFrequency' ) print(count_per) # {'count': 5, 'per': '1s'} # Convert frequency to interval between occurrences interval = tooltime.convert_timefrequency( frequency, to_representation='TimefrequencyInterval', from_representation='TimefrequencyFrequency' ) print(interval) # {'interval': 0.2} # Convert count per back to frequency freq_result = tooltime.convert_timefrequency( {'count': 10, 'per': '2s'}, to_representation='TimefrequencyFrequency' ) print(freq_result) # 5.0 # Convert interval to count per count_per_result = tooltime.convert_timefrequency( {'interval': '15m'}, to_representation='TimefrequencyCountPer' ) print(count_per_result) # {'count': 1, 'per': '15m'} ``` -------------------------------- ### Create Time Frequencies Source: https://github.com/sslivkoff/tooltime/blob/main/README.md Functions for creating time frequencies based on count and period, or a specified interval. Supported outputs include float values, dictionaries, and interval objects. These functions do not require external dependencies. ```Python def create_timefrequency(count, per): """Creates a time frequency as a float (count/per).""" pass def create_timefrequency_frequency(count, per): """Returns the time frequency as a float (count/per).""" pass def create_timefrequency_count_per(count, per): """Returns time frequency details as a dictionary with count and per.""" pass def create_timefrequency_interval(interval): """Creates a time frequency object representing an interval.""" pass ``` -------------------------------- ### Create Time Lengths Source: https://github.com/sslivkoff/tooltime/blob/main/README.md Functions for creating time lengths from seconds, with options for different representations like seconds, precise seconds, labels, clock format, phrases, and timedelta objects. These functions do not require external dependencies. ```Python def create_timelength(seconds, to_representation): """Creates a time length representation from seconds.""" pass def create_timelength_seconds(seconds): """Returns the time length in seconds.""" pass def create_timelength_seconds_precise(seconds): """Returns the time length in precise seconds (float).""" pass def create_timelength_label(seconds): """Returns the time length as a label string (e.g., '310s').""" pass def create_timelength_clock(seconds): """Returns the time length in clock format (H:MM:SS).""" pass def create_timelength_phrase(seconds): """Returns the time length as a human-readable phrase.""" pass def create_timelength_clock_phrase(seconds): """Returns the time length in clock format as a phrase.""" pass def create_timelength_timedelta(seconds): """Returns the time length as a datetime.timedelta object.""" pass ``` -------------------------------- ### Convert Timelength Representations Source: https://github.com/sslivkoff/tooltime/blob/main/README.md Provides functions to convert timelengths between various formats such as timestamps, seconds, human-readable phrases, clock formats, and labels. These functions are essential for interpreting and manipulating time durations. ```Python convert_timelength(1600000000, 'TimestampLabel') timelength_to_seconds(610) timelength_to_phrase(610) timelength_to_clock(610) timelength_to_clock_phrase( 259810) timelength_seconds_to_label(600) timelength_seconds_to_clock(610) timelength_seconds_to_phrase(610) timelength_seconds_to_clock_phrase(600) timelength_seconds_to_timedelta( 259810) timelength_label_to_seconds( '10m') timelength_clock_to_seconds( '0:10:10') timelength_phrase_to_seconds('10 minutes') timelength_clock_phrase_to_seconds( '3 days, 0:05:10') timelength_timedelta_to_seconds( timedelta) ``` -------------------------------- ### Specialized Timestamp Functions Source: https://github.com/sslivkoff/tooltime/blob/main/README.md Provides specialized functions for Timestamp objects, including flooring to a specified unit of precision and retrieving the lowest possible value for a given time unit. It also includes a function to print summary statistics for iterables of Timestamps. ```Python floor_datetime(dt_object, unit) get_unit_lowest_value(unit) print_timestamp_summary(timestamp_iterable) ``` -------------------------------- ### Convert Timeperiod Representations Source: https://github.com/sslivkoff/tooltime/blob/main/README.md Enables conversion of timeperiod data between different formats, including start-end pairs, start-length, and end-length. These functions are useful for standardizing and analyzing time intervals. ```Python convert_timeperiod({'start': 1600000000, 'end': 1600000001}, 'TimePeriodStartLength') convert_timeperiod_to_start_end( {'start': 1600000000, 'end': 1600000001}, 'TimePeriodStartLength') convert_timeperiod_to_start_length( {'start': 1600000000, 'end': 1600000001}, 'TimePeriodStartLength') convert_timeperiod_to_end_length( {'start': 1600000000, 'end': 1600000001}, 'TimePeriodStartLength') ``` -------------------------------- ### Convert Timestamps Source: https://github.com/sslivkoff/tooltime/blob/main/README.md Functions for converting timestamps between various formats including seconds, labels, ISO strings, and datetime objects. These functions accept different input types and return specific output formats. Dependencies may include the datetime module. ```Python def convert_timestamp(timestamp, to_format): """Converts a timestamp to a specified format.""" pass def timestamp_to_seconds(timestamp): """Converts a timestamp (label or ISO) to seconds.""" pass def timestamp_to_label(timestamp): """Converts a timestamp (seconds or ISO) to a label string.""" pass def timestamp_to_iso(timestamp): """Converts a timestamp (seconds or label) to ISO 8601 format.""" pass def timestamp_to_datetime(timestamp): """Converts a timestamp (seconds or label or ISO) to a datetime object.""" pass def timestamp_seconds_to_label(seconds): """Converts seconds timestamp to a label string.""" pass def timestamp_label_to_seconds(label): """Converts a label timestamp to seconds.""" pass def timestamp_seconds_to_iso(seconds): """Converts seconds timestamp to ISO 8601 format.""" pass def timestamp_iso_to_seconds(iso_string): """Converts an ISO 8601 timestamp string to seconds.""" pass ``` -------------------------------- ### Convert Timefrequency Representations Source: https://github.com/sslivkoff/tooltime/blob/main/README.md Facilitates conversion of timefrequency data between various representations, such as intervals, counts per unit of time, and frequency. These functions are crucial for analyzing and standardizing event frequencies. ```Python convert_timefrequency({'count': 5, 'per': '1s'}, 'TimeInterval') convert_timefrequency_to_frequency( {'interval': 0.2}) convert_timefrequency_to_count_per( {'interval': 0.2}) convert_timefrequency_to_interval( {'count': 5, 'per': '1s'}) ``` -------------------------------- ### Identify Timestamp Data Types Source: https://context7.com/sslivkoff/tooltime/llms.txt Provides functions to detect and validate various time-related data type representations, including generic timestamps, timestamps in seconds, formatted labels, and ISO 8601 strings. Also allows for detecting the specific representation of a given timestamp. Dependencies: tooltime library, datetime module. ```python import tooltime import datetime # Identify timestamp types print(tooltime.is_timestamp(1600000000)) # True print(tooltime.is_timestamp('hello')) # False print(tooltime.is_timestamp_seconds(1600000000)) # True print(tooltime.is_timestamp_label('20200913_122640Z')) # True print(tooltime.is_timestamp_iso('2020-09-13T12:26:40Z')) # True # Detect specific representation repr_name = tooltime.detect_timestamp_representation(1600000000) print(repr_name) # 'TimestampSeconds' repr_label = tooltime.detect_timestamp_representation('20200913_122640Z') print(repr_label) # 'TimestampLabel' ``` -------------------------------- ### Specialized Timeperiod Functions Source: https://github.com/sslivkoff/tooltime/blob/main/README.md Offers specialized functions for Timeperiod objects, such as checking for overlaps, determining containment, creating superset or overlapping timeperiods, and standardizing timeperiod boundaries. ```Python timeperiods_overlap(tp1, tp2) timeperiod_contains(tp_outer, tp_inner) create_superset_timeperiod(timeperiod_list) create_overlapping_timeperiod(tp, trim_extend_amount, type) get_standard_timeperiod(tp, block_unit) ``` -------------------------------- ### Specialized Timefrequency Functions Source: https://github.com/sslivkoff/tooltime/blob/main/README.md Includes a specialized function for Timefrequency data to detect the resolution of an iterable of Timestamps. This is useful for understanding the granularity of time-series data. ```Python detect_resolution(timestamp_iterable) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.