Skip to content

Commit 1f5b3de

Browse files
authored
Fix Time Zone Name to include Standard / DST
- Restores the previous behavior of "Eastern Standard Time" & "Eastern Daylight Time" vs "Eastern Time" - Reduce duplication on documentation - clarification of null formatting - TODO - restore previous behavior in toXML and toJSON of including the timezone offset in the format of -05:00
1 parent 695d03e commit 1f5b3de

1 file changed

Lines changed: 31 additions & 59 deletions

File tree

src/main/java/com/kosherjava/zmanim/util/ZmanimFormatter.java

Lines changed: 31 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import java.time.ZoneId;
2727
import java.time.ZonedDateTime;
2828
import java.time.format.DateTimeFormatter;
29-
import java.time.format.TextStyle;
3029
import com.kosherjava.zmanim.AstronomicalCalendar;
3130

3231
/**
@@ -349,13 +348,15 @@ public String formatXSDDurationTime(Time time) {
349348
* format used is:
350349
*
351350
* <pre>
352-
* &lt;AstronomicalTimes date=&quot;1969-02-08&quot; type=&quot;com.kosherjava.zmanim.AstronomicalCalendar algorithm=&quot;US Naval Almanac Algorithm&quot; location=&quot;Lakewood, NJ&quot; latitude=&quot;40.095965&quot; longitude=&quot;-74.22213&quot; elevation=&quot;31.0&quot; timeZoneName=&quot;Eastern Standard Time&quot; timeZoneID=&quot;America/New_York&quot; timeZoneOffset=&quot;-5&quot;&gt;
353-
* &lt;Sunrise&gt;2007-02-18T06:45:27-05:00&lt;/Sunrise&gt;
354-
* &lt;TemporalHour&gt;PT54M17.529S&lt;/TemporalHour&gt;
351+
* &lt;AstronomicalTimes date=&quot;1969-02-08&quot; type=&quot;com.kosherjava.zmanim.AstronomicalCalendar algorithm=&quot;US Naval Almanac Algorithm&quot; location=&quot;Montreal, Quebec&quot; latitude=&quot;45.497&quot; longitude=&quot;-73.63&quot; elevation=&quot;85.0&quot; timeZoneName=&quot;Eastern Standard Time&quot; timeZoneID=&quot;America/New_York&quot; timeZoneOffset=&quot;-5&quot;&gt;
352+
* &lt;SeaLevelSunset&gt;1969-02-08T17:11:26-05:00&lt;/SeaLevelSunset&gt;
353+
* &lt;TemporalHour&gt;PT50M23.259S&lt;/TemporalHour&gt;
355354
* ...
356355
* &lt;/AstronomicalTimes&gt;
357356
* </pre>
358357
*
358+
* If a zman does not occur, the value "N/A" will be returned.
359+
*
359360
* Note that the output uses the <a href="http://www.w3.org/TR/xmlschema11-2/#dateTime">xsd:dateTime</a> format for
360361
* times such as sunrise, and <a href="http://www.w3.org/TR/xmlschema11-2/#duration">xsd:duration</a> format for
361362
* times that are a duration such as the length of a
@@ -364,15 +365,7 @@ public String formatXSDDurationTime(Time time) {
364365
*
365366
* @param astronomicalCalendar the AstronomicalCalendar Object
366367
*
367-
* @return The XML formatted <code>String</code>. The format will be:
368-
*
369-
* <pre>
370-
* &lt;AstronomicalTimes date=&quot;1969-02-08&quot; type=&quot;com.kosherjava.zmanim.AstronomicalCalendar algorithm=&quot;US Naval Almanac Algorithm&quot; location=&quot;Lakewood, NJ&quot; latitude=&quot;40.095965&quot; longitude=&quot;-74.22213&quot; elevation=&quot;31.0&quot; timeZoneName=&quot;Eastern Standard Time&quot; timeZoneID=&quot;America/New_York&quot; timeZoneOffset=&quot;-5&quot;&gt;
371-
* &lt;Sunrise&gt;2007-02-18T06:45:27-05:00&lt;/Sunrise&gt;
372-
* &lt;TemporalHour&gt;PT54M17.529S&lt;/TemporalHour&gt;
373-
* ...
374-
* &lt;/AstronomicalTimes&gt;
375-
* </pre>
368+
* @return The XML <code>String</code> formatted as described above.
376369
*
377370
* @todo Add proper schema, and support for nulls. XSD duration (for solar hours), should probably return nil and not P.
378371
*/
@@ -383,7 +376,10 @@ public static String toXML(AstronomicalCalendar astronomicalCalendar) {
383376
df = df.withZone(astronomicalCalendar.getGeoLocation().getZoneId());
384377

385378
LocalDate localDate = astronomicalCalendar.getLocalDate();
386-
ZoneId zi = astronomicalCalendar.getGeoLocation().getZoneId();
379+
GeoLocation geoLocation = astronomicalCalendar.getGeoLocation();
380+
ZonedDateTime lastMidnight = ZonedDateTime.of(astronomicalCalendar.getLocalDate(), LocalTime.MIDNIGHT, astronomicalCalendar.getGeoLocation().getZoneId());
381+
double offsetHours = lastMidnight.getOffset().getTotalSeconds() / 3600.0;
382+
String timeZoneName = lastMidnight.format(DateTimeFormatter.ofPattern("zzzz", Locale.getDefault()));
387383

388384
StringBuilder sb = new StringBuilder("<");
389385
boolean isAstronomicalCalendar = astronomicalCalendar.getClass().getName().equals("com.kosherjava.zmanim.AstronomicalCalendar");
@@ -412,14 +408,10 @@ public static String toXML(AstronomicalCalendar astronomicalCalendar) {
412408
sb.append(" latitude=\"").append(astronomicalCalendar.getGeoLocation().getLatitude()).append("\"");
413409
sb.append(" longitude=\"").append(astronomicalCalendar.getGeoLocation().getLongitude()).append("\"");
414410
sb.append(" elevation=\"").append(astronomicalCalendar.getGeoLocation().getElevation()).append("\"");
415-
sb.append(" timeZoneName=\"").append(zi.getDisplayName(TextStyle.FULL, Locale.getDefault())).append("\"");
416-
sb.append(" timeZoneID=\"").append(zi.getId()).append("\"");
417-
418-
ZonedDateTime lastMidnight = ZonedDateTime.of(astronomicalCalendar.getLocalDate(), LocalTime.MIDNIGHT, astronomicalCalendar.getGeoLocation().getZoneId());
419-
double offsetHours = lastMidnight.getOffset().getTotalSeconds() / 3600.0;
411+
sb.append(" timeZoneName=\"").append(timeZoneName).append("\"");
412+
sb.append(" timeZoneID=\"").append(geoLocation.getZoneId().getId()).append("\"");
420413
sb.append(" timeZoneOffset=\"").append(offsetHours).append("\"");
421414
//sb.append(" useElevationAllZmanim=\"").append(astronomicalCalendar.useElevationAllZmanim()).append("\""); //TODO likely using reflection
422-
423415
sb.append(">\n");
424416

425417
Method[] theMethods = astronomicalCalendar.getClass().getMethods();
@@ -497,16 +489,16 @@ public static String toXML(AstronomicalCalendar astronomicalCalendar) {
497489
* &quot;date&quot;:&quot;1969-02-08&quot;,
498490
* &quot;type&quot;:&quot;com.kosherjava.zmanim.AstronomicalCalendar&quot;,
499491
* &quot;algorithm&quot;:&quot;US Naval Almanac Algorithm&quot;,
500-
* &quot;location&quot;:&quot;Lakewood, NJ&quot;,
501-
* &quot;latitude&quot;:&quot;40.095965&quot;,
502-
* &quot;longitude&quot;:&quot;-74.22213&quot;,
503-
* &quot;elevation:&quot;31.0&quot;,
492+
* &quot;location&quot;:&quot;Montreal, Quebec&quot;,
493+
* &quot;latitude&quot;:&quot;45.497&quot;,
494+
* &quot;longitude&quot;:&quot;-73.63&quot;,
495+
* &quot;elevation:&quot;85.0&quot;,
504496
* &quot;timeZoneName&quot;:&quot;Eastern Standard Time&quot;,
505497
* &quot;timeZoneID&quot;:&quot;America/New_York&quot;,
506498
* &quot;timeZoneOffset&quot;:&quot;-5&quot;},
507499
* &quot;AstronomicalTimes&quot;:{
508-
* &quot;Sunrise&quot;:&quot;2007-02-18T06:45:27-05:00&quot;,
509-
* &quot;TemporalHour&quot;:&quot;PT54M17.529S&quot;
500+
* &quot;SeaLevelSunset&quot;:&quot;1969-02-08T17:11:26-05:00&quot;,
501+
* &quot;TemporalHour&quot;:&quot;PT50M23.259S&quot;
510502
* ...
511503
* }
512504
* }
@@ -516,30 +508,11 @@ public static String toXML(AstronomicalCalendar astronomicalCalendar) {
516508
* times such as sunrise, and <a href="http://www.w3.org/TR/xmlschema11-2/#duration">xsd:duration</a> format for
517509
* times that are a duration such as the length of a
518510
* {@link com.kosherjava.zmanim.AstronomicalCalendar#getTemporalHour() temporal hour}.
511+
* If a zman does not occur, the value "N/A" will be returned.
519512
*
520513
* @param astronomicalCalendar the AstronomicalCalendar Object
521514
*
522-
* @return The JSON formatted <code>String</code>. The format will be:
523-
* <pre>
524-
* {
525-
* &quot;metadata&quot;:{
526-
* &quot;date&quot;:&quot;1969-02-08&quot;,
527-
* &quot;type&quot;:&quot;com.kosherjava.zmanim.AstronomicalCalendar&quot;,
528-
* &quot;algorithm&quot;:&quot;US Naval Almanac Algorithm&quot;,
529-
* &quot;location&quot;:&quot;Lakewood, NJ&quot;,
530-
* &quot;latitude&quot;:&quot;40.095965&quot;,
531-
* &quot;longitude&quot;:&quot;-74.22213&quot;,
532-
* &quot;elevation:&quot;31.0&quot;,
533-
* &quot;timeZoneName&quot;:&quot;Eastern Standard Time&quot;,
534-
* &quot;timeZoneID&quot;:&quot;America/New_York&quot;,
535-
* &quot;timeZoneOffset&quot;:&quot;-5&quot;},
536-
* &quot;AstronomicalTimes&quot;:{
537-
* &quot;Sunrise&quot;:&quot;2007-02-18T06:45:27-05:00&quot;,
538-
* &quot;TemporalHour&quot;:&quot;PT54M17.529S&quot;
539-
* ...
540-
* }
541-
* }
542-
* </pre>
515+
* @return The JSON <code>String</code> formatted as described above.
543516
*/
544517
public static String toJSON(AstronomicalCalendar astronomicalCalendar) {
545518
ZmanimFormatter formatter = new ZmanimFormatter(ZmanimFormatter.XSD_DURATION_FORMAT, DateTimeFormatter.ofPattern(
@@ -548,23 +521,22 @@ public static String toJSON(AstronomicalCalendar astronomicalCalendar) {
548521
.withZone(astronomicalCalendar.getGeoLocation().getZoneId());
549522

550523
LocalDate localDate = astronomicalCalendar.getLocalDate();
551-
ZoneId zi = astronomicalCalendar.getGeoLocation().getZoneId();
524+
GeoLocation geoLocation = astronomicalCalendar.getGeoLocation();
525+
ZonedDateTime lastMidnight = ZonedDateTime.of(astronomicalCalendar.getLocalDate(), LocalTime.MIDNIGHT,
526+
astronomicalCalendar.getGeoLocation().getZoneId());
527+
double offsetHours = lastMidnight.getOffset().getTotalSeconds() / 3600.0;
528+
String timeZoneName = lastMidnight.format(DateTimeFormatter.ofPattern("zzzz", Locale.getDefault()));
552529

553530
StringBuilder sb = new StringBuilder("{\n\"metadata\":{\n");
554531
sb.append("\t\"date\":\"").append(df.format(localDate)).append("\",\n");
555532
sb.append("\t\"type\":\"").append(astronomicalCalendar.getClass().getName()).append("\",\n");
556533
sb.append("\t\"algorithm\":\"").append(astronomicalCalendar.getAstronomicalCalculator().getCalculatorName()).append("\",\n");
557-
sb.append("\t\"location\":\"").append(astronomicalCalendar.getGeoLocation().getLocationName()).append("\",\n");
558-
sb.append("\t\"latitude\":\"").append(astronomicalCalendar.getGeoLocation().getLatitude()).append("\",\n");
559-
sb.append("\t\"longitude\":\"").append(astronomicalCalendar.getGeoLocation().getLongitude()).append("\",\n");
560-
sb.append("\t\"elevation\":\"").append(astronomicalCalendar.getGeoLocation().getElevation()).append("\",\n");
561-
562-
563-
sb.append("\t\"timeZoneName\":\"").append(zi.getDisplayName(TextStyle.FULL, Locale.getDefault())).append("\",\n");
564-
sb.append("\t\"timeZoneID\":\"").append(zi.getId()).append("\",\n"); //FIXME
565-
566-
ZonedDateTime lastMidnight = ZonedDateTime.of(astronomicalCalendar.getLocalDate(), LocalTime.MIDNIGHT, astronomicalCalendar.getGeoLocation().getZoneId());
567-
double offsetHours = lastMidnight.getOffset().getTotalSeconds() / 3600.0;
534+
sb.append("\t\"location\":\"").append(geoLocation.getLocationName()).append("\",\n");
535+
sb.append("\t\"latitude\":\"").append(geoLocation.getLatitude()).append("\",\n");
536+
sb.append("\t\"longitude\":\"").append(geoLocation.getLongitude()).append("\",\n");
537+
sb.append("\t\"elevation\":\"").append(geoLocation.getElevation()).append("\",\n");
538+
sb.append("\t\"timeZoneName\":\"").append(timeZoneName).append("\",\n");
539+
sb.append("\t\"timeZoneID\":\"").append(geoLocation.getZoneId().getId()).append("\",\n");
568540
sb.append("\t\"timeZoneOffset\":\"").append(offsetHours).append("\"");
569541
sb.append("},\n\"");
570542

0 commit comments

Comments
 (0)