Skip to content

Conversation

@vinodkc
Copy link
Contributor

@vinodkc vinodkc commented Dec 4, 2025

What changes were proposed in this pull request?

This PR adds a new time_format function that converts TIME data type values to formatted string representations, providing functionality similar to date_format but specifically designed for TIME values.

Why are the changes needed?

Users need a standard way to format TIME values as strings for:

  • Display purposes: Presenting time in user-friendly formats
  • Reporting: Generating formatted output for reports and dashboards
  • Data export: Converting TIME values to specific string formats for external systems
  • Localization: Supporting different time display conventions (12-hour vs 24-hour)

Does this PR introduce any user-facing change?

Yes, this PR adds a new public API function.

Scala API

import org.apache.spark.sql.functions._

df.select(time_format($"time_col", "HH:mm:ss"))
df.select(time_format($"time_col", "hh:mm:ss a"))

Python API

from pyspark.sql import functions as F

df.select(F.time_format("time_col", "HH:mm:ss"))
df.select(F.time_format("time_col", "hh:mm:ss a"))

SQL Usage

SELECT time_format(TIME'14:30:45', 'HH:mm:ss');           -- '14:30:45'
SELECT time_format(TIME'14:30:45.1234', 'hh-mm-ss.SS a');         -- '02-30-45.12 PM'
SELECT time_format(TIME'09:05:00', 'h:mm a');             -- '9:05 AM'

Format Pattern Support

Pattern Description Example Output
HH:mm:ss 24-hour format 14:30:45
hh:mm:ss a 12-hour with AM/PM 02:30:45 PM
H:mm Single-digit hour 9:15
HH:mm:ss.SSS With milliseconds 14:30:45.123
HH:mm:ss.SSSSSS With microseconds 14:30:45.123456
HH-mm-ss Custom separator 14-30-45
'Time:' HH:mm With text literal Time: 14:30

How was this patch tested?

Added tests in TimeExpressionsSuite, TimeFunctionsSuiteBase

Was this patch authored or co-authored using generative AI tooling?

No

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant