Skip to content

Commit

Permalink
Merge "SystemReader: Offer methods with java.time API"
Browse files Browse the repository at this point in the history
  • Loading branch information
ifradeo authored and gerritforge-ltd committed Nov 8, 2024
2 parents 59f6c37 + 88f8321 commit a903ab5
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.Duration;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
Expand Down Expand Up @@ -201,6 +204,11 @@ public long getCurrentTime() {
return now;
}

@Override
public Instant now() {
return Instant.ofEpochMilli(now);
}

@Override
public MonotonicClock getClock() {
return () -> {
Expand Down Expand Up @@ -236,11 +244,21 @@ public int getTimezone(long when) {
return getTimeZone().getOffset(when) / (60 * 1000);
}

@Override
public ZoneOffset getTimeZoneAt(Instant when) {
return getTimeZoneId().getRules().getOffset(when);
}

@Override
public TimeZone getTimeZone() {
return TimeZone.getTimeZone("GMT-03:30");
}

@Override
public ZoneId getTimeZoneId() {
return ZoneOffset.ofHoursMinutes(-3, 30);
}

@Override
public Locale getLocale() {
return Locale.US;
Expand Down
14 changes: 14 additions & 0 deletions org.eclipse.jgit/.settings/.api_filters
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,18 @@
</message_arguments>
</filter>
</resource>
<resource path="src/org/eclipse/jgit/util/SystemReader.java" type="org.eclipse.jgit.util.SystemReader">
<filter id="336695337">
<message_arguments>
<message_argument value="org.eclipse.jgit.util.SystemReader"/>
<message_argument value="getTimeZoneAt(Instant)"/>
</message_arguments>
</filter>
<filter id="336695337">
<message_arguments>
<message_argument value="org.eclipse.jgit.util.SystemReader"/>
<message_argument value="now()"/>
</message_arguments>
</filter>
</resource>
</component>
61 changes: 61 additions & 0 deletions org.eclipse.jgit/src/org/eclipse/jgit/util/SystemReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
import java.nio.file.Paths;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.Locale;
import java.util.TimeZone;
import java.util.concurrent.atomic.AtomicReference;
Expand Down Expand Up @@ -166,10 +169,20 @@ public long getCurrentTime() {
return System.currentTimeMillis();
}

@Override
public Instant now() {
return Instant.now();
}

@Override
public int getTimezone(long when) {
return getTimeZone().getOffset(when) / (60 * 1000);
}

@Override
public ZoneOffset getTimeZoneAt(Instant when) {
return getTimeZoneId().getRules().getOffset(when);
}
}

/**
Expand Down Expand Up @@ -227,10 +240,20 @@ public long getCurrentTime() {
return delegate.getCurrentTime();
}

@Override
public Instant now() {
return delegate.now();
}

@Override
public int getTimezone(long when) {
return delegate.getTimezone(when);
}

@Override
public ZoneOffset getTimeZoneAt(Instant when) {
return delegate.getTimeZoneAt(when);
}
}

private static volatile SystemReader INSTANCE = DEFAULT;
Expand Down Expand Up @@ -501,9 +524,21 @@ private void updateAll(Config config)
* Get the current system time
*
* @return the current system time
*
* @deprecated Use {@link #now()}
*/
@Deprecated
public abstract long getCurrentTime();

/**
* Get the current system time
*
* @return the current system time
*
* @since 7.1
*/
public abstract Instant now();

/**
* Get clock instance preferred by this system.
*
Expand All @@ -520,19 +555,45 @@ public MonotonicClock getClock() {
* @param when
* a system timestamp
* @return the local time zone
*
* @deprecated Use {@link #getTimeZoneAt(Instant)} instead.
*/
@Deprecated
public abstract int getTimezone(long when);

/**
* Get the local time zone offset at "when" time
*
* @param when
* a system timestamp
* @return the local time zone
* @since 7.1
*/
public abstract ZoneOffset getTimeZoneAt(Instant when);

/**
* Get system time zone, possibly mocked for testing
*
* @return system time zone, possibly mocked for testing
* @since 1.2
*
* @deprecated Use {@link #getTimeZoneId()}
*/
@Deprecated
public TimeZone getTimeZone() {
return TimeZone.getDefault();
}

/**
* Get system time zone, possibly mocked for testing
*
* @return system time zone, possibly mocked for testing
* @since 7.1
*/
public ZoneId getTimeZoneId() {
return ZoneId.systemDefault();
}

/**
* Get the locale to use
*
Expand Down

0 comments on commit a903ab5

Please sign in to comment.