Skip to content

Commit

Permalink
Fixed DST problem
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderJonsson committed Apr 8, 2023
1 parent 632340d commit fc0fd39
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 13 deletions.
52 changes: 49 additions & 3 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

group 'se.alexanderjonsson'
version '0.1.0'
version '0.1.1'

java {
toolchain {
Expand Down
41 changes: 32 additions & 9 deletions src/main/java/se/alexanderjonsson/InternetTime.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@

import java.time.LocalTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;

public final class InternetTime {
/**
* This method returns the current time in Swatch .beats as an integer derived from the
* following formula: (currentTimeInSeconds / 86400 * 1000).
*
* @return an Integer representing the current time in .beats
*/
public static Integer getCurrentTimeAsInteger() {
Expand All @@ -17,6 +21,7 @@ public static Integer getCurrentTimeAsInteger() {
/**
* This method returns the current time in Swatch .beats as a Double derived from the
* following formula: (currentTimeInSeconds / 86400 * 1000).
*
* @return a Double representing the current time in .beats
*/
public static Double getCurrentTimeAsDouble() {
Expand All @@ -33,17 +38,23 @@ public static Double getCurrentTimeAsDouble() {
* @return a string formatted according to the chosen parameter.
*/
public static String getCurrentTimeAsString(TimeFormat format) {
switch (format){
case WITH_CENTIBEATS: return String.format("%06.2f",getCurrentTimeAsDouble()).replace(",",".");
case WITHOUT_CENTIBEATS: return String.format("%03d",getCurrentTimeAsInteger()).replace(",",".");
case WITHOUT_CENTIBEATS_WITHOUT_LEADING_ZEROES: return String.format("%s",getCurrentTimeAsInteger()).replace(",",".");
case CENTIBEATS_WITHOUT_LEADING_ZEROES: return String.format("%.2f",getCurrentTimeAsDouble()).replace(",",".");
default: return getCurrentTimeAsString();
switch (format) {
case WITH_CENTIBEATS:
return String.format("%06.2f", getCurrentTimeAsDouble()).replace(",", ".");
case WITHOUT_CENTIBEATS:
return String.format("%03d", getCurrentTimeAsInteger()).replace(",", ".");
case WITHOUT_CENTIBEATS_WITHOUT_LEADING_ZEROES:
return String.format("%s", getCurrentTimeAsInteger()).replace(",", ".");
case CENTIBEATS_WITHOUT_LEADING_ZEROES:
return String.format("%.2f", getCurrentTimeAsDouble()).replace(",", ".");
default:
return getCurrentTimeAsString();
}
}

/**
* This method overloads getCurrentTimeAsString(TimeFormat format)
*
* @return a String representing the current time in .beats
*/
public static String getCurrentTimeAsString() {
Expand All @@ -52,17 +63,29 @@ public static String getCurrentTimeAsString() {

/**
* This method returns the current time in Zurich as number of seconds since midnight in order to be used in the Swatch Internet Time calculations
*
* @return the number of seconds since midnight in Zurich
*/
private static long getCurrentTimeInZurichAsSeconds() {
ZoneId timezone = ZoneId.of("Europe/Zurich");
LocalTime now = LocalTime.now(timezone);
ZonedDateTime now = ZonedDateTime.now(timezone);
boolean isDST = now.getZone().getRules().isDaylightSavings(now.toInstant());

if (isDST) {
ZoneOffset offset = ZoneOffset.of("+01:00"); // Set the offset to +01:00 (Zurich time without DST)
now = now.withZoneSameInstant(offset); // Convert to Zurich time without DST


} else {
now = now.withZoneSameInstant(timezone); // Convert to non-DST time zone
}

LocalTime localNow = now.toLocalTime();
LocalTime midnight = LocalTime.MIDNIGHT;
return now.toSecondOfDay() - midnight.toSecondOfDay();
return localNow.toSecondOfDay() - midnight.toSecondOfDay();
}

/**
*
* @return a pretty string representation of the current time in .beats.
*/
@Override
Expand Down

0 comments on commit fc0fd39

Please sign in to comment.