Skip to content

Commit 7e0f2cf

Browse files
committed
truetype/HeadTable: Make TrueType date calculations UTC-based
The TrueType epoch is supposed to be understood as an UTC date. Instantiating `GregorianCalendar` with a date implicitly sets its timezone to the local timezone. Because it is not possible to pass both a date and a timezone to the constructor of `GregorianCalendar`, let's store the offset as a precalculated number (`date --date="1904-01-01 00:00:00 UTC" +%s`). While we are at it, let's also switch from negative- to positive-offset calculations.
1 parent 67f7596 commit 7e0f2cf

File tree

1 file changed

+7
-7
lines changed
  • main/java/BitsNPicas/src/com/kreative/bitsnpicas/truetype

1 file changed

+7
-7
lines changed

main/java/BitsNPicas/src/com/kreative/bitsnpicas/truetype/HeadTable.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class HeadTable extends TrueTypeTable {
2424
public static final int FLAGS_DEFINED_BY_ADOBE_2 = 0x1000;
2525
public static final int FLAGS_DEFINED_BY_ADOBE_3 = 0x2000;
2626
public static final int FLAGS_GENERIC_SYMBOLS_FOR_CODE_POINT_RANGES = 0x4000;
27-
public static final long DATE_EPOCH = new GregorianCalendar(1904, Calendar.JANUARY, 1, 0, 0, 0).getTimeInMillis();
27+
public static final long DATE_EPOCH_1904 = 2082844800L * 1000L; // Milliseconds between 1904-01-01 00:00:00 UTC and Unix epoch
2828
public static final int MAC_STYLE_PLAIN = 0x00;
2929
public static final int MAC_STYLE_BOLD = 0x01;
3030
public static final int MAC_STYLE_ITALIC = 0x02;
@@ -81,23 +81,23 @@ public void setFontRevisionDouble(double revision) {
8181
}
8282

8383
public GregorianCalendar getDateCreatedCalendar() {
84-
GregorianCalendar cal = new GregorianCalendar();
85-
cal.setTimeInMillis(DATE_EPOCH + (dateCreated * 1000L));
84+
GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
85+
cal.setTimeInMillis((dateCreated * 1000L) - DATE_EPOCH_1904);
8686
return cal;
8787
}
8888

8989
public void setDateCreatedCalendar(Calendar cal) {
90-
dateCreated = (cal.getTimeInMillis() - DATE_EPOCH) / 1000L;
90+
dateCreated = (cal.getTimeInMillis() + DATE_EPOCH_1904) / 1000L;
9191
}
9292

9393
public GregorianCalendar getDateModifiedCalendar() {
94-
GregorianCalendar cal = new GregorianCalendar();
95-
cal.setTimeInMillis(DATE_EPOCH + (dateModified * 1000L));
94+
GregorianCalendar cal = new GregorianCalendar(TimeZone.getTimeZone("UTC"));
95+
cal.setTimeInMillis((dateModified * 1000L) - DATE_EPOCH_1904);
9696
return cal;
9797
}
9898

9999
public void setDateModifiedCalendar(Calendar cal) {
100-
dateModified = (cal.getTimeInMillis() - DATE_EPOCH) / 1000L;
100+
dateModified = (cal.getTimeInMillis() + DATE_EPOCH_1904) / 1000L;
101101
}
102102

103103
@Override

0 commit comments

Comments
 (0)