3232
3333package org .opensearch .script ;
3434
35+ import org .joda .time .DateTime ;
3536import org .opensearch .common .SuppressForbidden ;
37+ import org .opensearch .common .SuppressLoggerChecks ;
38+ import org .opensearch .common .logging .DeprecationLogger ;
3639import org .opensearch .common .time .DateFormatter ;
40+ import org .opensearch .common .time .DateFormatters ;
41+ import org .opensearch .common .time .DateUtils ;
3742
43+ import java .security .AccessController ;
44+ import java .security .PrivilegedAction ;
3845import java .time .DayOfWeek ;
3946import java .time .Instant ;
4047import java .time .LocalDate ;
4855import java .time .chrono .ChronoZonedDateTime ;
4956import java .time .chrono .Chronology ;
5057import java .time .format .DateTimeFormatter ;
58+ import java .time .temporal .ChronoField ;
5159import java .time .temporal .Temporal ;
5260import java .time .temporal .TemporalAccessor ;
5361import java .time .temporal .TemporalAdjuster ;
5664import java .time .temporal .TemporalQuery ;
5765import java .time .temporal .TemporalUnit ;
5866import java .time .temporal .ValueRange ;
67+ import java .util .Locale ;
5968import java .util .Objects ;
6069
6170/**
@@ -71,6 +80,23 @@ public class JodaCompatibleZonedDateTime
7180 TemporalAccessor {
7281
7382 private static final DateFormatter DATE_FORMATTER = DateFormatter .forPattern ("strict_date_time" );
83+ private static final DeprecationLogger deprecationLogger = DeprecationLogger .getLogger (JodaCompatibleZonedDateTime .class );
84+
85+ private static void logDeprecated (String key , String message , Object ... params ) {
86+ AccessController .doPrivileged (new PrivilegedAction <Void >() {
87+ @ SuppressLoggerChecks (reason = "safely delegates to logger" )
88+ @ Override
89+ public Void run () {
90+ deprecationLogger .deprecate (key , message , params );
91+ return null ;
92+ }
93+ });
94+ }
95+
96+ private static void logDeprecatedMethod (String oldMethod , String newMethod ) {
97+ logDeprecated (oldMethod , "Use of the joda time method [{}] is deprecated. Use [{}] instead." , oldMethod , newMethod );
98+ }
99+
74100 private ZonedDateTime dt ;
75101
76102 public JodaCompatibleZonedDateTime (Instant instant , ZoneId zone ) {
@@ -401,7 +427,120 @@ public ZonedDateTime withZoneSameInstant(ZoneId zone) {
401427 return dt .withZoneSameInstant (zone );
402428 }
403429
430+ @ Deprecated
431+ public long getMillis () {
432+ logDeprecatedMethod ("getMillis()" , "toInstant().toEpochMilli()" );
433+ return dt .toInstant ().toEpochMilli ();
434+ }
435+
436+ @ Deprecated
437+ public int getCenturyOfEra () {
438+ logDeprecatedMethod ("getCenturyOfEra()" , "get(ChronoField.YEAR_OF_ERA) / 100" );
439+ return dt .get (ChronoField .YEAR_OF_ERA ) / 100 ;
440+ }
441+
442+ @ Deprecated
443+ public int getEra () {
444+ logDeprecatedMethod ("getEra()" , "get(ChronoField.ERA)" );
445+ return dt .get (ChronoField .ERA );
446+ }
447+
448+ @ Deprecated
449+ public int getHourOfDay () {
450+ logDeprecatedMethod ("getHourOfDay()" , "getHour()" );
451+ return dt .getHour ();
452+ }
453+
454+ @ Deprecated
455+ public int getMillisOfDay () {
456+ logDeprecatedMethod ("getMillisOfDay()" , "get(ChronoField.MILLI_OF_DAY)" );
457+ return dt .get (ChronoField .MILLI_OF_DAY );
458+ }
459+
460+ @ Deprecated
461+ public int getMillisOfSecond () {
462+ logDeprecatedMethod ("getMillisOfSecond()" , "get(ChronoField.MILLI_OF_SECOND)" );
463+ return dt .get (ChronoField .MILLI_OF_SECOND );
464+ }
465+
466+ @ Deprecated
467+ public int getMinuteOfDay () {
468+ logDeprecatedMethod ("getMinuteOfDay()" , "get(ChronoField.MINUTE_OF_DAY)" );
469+ return dt .get (ChronoField .MINUTE_OF_DAY );
470+ }
471+
472+ @ Deprecated
473+ public int getMinuteOfHour () {
474+ logDeprecatedMethod ("getMinuteOfHour()" , "getMinute()" );
475+ return dt .getMinute ();
476+ }
477+
478+ @ Deprecated
479+ public int getMonthOfYear () {
480+ logDeprecatedMethod ("getMonthOfYear()" , "getMonthValue()" );
481+ return dt .getMonthValue ();
482+ }
483+
484+ @ Deprecated
485+ public int getSecondOfDay () {
486+ logDeprecatedMethod ("getSecondOfDay()" , "get(ChronoField.SECOND_OF_DAY)" );
487+ return dt .get (ChronoField .SECOND_OF_DAY );
488+ }
489+
490+ @ Deprecated
491+ public int getSecondOfMinute () {
492+ logDeprecatedMethod ("getSecondOfMinute()" , "getSecond()" );
493+ return dt .getSecond ();
494+ }
495+
496+ @ Deprecated
497+ public int getWeekOfWeekyear () {
498+ logDeprecatedMethod ("getWeekOfWeekyear()" , "get(DateFormatters.WEEK_FIELDS_ROOT.weekOfWeekBasedYear())" );
499+ return dt .get (DateFormatters .WEEK_FIELDS_ROOT .weekOfWeekBasedYear ());
500+ }
501+
502+ @ Deprecated
503+ public int getWeekyear () {
504+ logDeprecatedMethod ("getWeekyear()" , "get(DateFormatters.WEEK_FIELDS_ROOT.weekBasedYear())" );
505+ return dt .get (DateFormatters .WEEK_FIELDS_ROOT .weekBasedYear ());
506+ }
507+
508+ @ Deprecated
509+ public int getYearOfCentury () {
510+ logDeprecatedMethod ("getYearOfCentury()" , "get(ChronoField.YEAR_OF_ERA) % 100" );
511+ return dt .get (ChronoField .YEAR_OF_ERA ) % 100 ;
512+ }
513+
514+ @ Deprecated
515+ public int getYearOfEra () {
516+ logDeprecatedMethod ("getYearOfEra()" , "get(ChronoField.YEAR_OF_ERA)" );
517+ return dt .get (ChronoField .YEAR_OF_ERA );
518+ }
519+
520+ @ Deprecated
521+ public String toString (String format ) {
522+ logDeprecatedMethod ("toString(String)" , "a DateTimeFormatter" );
523+ // TODO: replace with bwc formatter
524+ return new DateTime (dt .toInstant ().toEpochMilli (), DateUtils .zoneIdToDateTimeZone (dt .getZone ())).toString (format );
525+ }
526+
527+ @ Deprecated
528+ public String toString (String format , Locale locale ) {
529+ logDeprecatedMethod ("toString(String,Locale)" , "a DateTimeFormatter" );
530+ // TODO: replace with bwc formatter
531+ return new DateTime (dt .toInstant ().toEpochMilli (), DateUtils .zoneIdToDateTimeZone (dt .getZone ())).toString (format , locale );
532+ }
533+
404534 public DayOfWeek getDayOfWeekEnum () {
405535 return dt .getDayOfWeek ();
406536 }
537+
538+ @ Deprecated
539+ public int getDayOfWeek () {
540+ logDeprecated (
541+ "getDayOfWeek()" ,
542+ "The return type of [getDayOfWeek()] will change to an enum in 7.0. Use getDayOfWeekEnum().getValue()."
543+ );
544+ return dt .getDayOfWeek ().getValue ();
545+ }
407546}
0 commit comments