package com.hubspot.jinjava.objects.date;

import java.io.Serializable;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.time.temporal.ChronoField;
import java.util.Date;
import java.util.Objects;
import java.util.Optional;

import org.apache.commons.lang3.math.NumberUtils;

import com.hubspot.jinjava.objects.PyWrapper;

/**
 * an object which quacks like a python date
 *
 * @author jstehler
 *
 */
public final class PyishDate extends Date implements Serializable, PyWrapper {
  private static final long serialVersionUID = 1L;

  private final ZonedDateTime date;

  public PyishDate(ZonedDateTime dt) {
    super(dt.toInstant().toEpochMilli());
    this.date = dt;
  }

  public PyishDate(Date d) {
    this(ZonedDateTime.ofInstant(d.toInstant(), ZoneOffset.UTC));
  }

  public PyishDate(String publishDateStr) {
    this(NumberUtils.toLong(Objects.requireNonNull(publishDateStr), 0L));
  }

  public PyishDate(Long epochMillis) {
    this(ZonedDateTime.ofInstant(Instant.ofEpochMilli(
        Optional.ofNullable(epochMillis).orElseGet(System::currentTimeMillis)), ZoneOffset.UTC));
  }

  public String isoformat() {
    return strftime("yyyy-MM-dd");
  }

  public String strftime(String fmt) {
    return StrftimeFormatter.format(date, fmt);
  }

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

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

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

  public int getHour() {
    return date.getHour();
  }

  public int getMinute() {
    return date.getMinute();
  }

  public int getSecond() {
    return date.getSecond();
  }

  public int getMicrosecond() {
    return date.get(ChronoField.MILLI_OF_SECOND);
  }

  public Date toDate() {
    return Date.from(date.toInstant());
  }

  public ZonedDateTime toDateTime() {
    return date;
  }

  @Override
  public String toString() {
    return strftime("yyyy-MM-dd HH:mm:ss");
  }

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

  @Override
  public boolean equals(Object obj) {
    if (this == obj) {
      return true;
    }
    if (obj == null || getClass() != obj.getClass()) {
      return false;
    }
    PyishDate that = (PyishDate) obj;
    return Objects.equals(toDateTime(), that.toDateTime());
  }

}

@@ -36,16 +36,113 @@
 .date;%0A%0A
+import com.hubspot.jinjava.objects.PyWrapper;%0Aimport org.apache.commons.lang3.math.NumberUtils;%0A%0A
 import j
@@ -369,106 +369,8 @@
 l;%0A%0A
-import org.apache.commons.lang3.math.NumberUtils;%0A%0Aimport com.hubspot.jinjava.objects.PyWrapper;%0A%0A
 /**%0A
@@ -1130,32 +1130,135 @@
 set.UTC));%0A  %7D%0A%0A
+  public PyishDate(Instant instant) %7B%0A    this(ZonedDateTime.ofInstant(instant, ZoneOffset.UTC));%0A  %7D%0A%0A
   public String 

add a pyishdate constructor that takes an Instant