Created
July 10, 2012 20:57
-
-
Save mlamina/3086190 to your computer and use it in GitHub Desktop.
DateTime formatter den ich mir fuer mein REST backend schreiben musste
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.http import HttpResponse | |
from django.http import Http404 | |
from statistics.models import Sysinfo, CPUUsage | |
import json | |
from datetime import datetime | |
import re | |
import pdb | |
class DatetimeFormatter: | |
date_formats = \ | |
{ \ | |
'\d{4}-\d{2}-\d{2}$' : '%Y-%m-%d', \ | |
'\d{4}-\d{2}$' : '%Y-%m', \ | |
'\d{2}-\d{2}-\d{2}$' : '%y-%m-%d', \ | |
'\d{8}$' : '%Y%m%d', \ | |
} | |
time_formats = \ | |
{ \ | |
'\d{2}:\d{2}:\d{2}$' : '%H:%M:%S', \ | |
'\d{2}:\d{2}$' : '%H:%M', \ | |
'\d{2}$' : '%H', \ | |
'\d{4}$' : '%H%M', \ | |
'\d{6}$' : '%H%M%S', \ | |
} | |
@staticmethod | |
def check_datetime_format(dt): | |
dt = dt.rstrip().lstrip() | |
# the splitter can be either a whitespace, a T or not there at all | |
splitter = '' | |
if ' ' in dt: | |
splitter = ' ' | |
elif 'T' in dt: | |
splitter = 'T' | |
# if time is given in UTC it ends with a Z | |
ending = 'Z' if ('Z' in dt) else '' | |
# format not supported | |
if (splitter == '' or splitter == ' ') and ending == 'Z': | |
raise ValueError('datetime ' + dt + ' is of unsupported format') | |
# if only one parameter is given it is either a date or a time | |
if splitter == '': | |
try: | |
f = DatetimeFormatter.check_date_format(dt) | |
except ValueError: | |
try: | |
f = DatetimeFormatter.check_time_format(dt) | |
except ValueError: | |
raise | |
return f | |
# array contains date and time, if both are given | |
date_time = dt.strip(ending).split(splitter) | |
if len(date_time) == 2: | |
try: | |
df = DatetimeFormatter.check_date_format(date_time[0]) | |
tf = DatetimeFormatter.check_time_format(date_time[1]) | |
return df + splitter + tf + ending | |
except ValueError: | |
raise | |
else: | |
raise ValueError('Datetime "' + dt + '" is of unsupported format.') | |
@staticmethod | |
def check_date_format(d): | |
# iterate supported formats | |
for regex in DatetimeFormatter.date_formats: | |
if re.match(regex, d): | |
return DatetimeFormatter.date_formats[regex] | |
raise ValueError('Date "' + d + '" is of unsupported format.') | |
@staticmethod | |
def check_time_format(t): | |
# iterate supported formats | |
for regex in DatetimeFormatter.time_formats: | |
if re.match(regex, t): | |
return DatetimeFormatter.time_formats[regex] | |
raise ValueError('Time "' + t + '" is of unsupported format.') | |
@staticmethod | |
def get_datetime_from_format(dt, f): | |
date_time = datetime.strptime(dt, f) | |
if f in DatetimeFormatter.time_formats.values(): | |
date_time = datetime.combine(datetime.today().date(), date_time.time()) | |
return date_time | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment