Skip to content

Commit

Permalink
Adds spi datetime classes
Browse files Browse the repository at this point in the history
  • Loading branch information
RCHowell committed Nov 26, 2024
1 parent 1fd7894 commit 95b5e5f
Show file tree
Hide file tree
Showing 26 changed files with 1,134 additions and 2,487 deletions.
118 changes: 118 additions & 0 deletions partiql-spi/api/partiql-spi.api
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,124 @@ public final class org/partiql/spi/catalog/Table$DefaultImpls {
public static fun getSchema (Lorg/partiql/spi/catalog/Table;)Lorg/partiql/types/PType;
}

public class org/partiql/spi/datetime/Date {
public fun equals (Ljava/lang/Object;)Z
public fun getDay ()I
public fun getMonth ()I
public fun getYear ()I
public fun hashCode ()I
public fun minus (Lorg/partiql/spi/datetime/Date;)Lorg/partiql/spi/datetime/Interval;
public fun minus (Lorg/partiql/spi/datetime/Interval;)Lorg/partiql/spi/datetime/Date;
public static fun now ()Lorg/partiql/spi/datetime/Date;
public static fun of (III)Lorg/partiql/spi/datetime/Date;
public static fun of (Ljava/time/LocalDate;)Lorg/partiql/spi/datetime/Date;
public fun plus (Lorg/partiql/spi/datetime/Interval;)Lorg/partiql/spi/datetime/Date;
public fun toLocalDate ()Ljava/time/LocalDate;
public fun toString ()Ljava/lang/String;
}

public abstract interface class org/partiql/spi/datetime/Interval {
public static fun days (I)Lorg/partiql/spi/datetime/Interval$YM;
public static fun duration (Ljava/time/Duration;)Lorg/partiql/spi/datetime/Interval$DT;
public static fun hours (I)Lorg/partiql/spi/datetime/Interval$DT;
public static fun millis (J)Lorg/partiql/spi/datetime/Interval$DT;
public static fun minutes (I)Lorg/partiql/spi/datetime/Interval$DT;
public static fun months (I)Lorg/partiql/spi/datetime/Interval$YM;
public static fun nanos (J)Lorg/partiql/spi/datetime/Interval$DT;
public static fun period (Ljava/time/Period;)Lorg/partiql/spi/datetime/Interval$YM;
public static fun seconds (J)Lorg/partiql/spi/datetime/Interval$DT;
public static fun weeks (I)Lorg/partiql/spi/datetime/Interval$YM;
public static fun years (I)Lorg/partiql/spi/datetime/Interval$YM;
}

public class org/partiql/spi/datetime/Interval$DT : org/partiql/spi/datetime/Interval {
public fun equals (Ljava/lang/Object;)Z
public fun hashCode ()I
public fun toDuration ()Ljava/time/Duration;
public fun toString ()Ljava/lang/String;
}

public class org/partiql/spi/datetime/Interval$YM : org/partiql/spi/datetime/Interval {
public fun equals (Ljava/lang/Object;)Z
public fun hashCode ()I
public fun toPeriod ()Ljava/time/Period;
public fun toString ()Ljava/lang/String;
}

public class org/partiql/spi/datetime/Time {
public fun equals (Ljava/lang/Object;)Z
public fun getHour ()I
public fun getMinute ()I
public fun getNano ()I
public fun getSecond ()I
public fun hashCode ()I
public static fun now ()Lorg/partiql/spi/datetime/Time;
public static fun of (II)Lorg/partiql/spi/datetime/Time;
public static fun of (III)Lorg/partiql/spi/datetime/Time;
public static fun of (IIII)Lorg/partiql/spi/datetime/Time;
public static fun of (Ljava/time/LocalTime;)Lorg/partiql/spi/datetime/Time;
public fun toLocalTime ()Ljava/time/LocalTime;
public fun toString ()Ljava/lang/String;
}

public class org/partiql/spi/datetime/Timestamp {
public fun equals (Ljava/lang/Object;)Z
public fun getDay ()I
public fun getHour ()I
public fun getMinute ()I
public fun getMonth ()I
public fun getNano ()I
public fun getSecond ()I
public fun getYear ()I
public fun hashCode ()I
public fun now ()Lorg/partiql/spi/datetime/Timestamp;
public static fun of (IIIIII)Lorg/partiql/spi/datetime/Timestamp;
public static fun of (IIIIIII)Lorg/partiql/spi/datetime/Timestamp;
public static fun of (Ljava/time/LocalDateTime;)Lorg/partiql/spi/datetime/Timestamp;
public static fun of (Lorg/partiql/spi/datetime/Date;Lorg/partiql/spi/datetime/Time;)Lorg/partiql/spi/datetime/Timestamp;
public fun toDate ()Lorg/partiql/spi/datetime/Date;
public fun toLocalDateTime ()Ljava/time/LocalDateTime;
public fun toString ()Ljava/lang/String;
public fun toTime ()Lorg/partiql/spi/datetime/Time;
}

public class org/partiql/spi/datetime/Timestampz {
public fun equals (Ljava/lang/Object;)Z
public fun getDay ()I
public fun getHour ()I
public fun getMinute ()I
public fun getMonth ()I
public fun getNano ()I
public fun getOffset ()Ljava/time/ZoneOffset;
public fun getSecond ()I
public fun getYear ()I
public fun hashCode ()I
public fun now ()Lorg/partiql/spi/datetime/Timestampz;
public static fun of (Ljava/time/OffsetDateTime;)Lorg/partiql/spi/datetime/Timestampz;
public static fun of (Lorg/partiql/spi/datetime/Date;Lorg/partiql/spi/datetime/Timez;)Lorg/partiql/spi/datetime/Timestampz;
public fun toDate ()Lorg/partiql/spi/datetime/Date;
public fun toLocalDateTime ()Ljava/time/LocalDateTime;
public fun toOffsetDateTime ()Ljava/time/OffsetDateTime;
public fun toString ()Ljava/lang/String;
public fun toTimez ()Lorg/partiql/spi/datetime/Timez;
}

public class org/partiql/spi/datetime/Timez {
public fun equals (Ljava/lang/Object;)Z
public fun getHour ()I
public fun getMinute ()I
public fun getNano ()I
public fun getOffset ()Ljava/time/ZoneOffset;
public fun getSecond ()I
public fun hashCode ()I
public static fun now ()Lorg/partiql/spi/datetime/Timez;
public static fun of (Ljava/time/LocalTime;Ljava/time/ZoneOffset;)Lorg/partiql/spi/datetime/Timez;
public static fun of (Ljava/time/OffsetTime;)Lorg/partiql/spi/datetime/Timez;
public fun toLocalTime ()Ljava/time/LocalTime;
public fun toOffsetTime ()Ljava/time/OffsetTime;
public fun toString ()Ljava/lang/String;
}

public final class org/partiql/spi/errors/PError : org/partiql/spi/Enum {
public static final field ALWAYS_MISSING I
public static final field FEATURE_NOT_SUPPORTED I
Expand Down
166 changes: 166 additions & 0 deletions partiql-spi/src/main/java/org/partiql/spi/datetime/Date.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package org.partiql.spi.datetime;

import org.jetbrains.annotations.NotNull;

import java.time.Clock;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
* A PartiQL date.
* <br>
* This class provides basic APIs for creating a Date. More complex use-cases should create a Date from a LocalDate.
* For example, to create a `now()` Date with an alternative timezone, use:
* <pre>
* Clock clock = Clock.system(ZoneId.of("America/New_York"));
* Date date = new Date(LocalDate.now(clock));
* </pre>
* This API may be extended with additional constructors to reduce verbosity, but it is intentionally lean.
*/
public class Date {

/**
* Delegate all functionality to the JDK8+ recommend APIs, but keep this internalized.
*/
@NotNull
private final LocalDate date;

/**
* SQL date formatter.
*/
private static final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

/**
* NO PUBLIC CONSTRUCTORS.
*/
private Date(@NotNull LocalDate date) {
this.date = date;
}

/**
* @return date for current date-time for the system clock.
*/
@NotNull
public static Date now() {
return new Date(LocalDate.now(Clock.systemDefaultZone()));
}

/**
* Create a new Date from a {@link LocalDate}.
*
* @param date the {@link LocalDate} to wrap
*/
@NotNull
public static Date of(@NotNull LocalDate date) {
return new Date(date);
}

/**
* Create a new Date from a year, month, and day.
*
* @param year the year to represent, from Year.MIN_YEAR to Year.MAX_YEAR
* @param month the month-of-year to represent, from 1 (January) to 12 (December)
* @param day the day of month to represent, from 1 to 31
*/
@NotNull
public static Date of(int year, int month, int day) {
return new Date(LocalDate.of(year, month, day));
}

/**
* @return a {@link LocalDate} representation of this date.
*/
@NotNull
public LocalDate toLocalDate() {
return date;
}

/**
* @return the year of this date
*/
public int getYear() {
return date.getYear();
}

/**
* @return the month (of the year) field value.
*/
public int getMonth() {
return date.getMonthValue();
}

/**
* @return the day (of the month) field value.
*/
public int getDay() {
return date.getDayOfMonth();
}

/**
* @return temporal difference between this date and the other date.
*/
@NotNull
public Interval minus(Date other) {
// inverse of 'until'
return Interval.period(other.date.until(this.date));
}

/**
* @return a new Date representing the result of adding the interval to this Date
*/
@NotNull
public Date plus(Interval interval) {
LocalDate date;
if (interval instanceof Interval.YM) {
date = this.date.plus(((Interval.YM) interval).toPeriod());
} else {
date = this.date.plus(((Interval.DT) interval).toDuration());
}
return new Date(date);
}

/**
* @return a new Date representing the result of subtracting the interval to this Date
*/
@NotNull
public Date minus(Interval interval) {
LocalDate date;
if (interval instanceof Interval.YM) {
date = this.date.minus(((Interval.YM) interval).toPeriod());
} else {
date = this.date.minus(((Interval.DT) interval).toDuration());
}
return new Date(date);
}

@Override
public int hashCode() {
return date.hashCode();
}

/**
* In SQL, datetime values are mutually comparable if they share the same fields.
* <br>
*
* @param obj the object to compare to
* @return true if the objects are equal, false otherwise
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof Date))
return false;
return date.equals(((Date) obj).date);
}

/**
* @return the SQL string for this date.
*/
@Override
public String toString() {
return "DATE " + date.format(formatter);
}
}
Loading

0 comments on commit 95b5e5f

Please sign in to comment.