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

spedas / pyspedas / 25194239086

26 Apr 2026 08:07PM UTC coverage: 61.697% (-28.8%) from 90.54%
25194239086

push

github

jameswilburlewis
Added test for loading Cluster CODIF differential energy flux

0 of 7 new or added lines in 1 file covered. (0.0%)

19460 existing lines in 418 files now uncovered.

30204 of 48955 relevant lines covered (61.7%)

1.44 hits per line

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

86.67
/pyspedas/tplot_tools/time_string.py
1
"""
2
Transform datetimes from decimal to string.
3
"""
4
from datetime import datetime, timezone
3✔
5
from pyspedas.tplot_tools import time_float
3✔
6

7
def time_string_one(float_time=None, fmt=None):
3✔
8
    """
9
    Transforms a single float daytime value into a string representation.
10

11
    Parameters
12
    ----------
13
    float_time : float, optional
14
        The input time as a float. If not provided, the current time is used. If input is actually a string,
15
        it is returned as-is.
16
    fmt : str, optional
17
        The format string for time conversion. The default format is '%Y-%m-%d %H:%M:%S.%f'.
18

19
    Returns
20
    -------
21
    str
22
        The datetime as a string formatted according to `fmt`. If `float_time` is not provided,
23
        returns the current datetime formatted as specified.
24

25
    Examples
26
    --------
27
    >>> from pyspedas import time_string_one
28
    >>> time_string_one(1679745600.0)
29
    '2023-03-25 12:00:00.000000'
30

31
    >>> time_string_one(1679745600.0, '%Y-%m-%d')
32
    '2023-03-25'
33

34
    >>> time_string_one()
35
    # Returns the current datetime in the default format
36
    """
37

38
    if fmt is None:
3✔
39
        fmt = '%Y-%m-%d %H:%M:%S.%f'
3✔
40

41
    if float_time is None:
3✔
UNCOV
42
        str_time = datetime.now().strftime(fmt)
×
43
    elif isinstance(float_time, str):
3✔
44
        # It's already a string, just return the input
45
        return float_time
×
46
    else:
47
        str_time = datetime.fromtimestamp(float_time,timezone.utc).strftime(fmt)
3✔
48

49
    return str_time
3✔
50

51

52
def time_string(float_time=None, fmt=None):
3✔
53
    """
54
    Transforms a list of float daytime values into a list of string representations.
55

56
    Parameters
57
    ----------
58
    float_time : floats, or list of floats, optional
59
        The input time(s) as float(s). If not provided, the current time is used. Any string inputs are returned as-is.
60
    fmt : str, optional
61
        The format string for time conversion. The default format is '%Y-%m-%d %H:%M:%S.%f'.
62

63
    Returns
64
    -------
65
    str or list of str
66
        The datetimes as strings formatted according to `fmt`. If `float_time` is not provided, returns
67
        the current datetime formatted as specified. If `float_time` is a single float, returns
68
        a single datetime string.
69

70
    Examples
71
    --------
72
    >>> time_string(1679745600.0)
73
    '2023-03-25 12:00:00.000000'
74

75
    >>> time_string([1679745600.0, 1679832000.0], "%Y-%m-%d %H:%M:%S")
76
    ['2023-03-25 12:00:00', '2023-03-26 12:00:00']
77

78
    >>> time_string()
79
    #'Returns the current datetime in the default format'
80

81
    Notes
82
    -----
83
    Compare to https://www.epochconverter.com/
84
    """
85
    if float_time is None:
3✔
UNCOV
86
        return time_string_one(None, fmt)
×
87
    else:
88
        if isinstance(float_time, (int, float)):
3✔
89
            return time_string_one(float_time, fmt)
3✔
90
        else:
91
            time_list = list()
3✔
92
            for t in float_time:
3✔
93
                time_list.append(time_string_one(t, fmt))
3✔
94
            return time_list
3✔
95

96

97
def time_datetime(time=None, tz=None):
3✔
98
    """
99
    Transforms a list of float daytime values or strings to a list of pythonic `datetime.datetime` objects.
100

101
    Parameters
102
    ----------
103
    time : float, list of floats, str, or list of strs, optional
104
        The input time(s) as float(s) representing UNIX timestamps or strings.
105
        If not provided, the current time is used.
106
    tz : datetime.timezone, optional
107
        The timezone for the datetime object(s).
108
        If not provided, UTC is used.
109

110
    Returns
111
    -------
112
    datetime.datetime or list of datetime.datetime
113
        The datetimes as `datetime.datetime` objects. If `time` is a single value, returns a single `datetime.datetime` object;
114
        if `time` is a list, returns a list of `datetime.datetime` objects. If `time` is None, returns the current datetime in UTC.
115

116
    Examples
117
    --------
118
    >>> from pyspedas import time_datetime
119
    >>> from datetime import timezone, timedelta
120
    >>> time_datetime(1679745600.0)
121
    # or
122
    >>> time_datetime('2023-03-25 12:00:00')
123
    # Returns a datetime object for 2023-03-25 12:00:00 UTC
124
    datetime.datetime(2023, 3, 25, 12, 0, tzinfo=datetime.timezone.utc)
125

126
    >>> time_datetime([1679745600.0, 1679832000.0])
127
    # or
128
    >>> time_datetime(['2023-03-25 12:00:00', '2023-03-26 12:00:00'])
129
    # Returns a list of datetime objects for 2023-03-25 12:00:00 UTC and 2023-03-26 12:00:00 UTC
130
    [datetime.datetime(2023, 3, 25, 12, 0, tzinfo=datetime.timezone.utc), datetime.datetime(2023, 3, 26, 12, 0, tzinfo=datetime.timezone.utc)]
131

132
    >>> time_datetime()
133
    # Returns the current datetime object in UTC
134

135
    >>> time_datetime(1679745600.0, tz=timezone(timedelta(hours=-6)))
136
    # Returns a datetime object for 2023-03-25 06:00:00 in a timezone that is -6 hour from UTC
137
    datetime.datetime(2023, 3, 25, 6, 0, tzinfo=datetime.timezone(datetime.timedelta(days=-1, seconds=64800)))
138

139
    """
140

141
    if tz is None:
3✔
142
        tz = timezone.utc
3✔
143

144
    if time is None:
3✔
UNCOV
145
        return datetime.now()
×
146

147
    if isinstance(time, str):
3✔
148
        return time_datetime(time_float(time))
3✔
149

150
    if isinstance(time, (int, float)):
3✔
151
        return datetime.fromtimestamp(time, tz=tz)
3✔
152

153
    return [time_datetime(_time) for _time in time]
3✔
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

© 2026 Coveralls, Inc