@@ -64,6 +64,10 @@ public static LocalDateTime timestampFromMicros(long microsFromEpoch) {
64
64
return ChronoUnit .MICROS .addTo (EPOCH , microsFromEpoch ).toLocalDateTime ();
65
65
}
66
66
67
+ public static LocalDateTime timestampFromNanos (long nanosFromEpoch ) {
68
+ return ChronoUnit .NANOS .addTo (EPOCH , nanosFromEpoch ).toLocalDateTime ();
69
+ }
70
+
67
71
public static long microsFromInstant (Instant instant ) {
68
72
return ChronoUnit .MICROS .between (EPOCH , instant .atOffset (ZoneOffset .UTC ));
69
73
}
@@ -72,6 +76,10 @@ public static long microsFromTimestamp(LocalDateTime dateTime) {
72
76
return ChronoUnit .MICROS .between (EPOCH , dateTime .atOffset (ZoneOffset .UTC ));
73
77
}
74
78
79
+ public static long nanosFromTimestamp (LocalDateTime dateTime ) {
80
+ return ChronoUnit .NANOS .between (EPOCH , dateTime .atOffset (ZoneOffset .UTC ));
81
+ }
82
+
75
83
public static long microsToMillis (long micros ) {
76
84
// When the timestamp is negative, i.e before 1970, we need to adjust the milliseconds portion.
77
85
// Example - 1965-01-01 10:11:12.123456 is represented as (-157700927876544) in micro precision.
@@ -99,10 +107,18 @@ public static OffsetDateTime timestamptzFromMicros(long microsFromEpoch) {
99
107
return ChronoUnit .MICROS .addTo (EPOCH , microsFromEpoch );
100
108
}
101
109
110
+ public static OffsetDateTime timestamptzFromNanos (long nanosFromEpoch ) {
111
+ return ChronoUnit .NANOS .addTo (EPOCH , nanosFromEpoch );
112
+ }
113
+
102
114
public static long microsFromTimestamptz (OffsetDateTime dateTime ) {
103
115
return ChronoUnit .MICROS .between (EPOCH , dateTime );
104
116
}
105
117
118
+ public static long nanosFromTimestamptz (OffsetDateTime dateTime ) {
119
+ return ChronoUnit .NANOS .between (EPOCH , dateTime );
120
+ }
121
+
106
122
public static String formatTimestampMillis (long millis ) {
107
123
return Instant .ofEpochMilli (millis ).toString ().replace ("Z" , "+00:00" );
108
124
}
@@ -126,11 +142,27 @@ public static String microsToIsoTimestamptz(long micros) {
126
142
return localDateTime .atOffset (ZoneOffset .UTC ).format (zeroOffsetFormatter );
127
143
}
128
144
145
+ public static String nanosToIsoTimestamptz (long nanos ) {
146
+ LocalDateTime localDateTime = timestampFromNanos (nanos );
147
+ DateTimeFormatter zeroOffsetFormatter =
148
+ new DateTimeFormatterBuilder ()
149
+ .parseCaseInsensitive ()
150
+ .append (DateTimeFormatter .ISO_LOCAL_DATE_TIME )
151
+ .appendOffset ("+HH:MM:ss" , "+00:00" )
152
+ .toFormatter ();
153
+ return localDateTime .atOffset (ZoneOffset .UTC ).format (zeroOffsetFormatter );
154
+ }
155
+
129
156
public static String microsToIsoTimestamp (long micros ) {
130
157
LocalDateTime localDateTime = timestampFromMicros (micros );
131
158
return localDateTime .format (DateTimeFormatter .ISO_LOCAL_DATE_TIME );
132
159
}
133
160
161
+ public static String nanosToIsoTimestamp (long nanos ) {
162
+ LocalDateTime localDateTime = timestampFromNanos (nanos );
163
+ return localDateTime .format (DateTimeFormatter .ISO_LOCAL_DATE_TIME );
164
+ }
165
+
134
166
public static int isoDateToDays (String dateString ) {
135
167
return daysFromDate (LocalDate .parse (dateString , DateTimeFormatter .ISO_LOCAL_DATE ));
136
168
}
@@ -144,6 +176,11 @@ public static long isoTimestamptzToMicros(String timestampString) {
144
176
OffsetDateTime .parse (timestampString , DateTimeFormatter .ISO_DATE_TIME ));
145
177
}
146
178
179
+ public static long isoTimestamptzToNanos (String timestampString ) {
180
+ return nanosFromTimestamptz (
181
+ OffsetDateTime .parse (timestampString , DateTimeFormatter .ISO_DATE_TIME ));
182
+ }
183
+
147
184
public static boolean isUTCTimestamptz (String timestampString ) {
148
185
OffsetDateTime offsetDateTime =
149
186
OffsetDateTime .parse (timestampString , DateTimeFormatter .ISO_DATE_TIME );
@@ -155,6 +192,11 @@ public static long isoTimestampToMicros(String timestampString) {
155
192
LocalDateTime .parse (timestampString , DateTimeFormatter .ISO_LOCAL_DATE_TIME ));
156
193
}
157
194
195
+ public static long isoTimestampToNanos (String timestampString ) {
196
+ return nanosFromTimestamp (
197
+ LocalDateTime .parse (timestampString , DateTimeFormatter .ISO_LOCAL_DATE_TIME ));
198
+ }
199
+
158
200
public static int daysToYears (int days ) {
159
201
return convertDays (days , ChronoUnit .YEARS );
160
202
}
0 commit comments