Re: Converting a TimestampTz into a C# DateTime

From: valeriof <valerio_farruggio(at)hotmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Converting a TimestampTz into a C# DateTime
Date: 2016-11-15 11:00:11
Message-ID: 1479207611814-5930394.post@n3.nabble.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I was able to make it work by reusing the code in TimeStampHandler.cs (in my
application I cannot directly reference Npgsql):

long datetime = GetInt64(buffer, ref pos);
// 8 bytes: datetime

if (datetime == long.MaxValue)
return DateTime.MaxValue;
else if (datetime == long.MinValue)
return DateTime.MinValue;

DateTime dt;
int date;
long time;

if (datetime >= 0)
{
date = (int)(datetime / 86400000000L);
time = datetime % 86400000000L;

date += 730119; // 730119 = days since era (0001-01-01)
for 2000-01-01
time *= 10; // To 100ns
}
else
{
datetime = -datetime;
date = (int)(datetime / 86400000000L);
time = datetime % 86400000000L;
if (time != 0)
{
++date;
time = 86400000000L - time;
}
date = 730119 - date; // 730119 = days since era
(0001-01-01) for 2000-01-01
time *= 10; // To 100ns
}

TimeSpan ts = new TimeSpan(date, 0, 0, 0);
dt = (new DateTime(ts.Ticks) + new
TimeSpan(time)).ToLocalTime();

return dt;

BTW, a comment says this about the floating point representation: "A
deprecated compile-time option of PostgreSQL switches to a floating-point
representation of some date/time
fields. Npgsql (currently) does not support this mode." Is it safe to say
that the floating point format is less in use compared to the long int? If
Npgsql doesn't support it, any application that uses Npgsql will have this
limitation anyway. Am I correct?

--
View this message in context: http://postgresql.nabble.com/Converting-a-TimestampTz-into-a-C-DateTime-tp5930221p5930394.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message kaustubh kelkar 2016-11-15 11:23:17 Re: Fwd: Mail to be posted in PostgreSQL community
Previous Message Venkata B Nagothi 2016-11-15 08:56:48 Re: Fwd: Mail to be posted in PostgreSQL community