from __future__ import annotations
from tests.fixtures.spark_imports import get_spark_imports
_imports = get_spark_imports()
F = _imports.F
def test_to_timestamp_string_no_format(spark) -> None:
df = spark.createDataFrame(
[{"ts_str": "2024-01-01 10:00:00"}],
["ts_str"],
)
df = df.withColumn("ts", F.to_timestamp(F.col("ts_str")))
rows = df.collect()
assert len(rows) == 1
assert rows[0]["ts"] is not None
ts = rows[0]["ts"]
assert "2024-01-01" in str(ts) and "10:00:00" in str(ts)
def test_to_timestamp_string_with_format(spark) -> None:
df = spark.createDataFrame(
[{"ts_str": "2024-01-01T10:00:00"}],
["ts_str"],
)
df = df.withColumn(
"ts",
F.to_timestamp(F.col("ts_str"), "yyyy-MM-dd'T'HH:mm:ss"),
)
rows = df.collect()
assert len(rows) == 1
assert rows[0]["ts"] is not None
ts = rows[0]["ts"]
assert "2024-01-01" in str(ts) and "10:00:00" in str(ts)