Okay, here is the regreplace solution. I went ahead and decided to increment by days because that was the most complicated. You can use scale the logic down to increment by month or even year.
So first, we define the pattern. Since the regex engine that is being used is Python’s re regex engine, I used Python’s regex syntax to give each group a name. Because this is not a simple find and replace, we will have to use some procedural logic via a python module, so I don’t bother defining the replace, but instead define a plugin module to call. In this case, the plugin will require no arguments, so I do not bother defining any.:
[pre=#232628] “date_up”: {
“find”: “(?P\d{4})(?P\d{2})(?P\d{2})”,
“plugin”: “User.rr_modules.date_up”
}[/pre]
Now I created a folder in my “Users” folder called “rr_modules”. I then created a file called “date_up.py” inside “rr_modules”. Hopefully you are still with me. Inside of “date_up.py”, I added this code (notice the included tests in the comments):
[pre=#232628]SHORT_MONTH = 30
LONG_MONTH = 31
FEB_MONTH = 28
FEB_LEAP_MONTH = 29
JAN = 1
FEB = 2
MAR = 3
APR = 4
MAY = 5
JUN = 6
JUL = 7
AUG = 8
SEP = 9
OCT = 10
NOV = 11
DEC = 12
Test 1: 20140228
Test 2: 20141231
Test 3: 20140101
def is_leap_year(year):
return ((year % 4 == 0) and (year % 100 != 0)) or (year % 400 == 0)
def days_in_months(month, year):
days = LONG_MONTH
if month == FEB:
days = FEB_LEAP_MONTH if is_leap_year(year) else FEB_MONTH
elif month in SEP, APR, JUN, NOV]:
days = SHORT_MONTH
return days
def increment_by_day(day, month, year):
mdays = days_in_months(month, year)
if day == mdays:
day = 1
if month == DEC:
month = JAN
year += 1
else:
month += 1
else:
day += 1
return day, month, year
def replace(m):
g = m.groupdict()
year = int(g"year"].lstrip(“0”))
month = int(g"month"].lstrip(“0”))
day = int(g"day"].lstrip(“0”))
day, month, year = increment_by_day(day, month, year)
return "%04d%02d%02d" % (year, month, day)[/pre]
Finally, you create the rule in your Default.sublime-commands file (remove “find_only” argument if you don’t wish to be prompted):
[pre=#232628] // Convert tabs to spaces
{
“caption”: “Replace: Date Up”,
“command”: “reg_replace”,
“args”: {“replacements”: “date_up”], “find_only”: true}
},[/pre]
Then run. If you find any bugs, you will need to resolve them, but on initial testing, it seems to work fine. I didn’t bother making the regex to complex. I will actually let you sort out if you want expand the regex to reduce false positives or include other date formats.