Skip to content

Instantly share code, notes, and snippets.

@mishudark
Created March 27, 2011 17:25
Show Gist options
  • Select an option

  • Save mishudark/889391 to your computer and use it in GitHub Desktop.

Select an option

Save mishudark/889391 to your computer and use it in GitHub Desktop.
flexible date like '+2 days' 'friday' '+2 thursday'
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2011 mishudark <moonsadly@gmail.com><mishudark@astrata.com.mx>
#
# 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, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
import re
import datetime
"""
usage:
#some posible values
value = '+1 days'
value = '+5 months'
value = '+1 wednesday'
value = '+2 friday'
value = 'monday'
flexible_date(value)
#return a date format d-m-Y
"""
def flexible_date(value):
options = {
'month':'m', 'mes':'m',
'months':'m', 'meses':'m',
'year':'y', 'año':'y',
'years':'y', 'años':'y',
'day':'d', 'dia':'d',
'days':'d', 'dias':'d',
}
days = {
'monday':1, 'lunes':1,
'tuesday':2, 'martes':2,
'wednesday':3, 'miercoles':3,
'thursday':4, 'jueves':4,
'friday':5, 'viernes':5,
'saturday':6, 'sabado':6,
'sunday':7, 'domingo':7,
}
logical = {
'now':0, 'ahora':0,
'today':0, 'hoy':0,
'tomorrow':1, 'mañana':1,
}
value = value.replace('/','-')
#Regex
normal = '([0-9]{1,2}\-[0-9]{1,2}\-[0-9]{0,4})'
relative = '([\+\-]?[0-9]+\s\w+)'
date = re.findall(normal,value)
if(len(date)>0):
return date[0]
#find format like '+1 days'
date = re.findall(relative,value)
if(len(date)>0):
value = date[0]
[num,item] = value.split(' ')
num = int(num)
else:
item = value;
num = 1;
#get diference between now an relative date
today = datetime.date.today()
if item in days:
num += 1
if (num * 7 - 7 + days[item]) > today.weekday():
diff = num * 7 - 7 + days[item] - today.weekday() -1
else:
diff = num * 7 + days[item] - today.weekday() -1
#correct date
correct = today + datetime.timedelta(days = diff)
d = correct.day
m = correct.month
y = correct.year
date = "%d-%d-%d" % (d,m,y)
elif item in logical:
diff = logical[item]
#correct date
correct = today + datetime.timedelta(days = diff)
d = correct.day
m = correct.month
y = correct.year
date = "%d-%d-%d" % (d,m,y)
elif item in options:
d = today.day
m = today.month
y = today.year
if options[item] == 'm':
m = (m + num) % 12
y += int( (m + num -1) / 12 )
elif options[item] == 'y':
y += num
elif options[item] == 'd':
today += datetime.timedelta(days = num)
d = today.day
m = today.month
y = today.year
date = "%d-%d-%d" % (d,m,y)
else:
date = ''
return date
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment