-
Notifications
You must be signed in to change notification settings - Fork 1.1k
google-cloud-core: Optimize Timestamp.parseTimestamp(String) #4831
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
olavloite
wants to merge
11
commits into
googleapis:master
from
olavloite:core-optimize-parse-date-timestamp
Closed
Changes from 10 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7bd1485
optimize parsing of dates and timestamps
olavloite fee061b
fixed formatting
olavloite 16657ee
fixed parsing nanoseconds from less than 9 digits
olavloite 85aee11
switch statement is faster than if-statement
olavloite acf3c11
fixed formatting
olavloite b1feca4
added additional tests
olavloite be151f8
T and Z should be case insensitive
olavloite 2cd776d
removed date parsing (moved to separate PR)
olavloite 03384a0
Integer.parseInt is just as efficient
olavloite 82a8257
added try-catch for NumberFormatException
olavloite 63001ff
removed unnecessary else and put calculation on one line
olavloite File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,17 +18,15 @@ | |
|
|
||
| import static com.google.common.base.Preconditions.checkArgument; | ||
|
|
||
| import com.google.api.client.util.Preconditions; | ||
| import com.google.protobuf.util.Timestamps; | ||
| import java.io.Serializable; | ||
| import java.util.Date; | ||
| import java.util.Objects; | ||
| import java.util.concurrent.TimeUnit; | ||
| import org.threeten.bp.Instant; | ||
| import org.threeten.bp.LocalDateTime; | ||
| import org.threeten.bp.ZoneOffset; | ||
| import org.threeten.bp.format.DateTimeFormatter; | ||
| import org.threeten.bp.format.DateTimeFormatterBuilder; | ||
| import org.threeten.bp.temporal.TemporalAccessor; | ||
|
|
||
| /** | ||
| * Represents a timestamp with nanosecond precision. Timestamps cover the range [0001-01-01, | ||
|
|
@@ -49,15 +47,6 @@ public final class Timestamp implements Comparable<Timestamp>, Serializable { | |
|
|
||
| private static final DateTimeFormatter format = DateTimeFormatter.ISO_LOCAL_DATE_TIME; | ||
|
|
||
| private static final DateTimeFormatter timestampParser = | ||
| new DateTimeFormatterBuilder() | ||
| .appendOptional(DateTimeFormatter.ISO_LOCAL_DATE_TIME) | ||
| .optionalStart() | ||
| .appendOffsetId() | ||
| .optionalEnd() | ||
| .toFormatter() | ||
| .withZone(ZoneOffset.UTC); | ||
|
|
||
| private final long seconds; | ||
| private final int nanos; | ||
|
|
||
|
|
@@ -176,14 +165,64 @@ public com.google.protobuf.Timestamp toProto() { | |
| return com.google.protobuf.Timestamp.newBuilder().setSeconds(seconds).setNanos(nanos).build(); | ||
| } | ||
|
|
||
| private static final int[] POWERS_OF_10 = { | ||
| 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000, 1000000000 | ||
| }; | ||
|
|
||
| /** | ||
| * Creates a Timestamp instance from the given string. String is in the RFC 3339 format without | ||
| * the timezone offset (always ends in "Z"). | ||
| */ | ||
| public static Timestamp parseTimestamp(String timestamp) { | ||
| TemporalAccessor temporalAccessor = timestampParser.parse(timestamp); | ||
| Instant instant = Instant.from(temporalAccessor); | ||
| return ofTimeSecondsAndNanos(instant.getEpochSecond(), instant.getNano()); | ||
| Preconditions.checkNotNull(timestamp); | ||
| final String invalidTimestamp = "Invalid timestamp: " + timestamp; | ||
| Preconditions.checkArgument( | ||
| timestamp.length() >= 19 && timestamp.length() <= 30, invalidTimestamp); | ||
| Preconditions.checkArgument(timestamp.charAt(4) == '-', invalidTimestamp); | ||
| Preconditions.checkArgument(timestamp.charAt(7) == '-', invalidTimestamp); | ||
| Preconditions.checkArgument( | ||
| timestamp.charAt(10) == 'T' || timestamp.charAt(10) == 't', invalidTimestamp); | ||
| Preconditions.checkArgument( | ||
| (timestamp.length() == 19 && (timestamp.charAt(18) != 'Z' || timestamp.charAt(18) == 'z')) | ||
| || (timestamp.length() == 20 | ||
| && (timestamp.charAt(19) == 'Z' || timestamp.charAt(19) == 'z')) | ||
| || (timestamp.charAt(19) == '.' | ||
| && timestamp.length() > 20 | ||
| && (timestamp.charAt(20) != 'Z' || timestamp.charAt(20) == 'z')), | ||
| invalidTimestamp); | ||
| try { | ||
| int year = Integer.parseInt(timestamp.substring(0, 4)); | ||
| int month = Integer.parseInt(timestamp.substring(5, 7)); | ||
| int day = Integer.parseInt(timestamp.substring(8, 10)); | ||
| int hour = Integer.parseInt(timestamp.substring(11, 13)); | ||
| int minute = Integer.parseInt(timestamp.substring(14, 16)); | ||
| int second = Integer.parseInt(timestamp.substring(17, 19)); | ||
| int fraction = 0; | ||
| if (timestamp.length() > 20) { | ||
| if (timestamp.charAt(19) == '.') { | ||
| int endIndex; | ||
| if (timestamp.charAt(timestamp.length() - 1) == 'Z' | ||
| || timestamp.charAt(timestamp.length() - 1) == 'z') { | ||
| endIndex = timestamp.length() - 1; | ||
| } else { | ||
| endIndex = timestamp.length(); | ||
| } | ||
| if (endIndex - 20 > 9) { | ||
| throw new IllegalArgumentException(invalidTimestamp); | ||
| } | ||
| fraction = Integer.parseInt(timestamp.substring(20, endIndex)); | ||
| // Adjust the result to nanoseconds if the input length is less than 9 digits (9 - | ||
| // (endIndex | ||
| // - 20)). | ||
| fraction *= POWERS_OF_10[9 - (endIndex - 20)]; | ||
| } else { | ||
|
||
| } | ||
| } | ||
| LocalDateTime ldt = LocalDateTime.of(year, month, day, hour, minute, second, fraction); | ||
| return ofTimeSecondsAndNanos(ldt.toEpochSecond(ZoneOffset.UTC), ldt.getNano()); | ||
| } catch (NumberFormatException e) { | ||
| throw new IllegalArgumentException(invalidTimestamp, e); | ||
| } | ||
| } | ||
|
|
||
| private StringBuilder toString(StringBuilder b) { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please have the mathematical formula on a single line, if possible.
(9 - (endIndex - 20)).