Skip to content

Commit db89c92

Browse files
committed
Add comments to toJavaTimestamp and to fromJavaTimestamp
1 parent a6420f2 commit db89c92

File tree

1 file changed

+33
-4
lines changed

1 file changed

+33
-4
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/DateTimeUtils.scala

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ object DateTimeUtils {
113113
}
114114

115115
/**
116-
* The oposite to `fromJavaDate` method which converts a number of days to an
116+
* The opposite to `fromJavaDate` method which converts a number of days to an
117117
* instance of `java.sql.Date`. It builds a local date in Proleptic Gregorian
118118
* calendar, extracts date fields `year`, `month`, `day`, and creates a local
119119
* date in the hybrid calendar (Julian + Gregorian calendars) from the fields.
@@ -123,23 +123,52 @@ object DateTimeUtils {
123123
* in the target calender.
124124
*
125125
* @param daysSinceEpoch The number of days since 1970-01-01.
126-
* @return A java.sql.Date from number of days since epoch.
126+
* @return A `java.sql.Date` from number of days since epoch.
127127
*/
128128
def toJavaDate(daysSinceEpoch: SQLDate): Date = {
129129
val localDate = LocalDate.ofEpochDay(daysSinceEpoch)
130130
new Date(localDate.getYear - 1900, localDate.getMonthValue - 1, localDate.getDayOfMonth)
131131
}
132132

133133
/**
134-
* Returns a java.sql.Timestamp from number of micros since epoch.
134+
* Converts microseconds since the epoch to an instance of `java.sql.Timestamp`
135+
* via creating a local timestamp at the system time zone in Proleptic Gregorian
136+
* calendar, extracting date and time fields like `year` and `hours`, and forming
137+
* new timestamp in the hybrid calendar from the extracted fields.
138+
*
139+
* The conversion is based on the JVM system time zone because the `java.sql.Timestamp`
140+
* uses the time zone internally.
141+
*
142+
* The method performs the conversion via local timestamp fields to have the same date-time
143+
* representation as `year`, `month`, `day`, ..., `seconds` in the original calendar
144+
* and in the target calendar.
145+
*
146+
* @param us The number of microseconds since 1970-01-01T00:00:00.000000Z.
147+
* @return A `java.sql.Timestamp` from number of micros since epoch.
135148
*/
136149
def toJavaTimestamp(us: SQLTimestamp): Timestamp = {
137150
val ldt = microsToInstant(us).atZone(ZoneId.systemDefault()).toLocalDateTime
138151
Timestamp.valueOf(ldt)
139152
}
140153

141154
/**
142-
* Returns the number of micros since epoch from java.sql.Timestamp.
155+
* Converts an instance of `java.sql.Timestamp` to the number of microseconds since
156+
* 1970-01-01T00:00:00.000000Z. It extracts date-time fields from the input, builds
157+
* a local timestamp in Proleptic Gregorian calendar from the fields, and binds
158+
* the timestamp to the system time zone. The resulted instant is converted to
159+
* microseconds since the epoch.
160+
*
161+
* The conversion is performed via the system time zone because it is used internally
162+
* in `java.sql.Timestamp` while extracting date-time fields.
163+
*
164+
* The goal of the function is to have the same local date-time in the original calendar
165+
* - the hybrid calendar (Julian + Gregorian) and in the target calendar which is
166+
* Proleptic Gregorian calendar, see SPARK-26651.
167+
*
168+
* @param t It represents a specific instant in time based on
169+
* the hybrid calendar which combines Julian and
170+
* Gregorian calendars.
171+
* @return The number of micros since epoch from `java.sql.Timestamp`.
143172
*/
144173
def fromJavaTimestamp(t: Timestamp): SQLTimestamp = {
145174
val era = if (t.before(julianCommonEraStart)) 0 else 1

0 commit comments

Comments
 (0)