用于在数据库中存储`DiffTime`的类型

时间:2021-12-20 22:40:35

I need to store a DiffTime from the time library in a database (PostgreSQL) and have three options for converting to a database type:

我需要将时间库中的DiffTime存储在数据库(PostgreSQL)中,并且有三个选项可以转换为数据库类型:

  1. Integer using fromIntegral . fromEnum
  2. 整数使用fromIntegral。 fromEnum
  3. Numeric via Rational toRational
  4. 通过Rational toRational的数字
  5. Real using realToFrac
  6. 真正使用realToFrac

The integer is exact, but rather large, since stored in pico seconds (10^12). Numeric is also exact, but it is potentially very expensive to calculate a Rational, since it has to calculate gcd for numerator and denominator. The real is inexact, but maybe that doesn't matter.

整数是精确的,但相当大,因为存储在皮秒(10 ^ 12)。数值也是精确的,但计算一个Rational可能非常昂贵,因为它必须计算分子和分母的gcd。真实是不准确的,但也许这无关紧要。

My time granularity would never be below minutes or larger than a couple of days. I need to make exact calculations. Speed is probably not an issue.

我的时间粒度绝不会低于几分钟或大于几天。我需要做出精确的计算。速度可能不是问题。

Which option should I choose and what are the implications? Should Rational generally be avoided when handling user submitted numbers like explained in scientific library?

我应该选择哪个选项以及含义是什么?在处理用户提交的数字时,一般应该避免Rational,如科学库中所解释的那样吗

1 个解决方案

#1


5  

I think you might be looking for interval. The hasql library has an encoder and a decoder for DiffTime with the name interval as well.

我想你可能正在寻找间隔。 hasql库具有DiffTime的编码器和解码器,名称间隔也是如此。

#1


5  

I think you might be looking for interval. The hasql library has an encoder and a decoder for DiffTime with the name interval as well.

我想你可能正在寻找间隔。 hasql库具有DiffTime的编码器和解码器,名称间隔也是如此。