Skip to content

Commit

Permalink
Merge pull request #450 from collective/issue-318-skip-value-paramete…
Browse files Browse the repository at this point in the history
…r-for-default-date-time

Remove VALUE=DATE-TIME from DTSTART, DTEND, DTSTAMP
  • Loading branch information
mauritsvanrees committed Nov 3, 2022
2 parents 78db695 + 173948c commit 5dae2a4
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 30 deletions.
3 changes: 3 additions & 0 deletions CHANGES.rst
Expand Up @@ -11,6 +11,9 @@ Minor changes:
Ref: #473
Fixes: #472
[jacadzaca]
- Make datetime value shorter by removing the value parameter where possible.
Fixes: #318
[jacadzaca], [niccokunzmann]

Breaking changes:

Expand Down
4 changes: 2 additions & 2 deletions docs/usage.rst
Expand Up @@ -239,7 +239,7 @@ identifier (if applicable) are automatically added here::

>>> lines = event.to_ical().splitlines()
>>> assert (
... b"DTSTART;TZID=Europe/Vienna;VALUE=DATE-TIME:20101010T100000"
... b"DTSTART;TZID=Europe/Vienna:20101010T100000"
... in lines)


Expand Down Expand Up @@ -314,7 +314,7 @@ Write to disk::
>>> directory = tempfile.mkdtemp()
>>> f = open(os.path.join(directory, 'example.ics'), 'wb')
>>> f.write(cal.to_ical())
570
522
>>> f.close()


Expand Down
12 changes: 5 additions & 7 deletions src/icalendar/prop.py
Expand Up @@ -289,20 +289,18 @@ def __init__(self, dt):
raise ValueError('You must use datetime, date, timedelta, '
'time or tuple (for periods)')
if isinstance(dt, datetime):
self.params = Parameters({'value': 'DATE-TIME'})
self.params = Parameters()
elif isinstance(dt, date):
self.params = Parameters({'value': 'DATE'})
elif isinstance(dt, time):
self.params = Parameters({'value': 'TIME'})
elif isinstance(dt, tuple):
self.params = Parameters({'value': 'PERIOD'})

if (isinstance(dt, datetime) or isinstance(dt, time))\
and getattr(dt, 'tzinfo', False):
tzinfo = dt.tzinfo
tzid = tzid_from_dt(dt)
if tzid != 'UTC':
self.params.update({'TZID': tzid})
tzid = tzid_from_dt(dt) if isinstance(dt, (datetime, time)) else None
if not tzid is None and tzid != 'UTC':
self.params.update({'TZID': tzid})

self.dt = dt

def to_ical(self):
Expand Down
Expand Up @@ -6,16 +6,16 @@ X-WR-CALNAME:äöü ÄÖÜ €
X-WR-RELCALID:12345
BEGIN:VEVENT
SUMMARY:Non-ASCII Test: ÄÖÜ äöü €
DTSTART;VALUE=DATE-TIME:20101010T100000Z
DTEND;VALUE=DATE-TIME:20101010T120000Z
DTSTART:20101010T100000Z
DTEND:20101010T120000Z
UID:123456
CREATED;VALUE=DATE-TIME:20101010T000000Z
CREATED:20101010T000000Z
DESCRIPTION:icalendar should be able to de/serialize non-ascii.
LOCATION:Tribstrül
END:VEVENT
BEGIN:VEVENT
SUMMARY:åäö
DTSTART;VALUE=DATE-TIME:20101010T000000Z
DTSTART:20101010T000000Z
END:VEVENT
BEGIN:VEVENT
DESCRIPTION:äöüßÄÖÜ
Expand Down
27 changes: 27 additions & 0 deletions src/icalendar/tests/test_issue_318_skip_default_parameters.py
@@ -0,0 +1,27 @@
"""Some parameters are specified as default by the RFC 5545.
These tests make sure that these parameter values are not added to the
properties.
Example:
DTSTART;VALUE=DATE-TIME:20190616T050000Z
equals DTSTART:20190616T050000Z
"""
import pytest
from icalendar import Event
from datetime import datetime


@pytest.mark.parametrize("attr", [
"DTSTART",
"DTEND",
"DTSTAMP",
])
def test_datetime_in_event(attr):
"""Check that the "VALUE=DATE-TIME" is absent because not needed."""
event = Event()
event.add(attr, datetime(2022, 10, 13, 9, 16, 42))
ics = event.to_ical()
assert b"VALUE=DATE-TIME" not in ics

4 changes: 2 additions & 2 deletions src/icalendar/tests/test_parsing.py
Expand Up @@ -124,8 +124,8 @@ def test_timezones_to_ical_is_inverse_of_from_ical(timezones):
assert timezone.to_ical() == timezone.raw_ics

@pytest.mark.parametrize('date, expected_output', [
(datetime(2012, 7, 16, 0, 0, 0), b'DTSTART;VALUE=DATE-TIME:20120716T000000Z'),
(datetime(2021, 11, 17, 15, 9, 15), b'DTSTART;VALUE=DATE-TIME:20211117T150915Z')
(datetime(2012, 7, 16, 0, 0, 0), b'DTSTART:20120716T000000Z'),
(datetime(2021, 11, 17, 15, 9, 15), b'DTSTART:20211117T150915Z')
])
def test_no_tzid_when_utc(utc, date, expected_output):
'''Issue #58 - TZID on UTC DATE-TIMEs
Expand Down
20 changes: 10 additions & 10 deletions src/icalendar/tests/test_timezoned.py
Expand Up @@ -149,21 +149,21 @@ def test_create_to_ical_pytz(self):
test_out = test_out.decode('utf-8')

vtimezone_lines = "BEGIN:VTIMEZONE|TZID:Europe/Vienna|X-LIC-LOCATION:"
"Europe/Vienna|BEGIN:STANDARD|DTSTART;VALUE=DATE-TIME:19701025T03"
"Europe/Vienna|BEGIN:STANDARD|DTSTART:19701025T03"
"0000|RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10|RRULE:FREQ=YEARLY;B"
"YDAY=-1SU;BYMONTH=3|TZNAME:CET|TZOFFSETFROM:+0200|TZOFFSETTO:+01"
"00|END:STANDARD|BEGIN:DAYLIGHT|DTSTART;VALUE=DATE-TIME:19700329T"
"00|END:STANDARD|BEGIN:DAYLIGHT|DTSTART:19700329T"
"020000|TZNAME:CEST|TZOFFSETFROM:+0100|TZOFFSETTO:+0200|END:DAYLI"
"GHT|END:VTIMEZONE"
self.assertTrue(vtimezone_lines in test_out)

test_str = "DTSTART;TZID=Europe/Vienna;VALUE=DATE-TIME:20120213T100000"
test_str = "DTSTART;TZID=Europe/Vienna:20120213T100000"
self.assertTrue(test_str in test_out)
self.assertTrue("ATTENDEE:sepp" in test_out)

# ical standard expects DTSTAMP and CREATED in UTC
self.assertTrue("DTSTAMP;VALUE=DATE-TIME:20101010T081010Z" in test_out)
self.assertTrue("CREATED;VALUE=DATE-TIME:20101010T081010Z" in test_out)
self.assertTrue("DTSTAMP:20101010T081010Z" in test_out)
self.assertTrue("CREATED:20101010T081010Z" in test_out)

def test_create_to_ical_zoneinfo(self):
cal = icalendar.Calendar()
Expand Down Expand Up @@ -232,21 +232,21 @@ def test_create_to_ical_zoneinfo(self):
test_out = test_out.decode('utf-8')

vtimezone_lines = "BEGIN:VTIMEZONE|TZID:Europe/Vienna|X-LIC-LOCATION:"
"Europe/Vienna|BEGIN:STANDARD|DTSTART;VALUE=DATE-TIME:19701025T03"
"Europe/Vienna|BEGIN:STANDARD|DTSTART:19701025T03"
"0000|RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10|RRULE:FREQ=YEARLY;B"
"YDAY=-1SU;BYMONTH=3|TZNAME:CET|TZOFFSETFROM:+0200|TZOFFSETTO:+01"
"00|END:STANDARD|BEGIN:DAYLIGHT|DTSTART;VALUE=DATE-TIME:19700329T"
"00|END:STANDARD|BEGIN:DAYLIGHT|DTSTART:19700329T"
"020000|TZNAME:CEST|TZOFFSETFROM:+0100|TZOFFSETTO:+0200|END:DAYLI"
"GHT|END:VTIMEZONE"
self.assertTrue(vtimezone_lines in test_out)

test_str = "DTSTART;TZID=Europe/Vienna;VALUE=DATE-TIME:20120213T100000"
test_str = "DTSTART;TZID=Europe/Vienna:20120213T100000"
self.assertTrue(test_str in test_out)
self.assertTrue("ATTENDEE:sepp" in test_out)

# ical standard expects DTSTAMP and CREATED in UTC
self.assertTrue("DTSTAMP;VALUE=DATE-TIME:20101010T081010Z" in test_out)
self.assertTrue("CREATED;VALUE=DATE-TIME:20101010T081010Z" in test_out)
self.assertTrue("DTSTAMP:20101010T081010Z" in test_out)
self.assertTrue("CREATED:20101010T081010Z" in test_out)


def test_tzinfo_dateutil(self):
Expand Down
10 changes: 5 additions & 5 deletions src/icalendar/tests/test_unit_cal.py
Expand Up @@ -227,12 +227,12 @@ def test_cal_Component_add(self):

lines = comp.to_ical().splitlines()
self.assertTrue(
b"DTSTART;TZID=Europe/Vienna;VALUE=DATE-TIME:20101010T100000"
b"DTSTART;TZID=Europe/Vienna:20101010T100000"
in lines)
self.assertTrue(b"CREATED;VALUE=DATE-TIME:20101010T120000Z" in lines)
self.assertTrue(b"DTSTAMP;VALUE=DATE-TIME:20101010T120000Z" in lines)
self.assertTrue(b"CREATED:20101010T120000Z" in lines)
self.assertTrue(b"DTSTAMP:20101010T120000Z" in lines)
self.assertTrue(
b"LAST-MODIFIED;VALUE=DATE-TIME:20101010T160000Z" in lines
b"LAST-MODIFIED:20101010T160000Z" in lines
)

def test_cal_Component_add_no_reencode(self):
Expand Down Expand Up @@ -388,7 +388,7 @@ def test_cal_Calendar(self):
self.assertEqual(
cal.subcomponents[0].to_ical(),
b'BEGIN:VEVENT\r\nSUMMARY:Python meeting about calendaring\r\n'
+ b'DTSTART;VALUE=DATE-TIME:20050404T080000\r\nUID:42\r\n'
+ b'DTSTART:20050404T080000\r\nUID:42\r\n'
+ b'END:VEVENT\r\n')

# Write to disc
Expand Down

0 comments on commit 5dae2a4

Please sign in to comment.