将int类型的日期时间转换为毫秒。

huangapple go评论53阅读模式
英文:

Convert int type datetime to milliseconds

问题

int类型的日期时间表示如下,13:32:53:600 => 133253600,我想将int类型的日期时间转换为从当天的0:00:00起经过的毫秒数。例如,133253600 => 48773600

英文:

The int type datetime is expressed as follows, 13:32:53:600 => 133253600, and I wanna convert the int type datetime to the number of milliseconds that have passed since 0:00:00 of the day. For example, 133253600 => 48773600.

答案1

得分: 4

考虑一个以 HHMMSSmmm 格式表示的数字,其中 HH 是小时的十进制数,MM 是分钟的十进制数,SS 是秒的十进制数,mmm 是毫秒的十进制数。

将其解释为毫秒的十进制数,HH0000000 毫秒多于 HH 小时的总毫秒数。一小时有 3,600,000 毫秒,所以 HH 小时有 3,600,000•HH 毫秒,但 HH0000000 表示 10,000,000•HH 毫秒。它多了 6,400,000•HH 毫秒。因此,我们可以通过减去 6,400,000•HH 来进行修正。

类似地,MM00000 表示 100,000•MM 毫秒,但 MM 分钟只有 60,000•MM 毫秒。它多了 40,000•MM 毫秒,因此我们可以通过减去 40,000•MM 来进行修正。

SSmmm 确实表示了 SS 秒和 mmm 毫秒的毫秒数,所以对它们不需要进行修正。

我们可以通过将 HHMMSSmmm 除以 10,000,000 来轻松找到 HH,使用 C 的截断除法。要分离分钟需要更多的工作。将其除以 100,000 将给我们 HHMM,然后我们需要取余数来分离 MM。但是,相反地,我们可以重写修正以直接使用 HHMM

给定一些 x,它是 HHMMSSmmm,我们希望 x - HH•6,400,000 - MM•40,000。加减 HH00•40,000 不会改变数字,所以:

  • x - HH•6,400,000 - MM•40,000
  • = x - HH•6,400,000 - MM•40,000 + HH00•40,000 - HH00•40,000
  • = x - HH•6,400,000 + HH•100•40,000 - MM•40,000 - HH00•40,000
  • = x - HH•(6,400,000 - 4,000,000) - HHMM•40,000.
  • = x - HH•2,400,000 - HHMM•40,000。

如上所述,HH 可以在 C 中计算为 x/10000000,而 HHMM 可以计算为 x/100000

因此,给出所需结果的表达式是 x - x/10000000*2400000 - x/100000*40000,它使用了四个乘法/除法操作和两个加法/减法操作。

英文:

Consider a numeral in the form HHMMSSmmm, where HH is a decimal number of hours, MM is a decimal number of minutes SS is a decimal number of seconds, and mmm is a decimal number of milliseconds.

Interpreted as a decimal numeral of milliseconds, HH0000000 is more milliseconds than is in HH hours. There are 3,600,000 milliseconds in an hour, so 3,600,000•HH in HH hours, but HH0000000 represents 10,000,000•HH milliseconds. It has 6,400,000•HH milliseconds too many. Therefore, we can correct it by subtracting 6,400,000•HH.

Similarly, MM00000 represents 100,000•MM milliseconds, but there are only 60,000•MM milliseconds in MM minutes. It has 40,000•MM milliseconds too many, so we can correct it by subtracting 40,000•MM.

SSmmm does represent the number of milliseconds in SS seconds and mmm milliseconds, so no correction is needed for those.

We can easily find HH by dividing HHMMSSmmm by 10,000,000, using C’s truncating division. Isolating the minutes would require more work. Dividing by 100,000 would give us HHMM, and then we would need to take a remainder to separate MM. However, instead, we can rewrite the corrections to use HHMM directly.

Given some x that is HHMMSSmmm, we want xHH•6,400,000 − MM•40,000. Adding and subtracting HH00•40,000 does not change the number, so:

  • xHH•6,400,000 − MM•40,000
  • = xHH•6,400,000 − MM•40,000 + HH00•40,000 − HH00•40,000
  • = xHH•6,400,000 + HH•100•40,000 − MM•40,000 − HH00•40,000
  • = xHH•(6,400,000 − 4,000,000) − HHMM•40,000.
  • = xHH•2,400,000 − HHMM•40,000.

As noted above, HH can be calculated in C as x/10000000, and HHMM can be calculated as x/100000.

Therefore, an expression that gives us the desired result is x - x/10000000*2400000 - x/100000*40000, which uses four multiplication/division operations and two additions/subtraction operations.

答案2

得分: 0

以下是已翻译的部分:

计算结果如下:

r = ( ( ( H * 60 ) + M ) * 60 ) + S ) * 1000 + milli;

所以,如果包括获取组件,那就是

t = 133253600;

H = t / 10000000;
M = t / 100000 % 100;
S = t / 1000 % 100;
milli = t % 1000;

r = ( ( ( H * 60 ) + M ) * 60 ) + S ) * 1000 + milli;

但秒数在技术上已经是正确的。

t = 133253600;

H = t / 10000000;
M = t / 100000 % 100;
milli = t % 100000;

r = ( ( H * 60 ) + M ) * 60 + milli;

这是所需的 8 个操作。4 个除法、2 个乘法和 2 个加法。

英文:

The result is calculated as follows:

r = ( ( ( H * 60 ) + M ) * 60 ) + S ) * 1000 + milli;

So if we include getting the components, that's

t = 133253600;

H = t / 10000000;
M = t / 100000 % 100;
S = t / 1000 % 100;
milli = t % 1000;

r = ( ( ( H * 60 ) + M ) * 60 ) + S ) * 1000 + milli;

But the seconds are technically already correct.

t = 133253600;

H = t / 10000000;
M = t / 100000 % 100;
milli = t % 100000;

r = ( ( H * 60 ) + M ) * 60 + milli;

That is the required 8 ops. 4 divisions, 2 multiplications and 2 additions.

答案3

得分: 0

只需除以10000000(保留整数部分)即可获得小时(前两位数字),除以100000获得前四位数字,除以1000获得前六位数字,依此类推。使用这些数字,您可以对100取模以获得该数字的最后2位数字,并对1000取模以保留最后3位数字。最后,您需要将它们转换为毫秒,就这样。

int time=133253600 //示例
int hours = time/ 10000000;
int minutes = (time/ 100000) % 100;
int seconds = (time/ 1000) % 100;
int milliseconds = time% 1000;

int timeInMs = (hours * 3600000) + (minutes * 60000) + (seconds * 1000) + milliseconds;

这并不需要8个操作,但我无法想象如何能够少于这个数量。如果您的操作依赖于先前的操作,您可以通过使用较小的数字进行除法来进行优化,但仍然需要相同数量的操作。

英文:

You would have to divide by 10000000 (Only keeping integers) to get hours (the first 2 numbers), 100000 to get the first 4, 1000 to get the first 6, etc.
With tose numbers you then can make modulus 100 to get last 2 digits of that number and modulus 1000 to keep last 3. Last thing you have to do is transform those into miliseconds and that's it.

    int time=133253600 //example
    int hours = time/ 10000000;
    int minutes = (time/ 100000) % 100;
    int seconds = (time/ 1000) % 100;
    int milliseconds = time% 1000;

    int timeInMs = (hours * 3600000) + (minutes * 60000) + (seconds * 1000) + milliseconds;

It doesn't use 8 operations, but I can't imagine how could you do it on less. You could optimize by dividing by smaller numbers if you do one operation depending on the previous one, maybe.

huangapple
  • 本文由 发表于 2023年5月21日 23:31:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76300654.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定