Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package io.github.steveswinsburg;
- import static org.junit.Assert.assertEquals;
- import java.sql.Date;
- import java.sql.Timestamp;
- import java.time.ZoneOffset;
- import java.util.Calendar;
- import java.util.TimeZone;
- import org.junit.Test;
- import ca.uhn.fhir.model.api.TemporalPrecisionEnum;
- import ca.uhn.fhir.model.primitive.DateTimeDt;
- /**
- * Some tests for {@link DateTimeDt} to help me troubleshoot a timezone issue
- */
- public class DateTimeDtTest {
- /**
- * Ensure that a string version of an offset can be mapped
- */
- @Test
- public void testOffset() {
- ZoneOffset offset = ZoneOffset.of("-0300");
- assertEquals("Didn't map offset correctly", "-03:00", offset.getId());
- }
- /**
- * Ensure that a string version of an offset can be mapped into a timezone
- */
- @Test
- public void testTimezoneFromOffset() {
- ZoneOffset offset = ZoneOffset.of("-0300");
- TimeZone timezone = TimeZone.getTimeZone(offset);
- assertEquals("Didn't map timezone correctly", "GMT-03:00", timezone.getDisplayName());
- }
- /**
- * A {@link DateTimeDt} created with the basic constructor uses the default timezone
- */
- @Test
- public void testDateInDefaultTimezone() {
- Date date = new Date();
- TimeZone timezone = TimeZone.getDefault();
- DateTimeDt fhirDate = new DateTimeDt(date);
- assertEquals("Timezone expected was not default", timezone, fhirDate.getTimeZone());
- }
- /**
- * A {@link DateTimeDt} created with the full constructor should use the provided timezone
- */
- @Test
- public void testDateInProvidedTimezone() {
- Date date = new Date();
- TimeZone timezone = TimeZone.getTimeZone("America/Los_Angeles");
- DateTimeDt fhirDate = new DateTimeDt(date, TemporalPrecisionEnum.SECOND, timezone);
- assertEquals("Timezone was not as expected", timezone, fhirDate.getTimeZone());
- }
- /**
- * A {@link DateTimeDt} created with the full constructor and a timezone created from a string offset, should use the provided timezone and map it across correctly
- */
- @Test
- public void testDateInProvidedTimezoneFromOffset() {
- Date date = new Date();
- ZoneOffset offset = ZoneOffset.of("-0300");
- TimeZone timezone = TimeZone.getTimeZone(offset);
- DateTimeDt fhirDate = new DateTimeDt(date, TemporalPrecisionEnum.SECOND, timezone);
- assertEquals("Timezone was not as expected", timezone, fhirDate.getTimeZone());
- }
- /**
- * A {@link DateTimeDt} created with the full constructor using a timestamp instead of a date, and a timezone created from a string offset, should use the provided timezone and map it across correctly
- */
- @Test
- public void testDateInProvidedTimezoneFromTimestamp() {
- Timestamp timestamp = new Timestamp();
- ZoneOffset offset = ZoneOffset.of("-0300");
- TimeZone timezone = TimeZone.getTimeZone(offset);
- DateTimeDt fhirDate = new DateTimeDt(timestamp, TemporalPrecisionEnum.SECOND, timezone);
- assertEquals("Timezone was not as expected", timezone, fhirDate.getTimeZone());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement