-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9aa27b8
commit 0bd149f
Showing
6 changed files
with
78 additions
and
154 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
34 changes: 28 additions & 6 deletions
34
src/main/java/cc/projectnexus/adapters/java/utils/JavaScriptUtils.java
This file contains 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 |
---|---|---|
@@ -1,27 +1,49 @@ | ||
package cc.projectnexus.adapters.java.utils; | ||
|
||
import cc.projectnexus.adapters.java.datamodels.GuildSettings; | ||
import cc.projectnexus.adapters.java.datamodels.Infraction; | ||
import com.google.gson.Gson; | ||
import com.google.gson.JsonObject; | ||
|
||
import java.sql.Timestamp; | ||
import java.time.Instant; | ||
import java.time.LocalDateTime; | ||
import java.time.ZoneOffset; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
public class JavaScriptUtils { | ||
|
||
public static String timestampJsToJava(String jsTimestamp) { | ||
public static Timestamp timestampJsToJava(String jsTimestamp) { | ||
DateTimeFormatter jsFormatter = DateTimeFormatter.ISO_INSTANT; | ||
DateTimeFormatter javaFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS"); | ||
String javaTimestampString; | ||
|
||
try { | ||
Instant instant = Instant.from(jsFormatter.parse(jsTimestamp)); | ||
LocalDateTime javaDateTime = LocalDateTime.ofInstant(instant, ZoneOffset.UTC); | ||
javaTimestampString = javaDateTime.format(javaFormatter); | ||
return Timestamp.valueOf(javaDateTime); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
return null; // or return an empty string, depending on your preference | ||
return null; // or handle the error as needed | ||
} | ||
} | ||
|
||
public static void setTimestampFields(GuildSettings guildSettings, String response) { | ||
JsonObject responseJson = new Gson().fromJson(response, JsonObject.class); | ||
|
||
String createdAt = responseJson.get("createdAt").getAsString(); | ||
String updatedAt = responseJson.get("lastUpdated").getAsString(); | ||
|
||
guildSettings.setCreatedAt(timestampJsToJava(createdAt)); | ||
guildSettings.setUpdatedAt(timestampJsToJava(updatedAt)); | ||
} | ||
|
||
public static void setTimestampFields(Infraction infraction, String response) { | ||
JsonObject responseJson = new Gson().fromJson(response, JsonObject.class); | ||
|
||
String createdAt = responseJson.get("createdAt").getAsString(); | ||
String updatedAt = responseJson.get("lastUpdated").getAsString(); | ||
|
||
return javaTimestampString; | ||
infraction.setCreatedAt(timestampJsToJava(createdAt)); | ||
infraction.setUpdatedAt(timestampJsToJava(updatedAt)); | ||
} | ||
|
||
} |