• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

GFZ / EL_PASO / 19631515375

24 Nov 2025 10:44AM UTC coverage: 66.034% (+31.2%) from 34.821%
19631515375

push

github

Bernhard Haas
Merge branch 'main' of github.com:GFZ/EL_PASO

1705 of 2582 relevant lines covered (66.03%)

1.32 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

60.0
/el_paso/processing/convert_string_to_datetime.py
1
# SPDX-FileCopyrightText: 2025 GFZ Helmholtz Centre for Geosciences
2
# SPDX-FileContributor: Bernhard Haas
3
#
4
# SPDX-License-Identifier: Apache-2.0
5

6
from datetime import datetime, timezone
2✔
7

8
import numpy as np
2✔
9
from dateutil import parser
2✔
10
from numpy.typing import NDArray
2✔
11

12
from el_paso import Variable
2✔
13

14

15
def convert_string_to_datetime(time_var: Variable, time_format: str | None = None) -> NDArray[np.generic]:
2✔
16
    """Converts a Variable's string-based time data to UTC datetime objects.
17

18
    This function transforms an array of time strings into Python datetime objects,
19
    automatically setting the timezone to UTC. If time_format is provided, it uses
20
    datetime.strptime for explicit parsing; otherwise, it uses a flexible parser
21
    (like dateutil.parser.parse) to infer the format.
22

23
    Args:
24
        time_var (Variable): The variable containing string-based time data to be
25
            converted. Its data is accessed via time_var.get_data().
26
        time_format (str | None): The explicit format string (e.g., "%Y-%m-%d %H:%M:%S")
27
            used to parse the time data. If None (default), the function uses a
28
            flexible parser to infer the correct format.
29

30
    Returns:
31
        NDArray[np.generic]: A NumPy array of Python datetime objects that are all
32
            localized to the UTC timezone.
33
    """
34
    time_var.metadata.add_processing_note("Converting string-time to datetime")
×
35

36
    if time_format is None:
×
37
        return np.asarray([parser.parse(t).replace(tzinfo=timezone.utc) for t in time_var.get_data()])
×
38

39
    return np.asarray([datetime.strptime(t, time_format).replace(tzinfo=timezone.utc) for t in time_var.get_data()])
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc