【问题标题】:Comparing two timestamps from mysql比较来自mysql的两个时间戳
【发布时间】:2016-01-18 11:07:59
【问题描述】:

我有两个从 mysql 检索到的日期时间变量。 一个等于'now()',另一个代表最后一次更改行。

我需要比较这两个日期时间值来获得两者之间的差异。

这就是我正在尝试的(我从 Mysql 中读取的内容是由定制的 Web api 处理的)...

 'get the current time
 Dim nowtime As DateTime = readsql("now()", "hosts", "")
    Console.WriteLine("NOW IS : " & nowtime)

    'read in all the rows
    Dim hosts() As String = readsql("hostname", "hosts", "chk_ping=1").ToString.Split("|")

    For Each host As String In hosts
        If Not host.Contains("No results found") Then
            Dim thishostip As String = readsql("IP", "hosts", "hostname='" & host & "'")
            Dim thishostlastpoll As DateTime = readsql("last_poll", "hosts", "hostname='" & host & "'")

            If thishostip.Contains("No results found") Then Exit For
            Console.WriteLine("HOST TIME IS:"  & thishostlastpoll.ToString)

            Dim timedifference As Integer = DateTime.Compare(nowtime, thishostlastpoll)
            Console.WriteLine("Time diff for " & host & " is : " & timedifference)


        End If

    Next 

我的日期时间值是这样进来的......

 18/01/2016 10:53:00
 18/01/2016 10:52:52

并且不会抛出任何错误/异常,但无论差异如何,'timedifference' 永远只有 1。

我怀疑它与日期时间的格式有关,这不是 VB 想要的,但我找不到任何可以将 mysql 转换为 vb 日期时间的东西,反之亦然。

任何方向都会被应用!提前致谢!

【问题讨论】:

  • 不是“格式”,日期没有格式。一旦你将这 2 个值放入 DateTime 变量中,它们就是 Net DateTime 变量,如果 Net 不喜欢它们的某些东西,你会得到一个错误。如果我比较这两个值,它工作正常。您能否显示readsql 并告诉我们数据库中使用的数据类型
  • readsql 是对处理所有数据库交互的 php api 的 http 调用。我想我会使用计时器而不是比较实际时间,然后一起否定这个问题:)

标签: mysql vb.net timestamp


【解决方案1】:

I suspect its something to do with the format of the datetime not being what VB wants, but I cant find anything for converting mysql to vb datetimes, just the other way round.

您可能对 1 或 2 件事感到困惑。

首先,日期没有格式。格式是计算机(或您编写的计算机代码)向用户显示日期的方式。网络(不是 VB 本身DateTime 是一个值,表示存储为一个非常大的数字的时间点。由于635886720000000000(今天的日期)对我们大多数人来说意义不大,所以DateTime 类型以传统格式表示它。

MySql 数据提供者对象完全能够将数据从 Net DateTime 值来回转换,但是 MySql 需要根据列定义来存储它。

真正的问题可能在这里:

Dim timedifference As Integer = DateTime.Compare(nowtime, thishostlastpoll)

鉴于变量名称和显示方式,您可能会对DateTime.Compare 的作用感到困惑。来自MSDN

比较两个 DateTime 实例并返回一个整数,指示第一个实例是早于、相同还是晚于第二个实例。

给定 2 个 MySql 列,定义为 TIMESTAMP(3) 并读入 NET DateTime vars:

Console.WriteLine("A is {0}", dtA)
Console.WriteLine("B is {0}", dtB)
Console.Write("Compare is {0}, therefore ", DateTime.Compare(dtA, dtB))

Select Case DateTime.Compare(dtA, dtB)
    Case Is < 0         ' ie -1
        Console.WriteLine("dtA is earlier")
    Case Is > 0
        Console.WriteLine("dtB is earlier")
    Case 0
        Console.WriteLine("dtA and dtB are exactly equal")
End Select

结果:

A 是 2016 年 1 月 18 日上午 8:39:51
B 是 2016 年 1 月 18 日上午 8:40:11
比较是 -1 因此,dtA 更早

DateTime.Compare 可能有点令人困惑的是,结果表示 较早/较小 值,而大多数其他 Compare 方法表示较大。

【讨论】:

  • 你是多么正确......我留下的'1'实际上是正确的,我误解了这个功能。感谢您指出这一点。我有另一种方法来实现我需要并且现在正在实施。
【解决方案2】:

而是尝试减去两个日期时间值,如下所示,这将返回一个TimeStamp 值。顺便说一句,您当前的日期时间格式是dd/mm/yyyy,我认为应该是mm/dd/yyyy

Dim timedifference As TimeStamp = nowtime - thishostlastpoll

【讨论】:

  • 我明白了....'错误 6 'System.TimeSpan' 类型的值无法转换为'Date'。'我将如何更改从 mysql 收到的格式?另外,我使用了'datetime'和'date',因为使用'timestamp'给了我“类型'timestamp is not defined'
猜你喜欢
  • 1970-01-01
  • 2013-04-26
  • 2012-02-04
  • 2015-01-02
  • 2021-04-25
  • 1970-01-01
  • 1970-01-01
  • 2019-02-27
  • 2017-07-10
相关资源
最近更新 更多