Passed
Pull Request — master (#69)
by
unknown
02:32 queued 01:04
created

nextcloud.common.value_parsing   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 4
eloc 16
dl 0
loc 36
rs 10
c 0
b 0
f 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A datetime_to_expire_date() 0 2 1
A timestamp_to_epoch_time() 0 22 3
1
# -*- coding: utf-8 -*-
2
"""
3
Extra tools for value parsing
4
"""
5
from datetime import datetime
6
import os
7
from nextcloud.compat import datetime_to_timestamp
8
9
10
def datetime_to_expire_date(date):
11
    return date.strftime("%Y-%m-%d")
12
13
14
def timestamp_to_epoch_time(rfc1123_date=''):
15
    """
16
    literal date time string (use in DAV:getlastmodified) to Epoch time
17
18
    No longer, Only rfc1123-date productions are legal as values for DAV:getlastmodified
19
    However, the value may be broken or invalid.
20
21
    Args:
22
        rfc1123_date (str): rfc1123-date (defined in RFC2616)
23
    Return:
24
        int or None : Epoch time, if date string value is invalid return None
25
    """
26
    try:
27
        _tz = os.environ.get('TZ', '')
28
        os.environ['TZ'] = 'UTC'
29
        _time = datetime.strptime(
30
            rfc1123_date, '%a, %d %b %Y %H:%M:%S GMT')
31
        os.environ['TZ'] = _tz
32
    except ValueError:
33
        return
34
    else:
35
        return datetime_to_timestamp(_time)
36