Last active
June 11, 2024 11:16
-
-
Save maxfire2008/096fd5f55c9d79a11d41769d58e8bca1 to your computer and use it in GitHub Desktop.
Convert rruleset to json and back
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
# rruleset_json.py by Max 2022 | |
# To the extent possible under law, the person who associated CC0 with | |
# rruleset_json.py has waived all copyright and related or neighboring rights | |
# to rruleset_json.py. | |
# You should have received a copy of the CC0 legalcode along with this | |
# work. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>. | |
# https://gist.github.com/maxfire2008/096fd5f55c9d79a11d41769d58e8bca1 | |
import json | |
import dateutil.rrule | |
import datetime | |
def rruleset_to_json(rruleset): | |
""" | |
Convert a rruleset to a JSON string. | |
""" | |
return json.dumps({ | |
'rrule': [str(rrule) for rrule in rruleset._rrule], | |
'rdate': [rdate.isoformat() for rdate in rruleset._rdate], | |
'exrule': [str(exrule) for exrule in rruleset._exrule], | |
'exdate': [exdate.isoformat() for exdate in rruleset._exdate], | |
}) | |
def json_to_rruleset(json_string): | |
""" | |
Convert a JSON string to a rruleset. | |
""" | |
if type(json_string) == str: | |
data = json.loads(json_string) | |
else: | |
data = json_string | |
rruleset = dateutil.rrule.rruleset() | |
rruleset._rrule = [dateutil.rrule.rrulestr( | |
rrule) for rrule in data['rrule']] | |
rruleset._rdate = [datetime.datetime.fromisoformat( | |
rdate) for rdate in data['rdate']] | |
rruleset._exrule = [dateutil.rrule.rrulestr( | |
exrule) for exrule in data['exrule']] | |
rruleset._exdate = [datetime.datetime.fromisoformat( | |
exdate) for exdate in data['exdate']] | |
return rruleset | |
if __name__ == "__main__": | |
from dateutil.rrule import * | |
from dateutil.parser import * | |
import datetime | |
print("Daily, for 7 days, jumping Saturday and Sunday occurrences.") | |
set = rruleset() | |
set.rrule(rrule(DAILY, count=7, dtstart=parse("19970902T090000"))) | |
set.exrule(rrule(YEARLY, byweekday=(SA, SU), | |
dtstart=parse("19970902T090000"))) | |
rrule_as_json = rruleset_to_json(set) | |
converted = json_to_rruleset(rrule_as_json) | |
print(rrule_as_json) | |
print(set[:10]) | |
print(converted[:10]) | |
print(set[:10] == converted[:10]) | |
print("Weekly, for 4 weeks, plus one time on day 7, and not on day 16.") | |
set = rruleset() | |
set.rrule(rrule(WEEKLY, count=4, dtstart=parse("19970902T090000"))) | |
set.rdate(datetime.datetime(1997, 9, 7, 9, 0)) | |
set.exdate(datetime.datetime(1997, 9, 16, 9, 0)) | |
rrule_as_json = rruleset_to_json(set) | |
converted = json_to_rruleset(rrule_as_json) | |
print(rrule_as_json) | |
print(set[:10]) | |
print(converted[:10]) | |
print(set[:10] == converted[:10]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment