Search Postgresql Archives

Re: Converting a TimestampTz into a C# DateTime

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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.


-- 
Sent via pgsql-general mailing list (pgsql-general@xxxxxxxxxxxxxx)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general



[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux