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

SwissDataScienceCenter / renku-python / 9058668052

13 May 2024 07:05AM UTC coverage: 77.713% (-8.4%) from 86.115%
9058668052

Pull #3727

github

web-flow
Merge 128d38387 into 050ed61bf
Pull Request #3727: fix: don't fail session launch when gitlab couldn't be reached

15 of 29 new or added lines in 3 files covered. (51.72%)

2594 existing lines in 125 files now uncovered.

23893 of 30745 relevant lines covered (77.71%)

3.2 hits per line

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

77.78
/renku/core/util/datetime8601.py
1
# Copyright Swiss Data Science Center (SDSC). A partnership between
2
# École Polytechnique Fédérale de Lausanne (EPFL) and
3
# Eidgenössische Technische Hochschule Zürich (ETHZ).
4
#
5
# Licensed under the Apache License, Version 2.0 (the "License");
6
# you may not use this file except in compliance with the License.
7
# You may obtain a copy of the License at
8
#
9
#     http://www.apache.org/licenses/LICENSE-2.0
10
#
11
# Unless required by applicable law or agreed to in writing, software
12
# distributed under the License is distributed on an "AS IS" BASIS,
13
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
# See the License for the specific language governing permissions and
15
# limitations under the License.
16
"""Renku datetime utilities."""
7✔
17

18
import re
7✔
19
from datetime import datetime, timezone
7✔
20
from typing import Optional
7✔
21

22
regex = (
7✔
23
    r"^(-?(?:[1-9][0-9]*)?[0-9]{4})-(1[0-2]|0[1-9])-(3[01]|0[1-9]|[12]["
24
    r"0-9])T(2[0-3]|[01][0-9]):([0-5][0-9]):([0-5][0-9])(\.[0-9]+)?(Z|["
25
    r"+-](?:2[0-3]|[01][0-9]):[0-5][0-9])?$"
26
)
27
match_iso8601 = re.compile(regex).match
7✔
28

29

30
def validate_iso8601(str_val):
7✔
31
    """Check if datetime string is in ISO8601 format."""
32
    try:
×
33
        if match_iso8601(str_val) is not None:
×
34
            return True
×
35
    except re.error:
×
36
        pass
×
37
    return False
×
38

39

40
def parse_date(value):
7✔
41
    """Convert date to datetime."""
42
    from dateutil.parser import parse as date_util_parse_date
7✔
43

44
    if value is None:
7✔
45
        return
7✔
46
    if isinstance(value, datetime):
5✔
47
        date = value
5✔
48
    else:
49
        date = date_util_parse_date(value)
×
50

51
    if not date.tzinfo:
5✔
UNCOV
52
        date = _set_to_local_timezone(date)
×
53

54
    return date
5✔
55

56

57
def fix_datetime(value) -> Optional[datetime]:
7✔
58
    """Fix timezone of non-aware datetime objects and remove microseconds."""
59
    if value is None:
7✔
60
        return None
6✔
61

62
    if isinstance(value, datetime):
7✔
63
        if not value.tzinfo:
7✔
64
            value = _set_to_local_timezone(value)
3✔
65

66
    return value
7✔
67

68

69
def _set_to_local_timezone(value):
7✔
70
    local_tz = local_now().tzinfo
3✔
71
    return value.replace(tzinfo=local_tz)
3✔
72

73

74
def local_now(remove_microseconds: bool = False) -> datetime:
7✔
75
    """Return current datetime in local timezone."""
76
    now = datetime.now(timezone.utc).astimezone()
7✔
77
    return now.replace(microsecond=0) if remove_microseconds else now
7✔
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