Github user davies commented on a diff in the pull request:
https://github.com/apache/spark/pull/6981#discussion_r34868614
--- Diff:
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/DateTimeUtils.scala
---
@@ -378,4 +395,183 @@ object DateTimeUtils {
c.set(segments(0), segments(1) - 1, segments(2), 0, 0, 0)
Some((c.getTimeInMillis / 1000 / 3600 / 24).toInt)
}
+
+ /**
+ * Returns the hour value of a given timestamp value. The timestamp is
expressed in microseconds.
+ */
+ def getHours(timestamp: Long): Int = {
+ val localTs = (timestamp / 1000) + defaultTimeZone.getOffset(timestamp
/ 1000)
+ ((localTs / 1000 / 3600) % 24).toInt
+ }
+
+ /**
+ * Returns the minute value of a given timestamp value. The timestamp is
expressed in
+ * microseconds.
+ */
+ def getMinutes(timestamp: Long): Int = {
+ val localTs = (timestamp / 1000) + defaultTimeZone.getOffset(timestamp
/ 1000)
+ ((localTs / 1000 / 60) % 60).toInt
+ }
+
+ /**
+ * Returns the second value of a given timestamp value. The timestamp is
expressed in
+ * microseconds.
+ */
+ def getSeconds(timestamp: Long): Int = {
+ val localTs = (timestamp / 1000) + defaultTimeZone.getOffset(timestamp
/ 1000)
+ ((localTs / 1000) % 60).toInt
+ }
+
+ private[this] def isLeapYear(year: Int): Boolean = {
+ (year % 4) == 0 && ((year % 100) != 0 || (year % 400) == 0)
+ }
+
+ /**
+ * Return the number of days since the start of 400 year period.
+ * The second year of a 400 year period (year 1) starts on day 365.
+ */
+ private[this] def yearBoundary(year: Int): Int = {
+ year * 365 + ((year / 4 ) - (year / 100) + (year / 400))
+ }
+
+ /**
+ * Calculates the number of years for the given number of days. This
depends
+ * on a 400 year period.
+ * @param days days since the beginning of the 400 year period
+ * @return number of year
+ */
+ private[this] def numYears(days: Int): Int = {
+ val year = days / 365
+ if (days > yearBoundary(year)) year else year - 1
+ }
+
+ /**
+ * Calculates the year and and the number of the day in the year for the
given
+ * number of days. The given days is the number of days since 1.1.1970.
+ *
+ * The calculation uses the fact that the period 1.1.2001 until
31.12.2400 is
+ * equals to the period 1.1.1601 until 31.12.2000.
+ */
+ private[this] def getYearAndDayInYear(daysSince1970: Int): (Int, Int) = {
+ // add the difference (in days) between 1.1.1970 and the artificial
year 0 (-17999)
+ val daysNormalized = daysSince1970 + toYearZero
+ val numOfQuarterCenturies = daysNormalized / daysIn400Years
+ val daysInThis400 = daysNormalized % daysIn400Years + 1
+ val years = numYears(daysInThis400)
+ val year: Int = (2001 - 20000) + 400 * numOfQuarterCenturies + years
+ val dayInYear = daysInThis400 - yearBoundary(years)
+ (year, dayInYear)
+ }
+
+ /**
+ * Returns the 'day in year' value for the given date. The date is
expressed in days
+ * since 1.1.1970.
+ */
+ def getDayInYear(date: Int): Int = {
+ getYearAndDayInYear(date)._2
+ }
+
+ /**
+ * Returns the year value for the given date. The date is expressed in
days
+ * since 1.1.1970.
+ */
+ def getYear(date: Int): Int = {
+ getYearAndDayInYear(date)._1
+ }
+
+ /**
+ * Returns the quarter for the given date. The date is expressed in days
+ * since 1.1.1970.
+ */
+ def getQuarter(date: Int): Int = {
+ val (year, dayInYear) = getYearAndDayInYear(date)
+ val leap = if (isLeapYear(year)) 1 else 0
+ if (dayInYear <= 90 + leap) {
+ 1
+ } else if (dayInYear <= 181 + leap) {
+ 2
+ } else if (dayInYear <= 273 + leap) {
+ 3
+ } else {
+ 4
+ }
+ }
+
+ /**
+ * Returns the month value for the given date. The date is expressed in
days
+ * since 1.1.1970. January is month 1.
+ */
+ def getMonth(date: Int): Int = {
+ val (year, dayInYear) = getYearAndDayInYear(date)
+ val leap = if (isLeapYear(year)) 1 else 0
--- End diff --
It's better to remove the `leap` from days, if `days > 31 + 28`
---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]