Re: Converting a TimestampTz into a C# DateTime - Mailing list pgsql-general

From valeriof
Subject Re: Converting a TimestampTz into a C# DateTime
Date
Msg-id 1479207611814-5930394.post@n3.nabble.com
Whole thread Raw
In response to Re: Converting a TimestampTz into a C# DateTime  (Arjen Nienhuis <a.g.nienhuis@gmail.com>)
Responses Re: Converting a TimestampTz into a C# DateTime  (Albe Laurenz <laurenz.albe@wien.gv.at>)
List 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.


pgsql-general by date:

Previous
From: Venkata B Nagothi
Date:
Subject: Re: Fwd: Mail to be posted in PostgreSQL community
Next
From: kaustubh kelkar
Date:
Subject: Re: Fwd: Mail to be posted in PostgreSQL community