Coverage for muuntaa/strftime.py: 100.00%

16 statements  

« prev     ^ index     » next       coverage.py v7.6.0, created at 2024-07-21 12:16:47 +00:00

1import datetime as dti 

2import logging 

3from typing import Union 

4 

5from muuntaa import NOW_CODE, ScopedMessages 

6 

7 

8def _line_slug(text: str) -> str: 

9 """Remove all new lines from text.""" 

10 return text.replace('\n', ' ').replace('\r', ' ') 

11 

12 

13def get_utc_timestamp(ts_text: str = NOW_CODE) -> tuple[Union[str, None], ScopedMessages]: 

14 """Returns an ordered pair of timestamp in UTC format and error (empty scoped messages no error). 

15 

16 If the magic timestamp text `now` is provided, then the current timestamp is returned. 

17 """ 

18 if ts_text == NOW_CODE: 

19 return dti.datetime.now(dti.timezone.utc).isoformat(timespec='milliseconds'), [] 

20 try: 

21 now = dti.datetime.fromisoformat(ts_text.replace('Z', '+00:00')) 

22 if now.tzinfo is None: 

23 now = now.replace(tzinfo=dti.timezone.utc) 

24 return now.isoformat(timespec='milliseconds'), [] 

25 except (TypeError, ValueError) as err: 

26 return None, [(logging.CRITICAL, f'invalid time stamp provided {ts_text}: {_line_slug(str(err))}.')]