forked from chihacknight/civic-json-worker
-
Notifications
You must be signed in to change notification settings - Fork 53
/
utils.py
38 lines (25 loc) · 1.03 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from datetime import datetime
def is_safe_name(name):
''' Return True if the string is a safe name.
'''
return raw_name(safe_name(name)) == name
def safe_name(name):
''' Return URL-safe organization name with spaces replaced by dashes.
Slashes will be removed, which is incompatible with raw_name().
'''
return name.replace(' ', '-').replace('/', '-').replace('?', '-').replace('#', '-')
def raw_name(name):
''' Return raw organization name with dashes replaced by spaces.
Also replace old-style underscores with spaces.
'''
return name.replace('_', ' ').replace('-', ' ')
def convert_datetime_to_iso_8601(dt):
''' Convert the passed datetime object to ISO 8601 format
'''
if not dt or type(dt) is not datetime:
return None
iso_string = unicode(dt.isoformat())
# add a 'Z' (representing the UTC time zone) to the end if there's no explicit time zone set
if not dt.tzinfo:
iso_string = u'{}Z'.format(iso_string.rstrip(u'Z'))
return iso_string