Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions news/2136.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FLOAT and GEO values are written without an exponent, as RFC 5545 3.3.7 requires: a latitude of 0.00001 is written as ``GEO:0.00001;...`` instead of ``GEO:1e-05;...``.
19 changes: 17 additions & 2 deletions src/icalendar/prop/float.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""FLOAT values from :rfc:`5545`."""

import math
from decimal import Decimal
from typing import Any, ClassVar
from xml.etree.ElementTree import Element, SubElement

Expand All @@ -12,6 +13,20 @@
from icalendar.parser.xcal.wrapper import from_xcal_wrapper


def to_ical_float(value: float) -> str:
"""Render a number as a FLOAT value of :rfc:`5545#section-3.3.7`.

FLOAT has no exponent notation, but ``str()`` on a float switches to it
below ``1e-04`` and from ``1e16`` on, which other iCalendar parsers reject.
``Decimal`` renders the shortest round-tripping decimal of the float in
plain notation, so ``1e-05`` becomes ``0.00001`` and ``1e16`` becomes
``10000000000000000``.
"""
if not math.isfinite(value):
return str(value)
return format(Decimal(repr(float(value))), "f")


class vFloat(float):
"""Float

Expand Down Expand Up @@ -69,7 +84,7 @@ def __new__(
return self

def to_ical(self) -> bytes:
return str(self).encode("utf-8")
return to_ical_float(self).encode("utf-8")

@classmethod
def from_ical(cls, ical: str | float) -> Self:
Expand Down Expand Up @@ -140,4 +155,4 @@ def to_xcal(self, element: Element) -> None:
element.text = xsd_float_to_string(self)


__all__ = ["vFloat"]
__all__ = ["to_ical_float", "vFloat"]
3 changes: 2 additions & 1 deletion src/icalendar/prop/geo.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from icalendar.parser import Parameters
from icalendar.parser.xcal.string import xsd_float_to_string
from icalendar.parser.xcal.wrapper import from_xcal_wrapper
from icalendar.prop.float import to_ical_float

if TYPE_CHECKING:
from icalendar.compatibility import Self
Expand Down Expand Up @@ -108,7 +109,7 @@ def __init__(
self.params = Parameters(params)

def to_ical(self) -> str:
return f"{self.latitude};{self.longitude}"
return f"{to_ical_float(self.latitude)};{to_ical_float(self.longitude)}"

@property
def ical_value(self) -> tuple[float, float]:
Expand Down
38 changes: 38 additions & 0 deletions src/icalendar/tests/prop/test_vFloat.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import re

import pytest

from icalendar.prop.float import vFloat
Expand Down Expand Up @@ -29,3 +31,39 @@ def test_ical_value():
"""ical_value property returns the float value."""
assert vFloat(1.0).ical_value == 1.0
assert vFloat(0.0).ical_value == 0.0


RFC5545_FLOAT = re.compile(rb"^[+-]?\d+(?:\.\d+)?$")
"""FLOAT of RFC 5545 3.3.7: no exponent, optional sign, optional fraction."""


@pytest.mark.parametrize(
("value", "expected"),
[
(1e-5, b"0.00001"),
(-2e-5, b"-0.00002"),
(1e-7, b"0.0000001"),
(1e16, b"10000000000000000"),
(-1e16, b"-10000000000000000"),
(1.333, b"1.333"),
(37.386013, b"37.386013"),
(0.0, b"0.0"),
(42.0, b"42.0"),
],
)
def test_no_exponent(value, expected):
"""FLOAT carries no exponent, see RFC 5545 3.3.7."""
ical = vFloat(value).to_ical()
assert ical == expected
assert RFC5545_FLOAT.match(ical), ical


def test_small_value_keeps_its_precision():
"""A value far below 1e-04 must not be rounded away when formatted."""
assert vFloat(1e-7).to_ical() == b"0.0000001"
assert vFloat.from_ical("0.0000001") == 1e-7


def test_written_value_parses_back():
for value in (1e-5, -2e-5, 1e16, 37.386013):
assert vFloat.from_ical(vFloat(value).to_ical().decode()) == value
36 changes: 36 additions & 0 deletions src/icalendar/tests/prop/test_vGeo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import re

import pytest

from icalendar import Event
from icalendar.prop import vGeo

RFC5545_FLOAT = re.compile(r"^[+-]?\d+(?:\.\d+)?$")


@pytest.mark.parametrize(
("geo", "expected"),
[
((0.00001, -0.00002), "0.00001;-0.00002"),
((1e16, -1e16), "10000000000000000;-10000000000000000"),
((37.386013, -122.082932), "37.386013;-122.082932"),
((1.2, 3.0), "1.2;3.0"),
],
)
def test_geo_has_no_exponent(geo, expected):
"""FLOAT has no exponent notation, see RFC 5545 3.3.7."""
ical = vGeo(geo).to_ical()
assert ical == expected
assert all(RFC5545_FLOAT.match(part) for part in ical.split(";"))


def test_event_geo_line_has_no_exponent():
"""The reported case: a GEO near the equator used to be written as 1e-05."""
event = Event()
event.add("GEO", (0.00001, -0.00002))
assert "GEO:0.00001;-0.00002" in event.to_ical().decode()


def test_written_geo_parses_back():
geo = (1e-5, -2e-5)
assert vGeo.from_ical(vGeo(geo).to_ical()) == geo
Loading