Completed
Push — master ( 419779...7515a9 )
by Charles
02:09
created

datetime_from_iso8601()   A

Complexity

Conditions 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
dl 0
loc 12
ccs 8
cts 8
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2 1
"""
3
    Date time helpers
4
5
    to convert easily date to ISO format and timestamp
6
"""
7 1
from datetime import datetime
8 1
import iso8601
0 ignored issues
show
Configuration introduced by
The import iso8601 could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
9 1
import pytz
0 ignored issues
show
Configuration introduced by
The import pytz could not be resolved.

This can be caused by one of the following:

1. Missing Dependencies

This error could indicate a configuration issue of Pylint. Make sure that your libraries are available by adding the necessary commands.

# .scrutinizer.yml
before_commands:
    - sudo pip install abc # Python2
    - sudo pip3 install abc # Python3
Tip: We are currently not using virtualenv to run pylint, when installing your modules make sure to use the command for the correct version.

2. Missing __init__.py files

This error could also result from missing __init__.py files in your module folders. Make sure that you place one file in each sub-folder.

Loading history...
10
11
12 1
def utcnow():
13
    """
14
        return current UTC date
15
    """
16 1
    return datetime.now(pytz.utc)
17
18
19 1
def datetime_from_iso8601(isodate, utc=False):
20
    """
21
        convert ISO 8601 date string to datetime
22
    """
23 1
    try:
24 1
        date = iso8601.parse_date(isodate)
25 1
        if utc:
26 1
            return date.astimezone(pytz.utc)
27
28 1
        return date
29 1
    except (AttributeError, iso8601.ParseError):
30 1
        return None
31
32
33 1
def iso8601_from_datetime(date):
34
    """
35
        convert datetime to ISO 8601 date string
36
    """
37 1
    try:
38 1
        return date.strftime('%Y-%m-%dT%H:%M:%S%z')
39 1
    except (AttributeError, iso8601.ParseError):
40 1
        return ''
41
42
43 1
def timestamp_from_datetime(date):
44
    """
45
        convert datetime to timestamp (UTC)
46
    """
47 1
    try:
48 1
        utc_date = date.replace(tzinfo=None) - date.utcoffset()
49 1
        return str(int((utc_date - datetime(1970, 1, 1)).total_seconds()))
50 1
    except (AttributeError, iso8601.ParseError):
51
        return ''
52