Skip to content

feat: strftime function to output custom datetime strings - #244

Merged
tsutterley merged 2 commits into
mainfrom
dev
Aug 7, 2026
Merged

feat: strftime function to output custom datetime strings#244
tsutterley merged 2 commits into
mainfrom
dev

Conversation

@tsutterley

Copy link
Copy Markdown
Member

feat: timezone option added to to_string for local time outputs

feat: `timezone` option added to `to_string` for local time outputs
Copilot AI lite review requested due to automatic review settings August 6, 2026 23:30

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds additional string-formatting capabilities to the Timescale API, enabling timezone-aware string outputs via to_string and custom formatted outputs via a new strftime helper.

Changes:

  • Extend Timescale.to_string() with a timezone option for local/UTC/naive string rendering.
  • Introduce Timescale.strftime(format, ...) for custom datetime string formats.
  • Adjust Timescale.to_datetime() signature to accept additional keyword arguments.
Suppressed comments (2)

timescale/time.py:1040

  • to_string no longer forwards **kwargs to np.datetime_as_string (they’re passed into to_datetime and then ignored), which is a behavior/API regression vs. the previous implementation. This breaks callers that relied on formatting kwargs like casting=.
        # convert to datetime objects
        dtime = self.to_datetime(unit=unit, **kwargs)
        return np.datetime_as_string(dtime, unit=unit, timezone=timezone)

timescale/time.py:1055

  • strftime advertises **kwargs as “datetime formatting” args, but they’re only (indirectly) used for conversion via to_datetime (and most keys are ignored). Since this is a new API, prefer an explicit unit parameter and ensure the datetime64 array is converted at microsecond resolution before casting to datetime.datetime (Python can’t represent nanoseconds).
    def strftime(self, format: str, **kwargs):
        """
        Convert a ``Timescale`` object to a custom formatted string array

        Parameters
        ----------
        format: str
            formatting string for output string array
        **kwargs: dict
            keyword arguments for datetime formatting
        """
        # convert to datetime objects
        dtime = self.to_datetime(**kwargs).astype(datetime.datetime)
        return np.array([d.strftime(format) for d in dtime])

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread timescale/time.py
Comment on lines +1025 to +1055
def to_string(self, unit: str = "s", timezone="naive", **kwargs):
"""
Convert a ``Timescale`` object to a formatted string array

Parameters
----------
unit: str, default 's'
datetime unit for output string array
timezone: str, default 'naive'
timezone for output string array
**kwargs: dict
keyword arguments for datetime formatting
"""
return np.datetime_as_string(
self.to_datetime(unit=unit), unit=unit, **kwargs
)
# convert to datetime objects
dtime = self.to_datetime(unit=unit, **kwargs)
return np.datetime_as_string(dtime, unit=unit, timezone=timezone)

def strftime(self, format: str, **kwargs):
"""
Convert a ``Timescale`` object to a custom formatted string array

Parameters
----------
format: str
formatting string for output string array
**kwargs: dict
keyword arguments for datetime formatting
"""
# convert to datetime objects
dtime = self.to_datetime(**kwargs).astype(datetime.datetime)
return np.array([d.strftime(format) for d in dtime])
Comment thread timescale/time.py
return scale * np.array(self.MJD - delta_time_epochs, dtype=np.float64)

def to_datetime(self, unit="ns"):
def to_datetime(self, unit="ns", **kwargs):
@github-actions

github-actions Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Coverage

Tests Skipped Failures Errors Time
40 0 💤 0 ❌ 0 🔥 26m 23s ⏱️

@github-actions

github-actions Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

Coverage

Tests Skipped Failures Errors Time
40 0 💤 0 ❌ 0 🔥 21m 43s ⏱️

@github-actions

github-actions Bot commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Coverage

Tests Skipped Failures Errors Time
40 0 💤 0 ❌ 0 🔥 31m 4s ⏱️

@tsutterley
tsutterley merged commit 605ea70 into main Aug 7, 2026
6 of 8 checks passed
@tsutterley
tsutterley deleted the dev branch August 7, 2026 01:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants