Skip to content
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

Cndit 1243 externalize timezone #155

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ public class ElrDeadLetterModel {
@Column(name="message")
private String message;

@Basic(optional = false)
@Column(name = "created_on",insertable = false, updatable = false)
@Temporal(TemporalType.TIMESTAMP)
@Column(name = "created_on")
private Timestamp createdOn;

@Column(name = "updated_on")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,24 +1,48 @@
package gov.cdc.dataingestion.share.helper;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;

@Service
public class TimeStampHelper {
private TimeStampHelper() {

private static String timeZone;

private TimeStampHelper() {
}

/**
* Create timestamp with the given timezone. Default UTC
* Timezones: UTC America/New_York America/Chicago America/Denver America/Phoenix America/Los_Angeles
* @return Timestamp
*/
public static Timestamp getCurrentTimeStamp() {
// Another Option: Timestamp.from(ZonedDateTime.now().toInstant()) //NOSONAR
//return Timestamp.from(Instant.now());//old implementation. //NOSONAR

if (timeZone == null || timeZone.isBlank()) {
timeZone="UTC";
}
LocalDateTime ldt = LocalDateTime.now();
ZonedDateTime zdt = ZonedDateTime.of(ldt, ZoneId.systemDefault());
ZonedDateTime gmt = zdt.withZoneSameInstant(ZoneId.of("UTC"));
ZonedDateTime gmt = zdt.withZoneSameInstant(ZoneId.of(timeZone));
return Timestamp.valueOf(gmt.toLocalDateTime());
}
public static Instant getInstantNow() {
return Instant.now();
}
}
@SuppressWarnings("java:S2696")
@Value("${app.timezone}")
public void setEnvTimeZone(String envTimeZone){
timeZone=envTimeZone;
}
public String getEnvTimeZone(){
return timeZone;
}
}
21 changes: 20 additions & 1 deletion data-ingestion-service/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ spring:
show-sql: true
hibernate:
ddl-auto: none
properties:
hibernate:
default_schema: dbo
format_sql: true
jdbc:
time_zone: UTC
timezone:
default_storage: NORMALIZE
kafka:
bootstrap-servers: ${BOOTSTRAP_SERVERS}
group-id: data-ingestion-group
Expand Down Expand Up @@ -111,6 +119,14 @@ spring:
show-sql: true
hibernate:
ddl-auto: none
properties:
hibernate:
default_schema: dbo
format_sql: true
jdbc:
time_zone: UTC
timezone:
default_storage: NORMALIZE
kafka:
bootstrap-servers: ${BOOTSTRAP_SERVERS}
group-id: data-ingestion-group
Expand Down Expand Up @@ -155,4 +171,7 @@ sftp:
enabled: ${DI_SFTP_ENABLED:disabled}
host: ${DI_SFTP_HOST:dummyhost}
username: ${DI_SFTP_USER:dummyuser}
password: ${DI_SFTP_PWD:dummypwd}
password: ${DI_SFTP_PWD:dummypwd}

app:
timezone: ${DI_TIMEZONE:UTC}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package gov.cdc.dataingestion.share.helper;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullSource;
import org.junit.jupiter.params.provider.ValueSource;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

class TimeStampHelperTest {

@ParameterizedTest
@ValueSource(strings = {"UTC", ""})
@NullSource
void testGetCurrentTimeStampWithEnvTimeZone(String timeStamp) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
Constructor<TimeStampHelper> pcc = TimeStampHelper.class.getDeclaredConstructor();
pcc.setAccessible(true);
TimeStampHelper privateConstructorInstance = pcc.newInstance();
privateConstructorInstance.setEnvTimeZone(timeStamp);
assertEquals(timeStamp, privateConstructorInstance.getEnvTimeZone());
assertNotNull(TimeStampHelper.getCurrentTimeStamp());
}
}
Loading