Github user davies commented on a diff in the pull request:

    https://github.com/apache/spark/pull/6981#discussion_r34930631
  
    --- Diff: 
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/util/DateTimeUtils.scala
 ---
    @@ -378,4 +395,208 @@ 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 = {
    +    ((timestamp / 1000 / 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, days in year)
    +   */
    +  private[this] def numYears(days: Int): (Int, Int) = {
    +    val year = days / 365
    +    val boundary = yearBoundary(year)
    +    if (days > boundary) (year, days - boundary) else (year - 1, days - 
yearBoundary(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, dayInYear) = numYears(daysInThis400)
    +    val year: Int = (2001 - 20000) + 400 * numOfQuarterCenturies + 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 = {
    +    var (year, dayInYear) = getYearAndDayInYear(date)
    +    if (isLeapYear(year)) {
    +      dayInYear = dayInYear - 1
    +    }
    +    if (dayInYear <= 90) {
    +      1
    +    } else if (dayInYear <= 181) {
    +      2
    +    } else if (dayInYear <= 273) {
    +      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 = {
    +    var (year, dayInYear) = getYearAndDayInYear(date)
    +    var isLeap = isLeapYear(year)
    +    if (isLeap && dayInYear > 60) {
    +      dayInYear = dayInYear - 1
    +      isLeap = false
    +    }
    +
    +    if (dayInYear <= 181) {
    +      if (dayInYear <= 90) {
    +        if (dayInYear <= 31) {
    +          1
    +        } else if (dayInYear <= 59 || (isLeap && dayInYear <= 60)) {
    +          2
    +        } else {
    +          3
    +        }
    +      } else {
    +        if (dayInYear <= 120) {
    +          4
    +        } else if (dayInYear <= 151) {
    +          5
    +        } else {
    +          6
    +        }
    +      }
    +    } else {
    +      if (dayInYear <= 273) {
    +        if (dayInYear <= 212) {
    +          7
    +        } else if (dayInYear <= 243) {
    +          8
    +        } else {
    +          9
    +        }
    +      } else {
    +        if (dayInYear <= 304) {
    +          10
    +        } else if (dayInYear <= 334) {
    +          11
    +        } else {
    +          12
    +        }
    +      }
    +    }
    +  }
    +
    +  /**
    +   * Returns the 'day of month' value for the given date. The date is 
expressed in days
    +   * since 1.1.1970.
    +   */
    +  def getDayOfMonth(date: Int): Int = {
    +    var (year, dayInYear) = getYearAndDayInYear(date)
    +    var isLeap = isLeapYear(year)
    +    if (isLeap && dayInYear > 60) {
    --- End diff --
    
    >= 60 ?


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to