[fpc-pascal] convert "epoch" to UTC

waldo kitty wkitty42 at windstream.net
Mon Jan 16 17:57:44 CET 2012


On 1/16/2012 06:22, Sven Barth wrote:
> Am 16.01.2012 04:10 schrieb "waldo kitty" <wkitty42 at windstream.net>
>  >
>  >
>  > i'm needing to convert a "UTC epoch" date to a "standard time string"... by
>  > that, i mean that i want to get something like "2011-01-13 22:24:04" out of
>  > the "epoch" number 12013.9338171...

TYPO ALERT! that should be .93338171... with the additional '3' in there, the 
final value for the seconds is 4... long day, tired eyes and dirty glasses :/

[TRIM]
>  > so, my question is this... is there an existing FPC routine that will convert
>  > this (or a part of this) properly or do i need to play with something to get
>  > it right? it is obvious that this is not a "unix epoch"... i'm not sure how
>  > "normal" julian date epochs are represented...
>  >
>  > i've looked through the docs on freepascal.org <http://freepascal.org> but
>  > none of them show an number with which to compare... there's no links pointing
>  > to a definition or example of a julian date or unix date or any such...
>
> I don't know whether your format is really a Julian or Unix time (for Julian it
> looks too low),

it is actually one of the "julian" ones... it is the "julian day of year" 
format... at least that's what i believe it is called...

> but can take a look at unit "DateUtils" of which the
> documentation is here:
> http://www.freepascal.org/docs-html/rtl/dateutils/index-5.html

yes, i had been all in there... that's where i was expecting to see an example 
that actually contained a number that would be converted to a date so that one 
might work out the math manually if desired... without a number, i had nothing 
to compare what i have with to see if it was even close...

> Especially you can try JulianToDateTime and UnixToDateTime. Both return a
> TDateTime which you can convert to a string using FormatDateTime
> ( http://www.freepascal.org/docs-html/rtl/sysutils/datetimetostring.html ) or
> DateTimeToStr ( http://www.freepascal.org/docs-html/rtl/sysutils/datetimetostr.html ).

i think i might be able to do something with the JulianToDateTime once i get the 
base year portion converted to a true julian... i spotted some C# code while 
doing a bit of research earlier (see below)... it appears to convert the year to 
a julian year and then add the decimal day portion... this should then be a 
proper julian date which i should be able to handle ;)

[---- WARNING: C# code follows ----]
double getJulianDay_Year(int year)
{
     double dYear = year - 1;
     double A = Math.Floor(dYear / 100);
     double B = 2 - A + Math.Floor(A / 4);
     //The use of 30.600000000000001 is to correct for floating point rounding 
problems
     double dResult = Math.Floor(365.25 * dYear) + 1721422.9 + B;
     return dResult;
}

double getJulianDay_SatEpoch(int year, double dSatelliteEpoch)
{
     //Tidy up the year and put it into the correct century
     year = year % 100;
     if (year < 57) year += 2000;
     else year += 1900;

     double dResult = getJulianDay_Year(year);
     dResult += dSatelliteEpoch;

     return dResult;
}
[---- end of C# code ----]

looks like it should be easy to convert to pascal, too ;) just gotta find out 
what that "floor" routine does ;)




More information about the fpc-pascal mailing list