与sqlite中的strftime进行日期比较

时间:2017-04-24 12:32:23

标签: sqlite strftime

我将微秒的unix时间戳与strftime的时间进行比较。为此,我需要使用一个通用单元。我不明白为什么它在微秒内工作(即通过将strftime乘以100万)而不是相反。

SELECT

strftime("%s", "2017-04-01 00:00:00") AS Apr1st,
1492605353892000/1000000 as d,

strftime("%s", "2017-04-01 00:00:00")*1000000 As Apr1st_us,
1492605353892000 AS d_us,

1492605353892000/1000000 > strftime("%s", "2017-04-01 00:00:00") AS cmp,
1492605353892000/1000000 - strftime("%s", "2017-04-01 00:00:00") AS diff,

1492605353892000 > strftime("%s", "2017-04-01 00:00:00")*1000000 AS cmp_us,
1492605353892000 - strftime("%s", "2017-04-01 00:00:00")*1000000 AS diff_us

;

给了我:

|     Apr1st |          d |        Apr1st_us |             d_us | cmp |    diff | cmp_us |       diff_us |
|------------|------------|------------------|------------------|-----|---------|--------|---------------|
| 1491004800 | 1492605353 | 1491004800000000 | 1492605353892000 |   0 | 1600553 |      1 | 1600553892000 |

1 个答案:

答案 0 :(得分:1)

strftime返回一个字符串,因此在与整数the integer is always less进行比较时。 strftime(...)*1000000的版本有效,因为乘法将其转换为整数,允许比较对数字起作用。

转换strftime返回值使其正确:

1492605353892000/1000000 > CAST(strftime("%s", "2017-04-01 00:00:00") AS INT)