Reputation: 2791
This is a tough one to google for. I have an XML document that's a million lines long, and I'm using Ruby to parse it and remove entries I don't care about. One my my criteria is the date created. These XML blocks have funny looking dates in them
<attribute name="datemodified" type="date">362895460.21263897418975830078</attribute>
<attribute name="datecreated" type="date">356831173.15324598550796508789</attribute>
I've never seen dates formatted like that exactly. They look similar to if you did something like Time.now.to_f
. Even so I don't know how I'd turn those into Ruby DateTime objects. If you can even identify how these times are created, or what they mean, that would be super helpful.
If it helps, this XML file was originally created by a Mac OS X application known as "Things".
Thanks for reading!
Update: I've created two more entries and recorded the times at which i created them:
From JULY-02-2012 9:57 AM
<attribute name="datemodified" type="date">362941035.01687598228454589844</attribute>
<attribute name="datecreated" type="date">362940986.89370900392532348633</attribute>
From JULY-02-2012 9:58 AM
<attribute name="datemodified" type="date">362941107.69538801908493041992</attribute>
<attribute name="datecreated" type="date">362941080.53793197870254516602</attribute>
I couldn't get down to the second on accuracy, but i did make them about minute apart... Which makes it seem that these are in fact seconds... But... from some random date. Maybe the developer's birthday :)
Doing some quick math, it would appear that the randomish date is right around 2000-12-31 16:09:43 -0800, or perhaps 01/01/01, for ease of memory... And 978336000 in seconds.
Upvotes: 2
Views: 525
Reputation: 27789
The Time.at
method translates from seconds since 1970 to a Time
instance:
[1] pry(main)> Time.at 362895460.21263897418975830078
=> 1981-07-02 00:17:40 -0400
If that date isn't right, but the units are seconds, you could add a constant to get to the correct date, e.g.
[2] pry(main)> Time.parse('2001-01-01') - Time.at(0)
=> 978325200.0
Upvotes: 3
Reputation: 163458
The numbers look to me like the integer part is a Julian date and the fraction part is the fraction of a day. I haven't investigated that in detail. For reference the code in Saxon for converting date/time from "Julian instant" is:
public static DateTimeValue fromJulianInstant(/*@NotNull*/ BigDecimal instant) {
BigInteger julianSecond = instant.toBigInteger();
BigDecimal microseconds = instant.subtract(new BigDecimal(julianSecond)).multiply(DecimalValue.BIG_DECIMAL_ONE_MILLION);
long js = julianSecond.longValue();
long jd = js / (24L * 60L * 60L);
DateValue date = DateValue.dateFromJulianDayNumber((int)jd);
js = js % (24L * 60L * 60L);
byte hour = (byte)(js / (60L * 60L));
js = js % (60L * 60L);
byte minute = (byte)(js / (60L));
js = js % (60L);
return new DateTimeValue(date.getYear(), date.getMonth(), date.getDay(),
hour, minute, (byte)js, microseconds.intValue(),0 , true);
}
plus
public static DateValue dateFromJulianDayNumber(int julianDayNumber) {
if (julianDayNumber >= 0) {
int L = julianDayNumber + 68569 + 1; // +1 adjustment for days starting at noon
int n = (4 * L) / 146097;
L = L - (146097 * n + 3) / 4;
int i = (4000 * (L + 1)) / 1461001;
L = L - (1461 * i) / 4 + 31;
int j = (80 * L) / 2447;
int d = L - (2447 * j) / 80;
L = j / 11;
int m = j + 2 - (12 * L);
int y = 100 * (n - 49) + i + L;
return new DateValue(y, (byte) m, (byte) d, true);
} else {
// add 12000 years and subtract them again...
DateValue dt = dateFromJulianDayNumber(julianDayNumber +
(365 * 12000 + 12000 / 4 - 12000 / 100 + 12000 / 400));
dt.year -= 12000;
return dt;
}
}
Upvotes: 0