16
16
*/
17
17
package org .apache .calcite .avatica .util ;
18
18
19
- import org .apache .flink .table .api .ValidationException ;
20
-
21
19
import java .text .DateFormat ;
22
20
import java .text .NumberFormat ;
23
21
import java .text .ParsePosition ;
@@ -69,9 +67,6 @@ private DateTimeUtils() {}
69
67
/** The Java default time zone. */
70
68
public static final TimeZone DEFAULT_ZONE = TimeZone .getDefault ();
71
69
72
- /** User's configured time zone*/
73
- private static volatile TimeZone userZone = UTC_ZONE ;
74
-
75
70
/**
76
71
* The number of milliseconds in a second.
77
72
*/
@@ -95,19 +90,6 @@ private DateTimeUtils() {}
95
90
*/
96
91
public static final long MILLIS_PER_DAY = 86400000 ; // = 24 * 60 * 60 * 1000
97
92
98
- /**
99
- * The number of microseconds in a day.
100
- */
101
- public static final long MICROS_PER_DAY = 86400000000L ; // = 24 * 60 * 60 * 1000 * 1000
102
-
103
- /**
104
- * The number of seconds in a day.
105
- *
106
- * <p>This is the modulo 'mask' used when converting
107
- * TIMESTAMP values to DATE and TIME values.
108
- */
109
- public static final long SECONDS_PER_DAY = 86400 ; // = 24 * 60 * 60
110
-
111
93
/**
112
94
* Calendar set to the epoch (1970-01-01 00:00:00 UTC). Useful for
113
95
* initializing other values. Calendars are not immutable, so be careful not
@@ -122,13 +104,6 @@ private DateTimeUtils() {}
122
104
123
105
//~ Methods ----------------------------------------------------------------
124
106
125
- public static TimeZone getUserZone () {
126
- return userZone ;
127
- }
128
- public static void setUserZone (TimeZone zone ) {
129
- userZone = zone ;
130
- }
131
-
132
107
/**
133
108
* Parses a string using {@link SimpleDateFormat} and a given pattern. This
134
109
* method parses a string at the specified parse position and if successful,
@@ -309,48 +284,15 @@ public static SimpleDateFormat newDateFormat(String format) {
309
284
return sdf ;
310
285
}
311
286
312
- /** Helper for CAST({timestamp} Or {date} or {time} AS VARCHAR(n)). */
313
- public static String unixDateTimeToString (Object o , TimeZone tz ) {
314
- int offset = tz .getOffset (Calendar .ZONE_OFFSET );
315
- if (tz .useDaylightTime ()) {
316
- offset = tz .getOffset (((java .util .Date ) o ).getTime ());
317
- }
318
- if (o instanceof Date ) {
319
- long time = ((Date ) o ).getTime ();
320
- time = time + UTC_ZONE .getOffset (time );
321
- if (o instanceof java .sql .Date ) {
322
- return unixDateToString ((int ) (time / MILLIS_PER_DAY ) + offset );
323
- }
324
- if (o instanceof java .sql .Time ) {
325
- return unixTimeToString (((int ) (time % MILLIS_PER_DAY ) + offset ) % (int ) MILLIS_PER_DAY );
326
- }
327
- if (o instanceof java .sql .Timestamp ) {
328
- return unixTimestampToString (time + offset , 3 );
329
- }
330
- }
331
- return o .toString ();
332
- }
333
-
334
287
/** Helper for CAST({timestamp} AS VARCHAR(n)). */
335
288
public static String unixTimestampToString (long timestamp ) {
336
289
return unixTimestampToString (timestamp , 0 );
337
290
}
338
291
339
- /**
340
- * Returns the formatted timestamp string
341
- * now, it is only for Timestamp Literal (TimestampString)
342
- * don't use it for scalar functions
343
- */
344
292
public static String unixTimestampToString (long timestamp , int precision ) {
345
293
final StringBuilder buf = new StringBuilder (17 );
346
-
347
- // Because unixTimeToString does't take offset into account
348
- // so, manually adjust the offset here
349
- long tzOffset = userZone .getOffset (timestamp );
350
- timestamp += tzOffset ;
351
-
352
- int date = (int ) ((timestamp ) / MILLIS_PER_DAY );
353
- int time = (int ) ((timestamp ) % MILLIS_PER_DAY );
294
+ int date = (int ) (timestamp / MILLIS_PER_DAY );
295
+ int time = (int ) (timestamp % MILLIS_PER_DAY );
354
296
if (time < 0 ) {
355
297
--date ;
356
298
time += MILLIS_PER_DAY ;
@@ -485,7 +427,7 @@ public static StringBuilder number(StringBuilder buf, int v, int n) {
485
427
}
486
428
487
429
public static int digitCount (int v ) {
488
- for (int n = 1 ;; n ++) {
430
+ for (int n = 1 ; true ; n ++) {
489
431
v /= 10 ;
490
432
if (v == 0 ) {
491
433
return n ;
@@ -674,178 +616,63 @@ private static void fraction(StringBuilder buf, int scale, long ms) {
674
616
}
675
617
}
676
618
677
- private static boolean isInteger (String s ) {
678
- boolean isInt = s .length () > 0 ;
679
- for (int i = 0 ; i < s .length (); i ++)
680
- {
681
- if (s .charAt (i ) < '0' || s .charAt (i ) > '9' ) {
682
- isInt = false ;
683
- break ;
684
- }
685
- }
686
- return isInt ;
687
- }
688
-
689
- private static boolean isLeapYear (int s ) {
690
- return s % 400 == 0 || (s % 4 == 0 && s % 100 != 0 );
691
- }
692
-
693
- private static boolean isIllegalDate (int y , int m , int d ) {
694
- int [] monthOf31Days = new int []{1 , 3 , 5 , 7 , 8 , 10 , 12 };
695
- if (y < 0 || y > 9999 || m < 1 || m > 12 || d < 1 || d > 31 ) {
696
- return false ;
697
- }
698
- if (m == 2 && d > 28 ) {
699
- if (!(isLeapYear (y ) && d == 29 )) {
700
- return false ;
701
- }
702
- }
703
- if (d == 31 ) {
704
- for (int i : monthOf31Days ) {
705
- if (i == m ) {
706
- return true ;
707
- }
708
- }
709
- return false ;
710
- }
711
- return true ;
712
- }
713
-
714
- public static Integer dateStringToUnixDate (String s ) {
715
- // allow timestamp str to date, e.g. 2017-12-12 09:30:00.0
716
- int ws1 = s .indexOf (" " );
717
- if (ws1 > 0 ) {
718
- s = s .substring (0 , ws1 );
719
- }
619
+ public static int dateStringToUnixDate (String s ) {
720
620
int hyphen1 = s .indexOf ('-' );
721
621
int y ;
722
622
int m ;
723
623
int d ;
724
624
if (hyphen1 < 0 ) {
725
- if (!isInteger (s .trim ())) {
726
- return null ;
727
- }
728
625
y = Integer .parseInt (s .trim ());
729
626
m = 1 ;
730
627
d = 1 ;
731
628
} else {
732
- if (!isInteger (s .substring (0 , hyphen1 ).trim ())) {
733
- return null ;
734
- }
735
629
y = Integer .parseInt (s .substring (0 , hyphen1 ).trim ());
736
630
final int hyphen2 = s .indexOf ('-' , hyphen1 + 1 );
737
631
if (hyphen2 < 0 ) {
738
- if (!isInteger (s .substring (hyphen1 + 1 ).trim ())) {
739
- return null ;
740
- }
741
632
m = Integer .parseInt (s .substring (hyphen1 + 1 ).trim ());
742
633
d = 1 ;
743
634
} else {
744
- if (!isInteger (s .substring (hyphen1 + 1 , hyphen2 ).trim ())) {
745
- return null ;
746
- }
747
635
m = Integer .parseInt (s .substring (hyphen1 + 1 , hyphen2 ).trim ());
748
- if (!isInteger (s .substring (hyphen2 + 1 ).trim ())) {
749
- return null ;
750
- }
751
636
d = Integer .parseInt (s .substring (hyphen2 + 1 ).trim ());
752
637
}
753
638
}
754
- if (!isIllegalDate (y , m , d )) {
755
- return null ;
756
- }
757
639
return ymdToUnixDate (y , m , d );
758
640
}
759
641
760
- public static Integer timeStringToUnixDate (String v ) {
642
+ public static int timeStringToUnixDate (String v ) {
761
643
return timeStringToUnixDate (v , 0 );
762
644
}
763
645
764
- public static Integer timeStringToUnixDate (String v , int start ) {
646
+ public static int timeStringToUnixDate (String v , int start ) {
765
647
final int colon1 = v .indexOf (':' , start );
766
- //timezone hh:mm:ss[.ssssss][[+|-]hh:mm:ss]
767
- //refer https://www.w3.org/TR/NOTE-datetime
768
- int timezoneHour ;
769
- int timezoneMinute ;
770
648
int hour ;
771
649
int minute ;
772
650
int second ;
773
651
int milli ;
774
- int operator = -1 ;
775
- int end = v .length ();
776
- int timezone = v .indexOf ('-' , start );
777
- if (timezone < 0 )
778
- {
779
- timezone = v .indexOf ('+' , start );
780
- operator = 1 ;
781
- }
782
- if (timezone < 0 ) {
783
- timezoneHour = 0 ;
784
- timezoneMinute = 0 ;
785
- } else {
786
- end = timezone ;
787
- final int colon3 = v .indexOf (':' , timezone );
788
- if (colon3 < 0 ) {
789
- if (!isInteger (v .substring (timezone + 1 ).trim ())) {
790
- return null ;
791
- }
792
- timezoneHour = Integer .parseInt (v .substring (timezone + 1 ).trim ());
793
- timezoneMinute = 0 ;
794
- } else {
795
- if (!isInteger (v .substring (timezone + 1 , colon3 ).trim ())) {
796
- return null ;
797
- }
798
- timezoneHour = Integer .parseInt (v .substring (timezone + 1 , colon3 ).trim ());
799
- if (!isInteger (v .substring (colon3 + 1 ).trim ())) {
800
- return null ;
801
- }
802
- timezoneMinute = Integer .parseInt (v .substring (colon3 + 1 ).trim ());
803
- }
804
- }
805
652
if (colon1 < 0 ) {
806
- if (!isInteger (v .substring (start , end ).trim ())) {
807
- return null ;
808
- }
809
- hour = Integer .parseInt (v .substring (start , end ).trim ());
653
+ hour = Integer .parseInt (v .trim ());
810
654
minute = 1 ;
811
655
second = 1 ;
812
656
milli = 0 ;
813
657
} else {
814
- if (!isInteger (v .substring (start , colon1 ).trim ())) {
815
- return null ;
816
- }
817
658
hour = Integer .parseInt (v .substring (start , colon1 ).trim ());
818
659
final int colon2 = v .indexOf (':' , colon1 + 1 );
819
660
if (colon2 < 0 ) {
820
- if (!isInteger (v .substring (colon1 + 1 , end ).trim ())) {
821
- return null ;
822
- }
823
- minute = Integer .parseInt (v .substring (colon1 + 1 , end ).trim ());
661
+ minute = Integer .parseInt (v .substring (colon1 + 1 ).trim ());
824
662
second = 1 ;
825
663
milli = 0 ;
826
664
} else {
827
- if (!isInteger (v .substring (colon1 + 1 , colon2 ).trim ())) {
828
- return null ;
829
- }
830
665
minute = Integer .parseInt (v .substring (colon1 + 1 , colon2 ).trim ());
831
666
int dot = v .indexOf ('.' , colon2 );
832
667
if (dot < 0 ) {
833
- if (!isInteger (v .substring (colon2 + 1 , end ).trim ())) {
834
- return null ;
835
- }
836
- second = Integer .parseInt (v .substring (colon2 + 1 , end ).trim ());
668
+ second = Integer .parseInt (v .substring (colon2 + 1 ).trim ());
837
669
milli = 0 ;
838
670
} else {
839
- if (!isInteger (v .substring (colon2 + 1 , dot ).trim ())) {
840
- return null ;
841
- }
842
671
second = Integer .parseInt (v .substring (colon2 + 1 , dot ).trim ());
843
- milli = parseFraction (v .substring (dot + 1 , end ).trim (), 100 );
672
+ milli = parseFraction (v .substring (dot + 1 ).trim (), 100 );
844
673
}
845
674
}
846
675
}
847
- hour += operator * timezoneHour ;
848
- minute += operator * timezoneMinute ;
849
676
return hour * (int ) MILLIS_PER_HOUR
850
677
+ minute * (int ) MILLIS_PER_MINUTE
851
678
+ second * (int ) MILLIS_PER_SECOND
@@ -976,7 +803,7 @@ public static int unixTimeExtract(TimeUnitRange range, int time) {
976
803
final int seconds = time / (int ) MILLIS_PER_SECOND ;
977
804
return seconds % 60 ;
978
805
default :
979
- throw new ValidationException ( "unit " + range + " can not be applied to time variable" );
806
+ throw new AssertionError ( range );
980
807
}
981
808
}
982
809
@@ -1133,7 +960,7 @@ public static int subtractMonths(int date0, int date1) {
1133
960
// Start with an estimate.
1134
961
// Since no month has more than 31 days, the estimate is <= the true value.
1135
962
int m = (date0 - date1 ) / 31 ;
1136
- for (;; ) {
963
+ while ( true ) {
1137
964
int date2 = addMonths (date1 , m );
1138
965
if (date2 >= date0 ) {
1139
966
return m ;
0 commit comments