Class DateTimeFunction

All Implemented Interfaces:
NamedExpression, HasSQL, Typed

public final class DateTimeFunction extends Function1_2
A date-time function.
  • Field Details

  • Constructor Details

    • DateTimeFunction

      public DateTimeFunction(int function, int field, Expression arg1, Expression arg2)
  • Method Details

    • getField

      public static int getField(String name)
      Get date-time field for the specified name.
      Parameters:
      name - the name
      Returns:
      the date-time field
      Throws:
      DbException - on unknown field name
    • getFieldName

      public static String getFieldName(int field)
      Get the name of the specified date-time field.
      Parameters:
      field - the date-time field
      Returns:
      the name of the specified field
    • getValue

      public Value getValue(SessionLocal session, Value v1, Value v2)
      Description copied from class: Function1_2
      Returns the value of this function.
      Overrides:
      getValue in class Function1_2
      Parameters:
      session - the session
      v1 - the value of first argument
      v2 - the value of second argument, or null
      Returns:
      the resulting value
    • extractInteger

      private static int extractInteger(SessionLocal session, Value date, int field)
      Get the specified field of a date, however with years normalized to positive or negative, and month starting with 1.
      Parameters:
      session - the session
      date - the date value
      field - the field type
      Returns:
      the value
    • extractInterval

      private static int extractInterval(Value date, int field)
    • extractDateTime

      static int extractDateTime(SessionLocal session, Value date, int field)
    • truncateDate

      private static Value truncateDate(SessionLocal session, int field, Value value)
      Truncate the given date-time value to the specified field.
      Parameters:
      session - the session
      field - the date-time field
      value - the date-time value
      Returns:
      date the truncated value
    • truncateToWeek

      private static long truncateToWeek(long dateValue, int firstDayOfWeek)
    • truncateToWeekYear

      private static long truncateToWeekYear(long dateValue, int firstDayOfWeek, int minimalDaysInFirstWeek)
    • dateadd

      public static Value dateadd(SessionLocal session, int field, long count, Value v)
      DATEADD function.
      Parameters:
      session - the session
      field - the date-time field
      count - count to add
      v - value to add to
      Returns:
      result
    • addYearsMonths

      private static Value addYearsMonths(int field, boolean years, long count, Value v, int type, long dateValue, long timeNanos)
    • addToTimeZone

      private static Value addToTimeZone(int field, long count, Value v, int type, long dateValue, long timeNanos)
    • datediff

      private static long datediff(SessionLocal session, int field, Value v1, Value v2)
      Calculate the number of crossed unit boundaries between two timestamps. This method is supported for MS SQL Server compatibility.
       DATEDIFF(YEAR, '2004-12-31', '2005-01-01') = 1
       
      Parameters:
      session - the session
      field - the date-time field
      v1 - the first date-time value
      v2 - the second date-time value
      Returns:
      the number of crossed boundaries
    • weekdiff

      private static long weekdiff(long absolute1, long absolute2, int firstDayOfWeek)
    • millennium

      private static int millennium(int year)
    • century

      private static int century(int year)
    • decade

      private static int decade(int year)
    • getLocalDayOfWeek

      private static int getLocalDayOfWeek(long dateValue)
    • getLocalWeekOfYear

      private static int getLocalWeekOfYear(long dateValue)
    • getWeekFields

      private static WeekFields getWeekFields()
    • extractEpoch

      private static ValueNumeric extractEpoch(SessionLocal session, Value value)
    • optimize

      public Expression optimize(SessionLocal session)
      Description copied from class: Expression
      Try to optimize the expression.
      Specified by:
      optimize in class Expression
      Parameters:
      session - the session
      Returns:
      the optimized expression
    • getUnenclosedSQL

      public StringBuilder getUnenclosedSQL(StringBuilder builder, int sqlFlags)
      Description copied from class: Expression
      Get the SQL statement of this expression. This may not always be the original SQL statement, especially after optimization. Enclosing '(' and ')' are never appended.
      Overrides:
      getUnenclosedSQL in class Function1_2
      Parameters:
      builder - string builder
      sqlFlags - formatting flags
      Returns:
      the specified string builder
    • getName

      public String getName()
      Description copied from interface: NamedExpression
      Get the name.
      Returns:
      the name in uppercase