Search in sources :

Example 1 with ISOChronology

use of org.joda.time.chrono.ISOChronology in project presto by prestodb.

the class DateTimeFunctions method addFieldValueTimeWithTimeZone.

@Description("add the specified amount of time to the given time")
@LiteralParameters("x")
@ScalarFunction("date_add")
@SqlType(StandardTypes.TIME_WITH_TIME_ZONE)
public static long addFieldValueTimeWithTimeZone(@SqlType("varchar(x)") Slice unit, @SqlType(StandardTypes.BIGINT) long value, @SqlType(StandardTypes.TIME_WITH_TIME_ZONE) long timeWithTimeZone) {
    ISOChronology chronology = unpackChronology(timeWithTimeZone);
    long millis = modulo24Hour(chronology, getTimeField(chronology, unit).add(unpackMillisUtc(timeWithTimeZone), toIntExact(value)));
    return updateMillisUtc(millis, timeWithTimeZone);
}
Also used : ISOChronology(org.joda.time.chrono.ISOChronology) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) Description(com.facebook.presto.spi.function.Description) LiteralParameters(com.facebook.presto.spi.function.LiteralParameters) SqlType(com.facebook.presto.spi.function.SqlType)

Example 2 with ISOChronology

use of org.joda.time.chrono.ISOChronology in project presto by prestodb.

the class DateTimeFunctions method currentDate.

@Description("current date")
@ScalarFunction
@SqlType(StandardTypes.DATE)
public static long currentDate(ConnectorSession session) {
    ISOChronology chronology = getChronology(session.getTimeZoneKey());
    // It is ok for this method to use the Object interfaces because it is constant folded during
    // plan optimization
    DateTime currentDateTime = new DateTime(session.getStartTime(), chronology).withTimeAtStartOfDay();
    DateTime baseDateTime = new DateTime(1970, 1, 1, 0, 0, chronology).withTimeAtStartOfDay();
    return Days.daysBetween(baseDateTime, currentDateTime).getDays();
}
Also used : ISOChronology(org.joda.time.chrono.ISOChronology) DateTime(org.joda.time.DateTime) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) Description(com.facebook.presto.spi.function.Description) SqlType(com.facebook.presto.spi.function.SqlType)

Example 3 with ISOChronology

use of org.joda.time.chrono.ISOChronology in project presto by prestodb.

the class DateOperators method castToTimestampWithTimeZone.

@ScalarOperator(CAST)
@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE)
public static long castToTimestampWithTimeZone(ConnectorSession session, @SqlType(StandardTypes.DATE) long value) {
    long utcMillis = TimeUnit.DAYS.toMillis(value);
    // date is encoded as milliseconds at midnight in UTC
    // convert to midnight if the session timezone
    ISOChronology chronology = getChronology(session.getTimeZoneKey());
    long millis = utcMillis - chronology.getZone().getOffset(utcMillis);
    return packDateTimeWithZone(millis, session.getTimeZoneKey());
}
Also used : ISOChronology(org.joda.time.chrono.ISOChronology) ScalarOperator(com.facebook.presto.spi.function.ScalarOperator) SqlType(com.facebook.presto.spi.function.SqlType)

Example 4 with ISOChronology

use of org.joda.time.chrono.ISOChronology in project presto by prestodb.

the class DateOperators method castToTimestamp.

@ScalarOperator(CAST)
@SqlType(StandardTypes.TIMESTAMP)
public static long castToTimestamp(ConnectorSession session, @SqlType(StandardTypes.DATE) long value) {
    long utcMillis = TimeUnit.DAYS.toMillis(value);
    // date is encoded as milliseconds at midnight in UTC
    // convert to midnight if the session timezone
    ISOChronology chronology = getChronology(session.getTimeZoneKey());
    return utcMillis - chronology.getZone().getOffset(utcMillis);
}
Also used : ISOChronology(org.joda.time.chrono.ISOChronology) ScalarOperator(com.facebook.presto.spi.function.ScalarOperator) SqlType(com.facebook.presto.spi.function.SqlType)

Example 5 with ISOChronology

use of org.joda.time.chrono.ISOChronology in project presto by prestodb.

the class TimestampOperators method castToDate.

@ScalarFunction("date")
@ScalarOperator(CAST)
@SqlType(StandardTypes.DATE)
public static long castToDate(ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP) long value) {
    // round down the current timestamp to days
    ISOChronology chronology = getChronology(session.getTimeZoneKey());
    long date = chronology.dayOfYear().roundFloor(value);
    // date is currently midnight in timezone of the session
    // convert to UTC
    long millis = date + chronology.getZone().getOffset(date);
    return TimeUnit.MILLISECONDS.toDays(millis);
}
Also used : ISOChronology(org.joda.time.chrono.ISOChronology) ScalarOperator(com.facebook.presto.spi.function.ScalarOperator) ScalarFunction(com.facebook.presto.spi.function.ScalarFunction) SqlType(com.facebook.presto.spi.function.SqlType)

Aggregations

ISOChronology (org.joda.time.chrono.ISOChronology)11 SqlType (com.facebook.presto.spi.function.SqlType)6 ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)4 ScalarOperator (com.facebook.presto.spi.function.ScalarOperator)4 Description (com.facebook.presto.spi.function.Description)2 LiteralParameters (com.facebook.presto.spi.function.LiteralParameters)1 PeriodGranularity (io.druid.java.util.common.granularity.PeriodGranularity)1 ArrayList (java.util.ArrayList)1 SortedSet (java.util.SortedSet)1 Chronology (org.joda.time.Chronology)1 DateTime (org.joda.time.DateTime)1 DateTimeZone (org.joda.time.DateTimeZone)1 Interval (org.joda.time.Interval)1 Period (org.joda.time.Period)1 ReadableInstant (org.joda.time.ReadableInstant)1 Test (org.junit.Test)1