# w.c.s. - web application for online forms
# Copyright (C) 2005-2017  Entr'ouvert
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.

from django import template
from django.utils import dateparse

register = template.Library()

@register.filter
def get(mapping, key):
    return mapping.get(key)

@register.filter
def parse_date(date_string):
    try:
        return dateparse.parse_date(date_string)
    except ValueError:
        return None

@register.filter
def parse_datetime(datetime_string):
    try:
        return dateparse.parse_datetime(datetime_string)
    except ValueError:
        return None

@register.filter
def parse_time(time_string):
    try:
        return dateparse.parse_time(time_string)
    except ValueError:
        return None
