2012-06-03 226 views
31

我需要从毫秒到表示相同时间量的(小时,分钟,秒,毫秒)元组。例如:从毫秒到小时,分钟,秒和毫秒

10799999ms = 2H59米59S 999ms

下面的伪代码是我能想出的唯一的事:

# The division operator below returns the result as a rounded down integer 
function to_tuple(x): 
    h = x/(60*60*1000) 
    x = x - h*(60*60*1000) 
    m = x/(60*1000) 
    x = x - m*(60*1000) 
    s = x/1000 
    x = x - s*1000 
    return (h,m,s,x) 

我敢肯定,它必须能够做到这一点更聪明/更优雅/更快/更紧凑。

+0

你可以用C和朋友模运算符(% )稍微简化x的计算(例如,x = x%(60 * 60 * 1000)) – fvu

+0

确保您在使用的标准语言库中没有此类功能。 –

回答

83

这里是我会怎么做它在Java中:

int seconds = (int) (milliseconds/1000) % 60 ; 
int minutes = (int) ((milliseconds/(1000*60)) % 60); 
int hours = (int) ((milliseconds/(1000*60*60)) % 24); 
+2

在java中使用TimeUnit可以使代码更具可读性。 –

+8

'long millis = 12884983; System.out.println(((millis /(1000 * 60))%60)); System.out.println(java.util.concurrent.TimeUnit.MILLISECONDS.toMinutes(millis));'output:34 | 214 – JBoy

6

好问题。是的,人们可以更有效地做到这一点。您的CPU可以在一次操作中提取两个整数的比率和余数。在<stdlib.h>中,公开此CPU操作的函数称为div()。在你的伪代码,你会用它是这样的:

function to_tuple(x): 
    qr = div(x, 1000) 
    ms = qr.rem 
    qr = div(qr.quot, 60) 
    s = qr.rem 
    qr = div(qr.quot, 60) 
    m = qr.rem 
    h = qr.quot 

效率较低的答案将分别使用/%运营商。然而,无论如何,如果你同时需要商和余数,那么你不妨称之为更高效的div()

4

不是真的eleganter,但有点短会

function to_tuple(x): 
    y = 60*60*1000 
    h = x/y 
    m = (x-(h*y))/(y/60) 
    s = (x-(h*y)-(m*(y/60)))/1000 
    mi = x-(h*y)-(m*(y/60))-(s*1000) 

    return (h,m,s,mi) 
5

也许可以更短的更优雅。但我做到了。

public String getHumanTimeFormatFromMilliseconds(String millisecondS){ 
    String message = ""; 
    long milliseconds = Long.valueOf(millisecondS); 
    if (milliseconds >= 1000){ 
     int seconds = (int) (milliseconds/1000) % 60; 
     int minutes = (int) ((milliseconds/(1000 * 60)) % 60); 
     int hours = (int) ((milliseconds/(1000 * 60 * 60)) % 24); 
     int days = (int) (milliseconds/(1000 * 60 * 60 * 24)); 
     if((days == 0) && (hours != 0)){ 
      message = String.format("%d hours %d minutes %d seconds ago", hours, minutes, seconds); 
     }else if((hours == 0) && (minutes != 0)){ 
      message = String.format("%d minutes %d seconds ago", minutes, seconds); 
     }else if((days == 0) && (hours == 0) && (minutes == 0)){ 
      message = String.format("%d seconds ago", seconds); 
     }else{ 
      message = String.format("%d days %d hours %d minutes %d seconds ago", days, hours, minutes, seconds); 
     } 
    } else{ 
     message = "Less than a second ago."; 
    } 
    return message; 
} 
0
milliseconds = x 
total = 0 
while (milliseconds >= 1000) { 
    milliseconds = (milliseconds - 1000) 
    total = total + 1 
} 
hr = 0 
min = 0 
while (total >= 60) { 
    total = total - 60 
    min = min + 1 
    if (min >= 60) hr = hr + 1 
    if (min == 60) min = 0 
} 
sec = total 

这是常规的,但我的东西,这是不是为您解决问题。方法工作完美。

0
milliseconds = 12884983 // or x milliseconds 
hr = 0 
min = 0 
sec = 0 
day = 0 
while (milliseconds >= 1000) { 
    milliseconds = (milliseconds - 1000) 
    sec = sec + 1 
    if (sec >= 60) min = min + 1 
    if (sec == 60) sec = 0 
    if (min >= 60) hr = hr + 1 
    if (min == 60) min = 0 
    if (hr >= 24) { 
    hr = (hr - 24) 
    day = day + 1 
    } 
} 

我希望我的短方法将帮助您

0
import java.text.SimpleDateFormat; 
import java.util.Date; 
import java.util.concurrent.TimeUnit; 

public class MyTest { 

    public static void main(String[] args) { 
     long seconds = 360000; 

     long days = TimeUnit.SECONDS.toDays(seconds); 
     long hours = TimeUnit.SECONDS.toHours(seconds - TimeUnit.DAYS.toSeconds(days)); 

     System.out.println("days: " + days); 
     System.out.println("hours: " + hours); 
    } 
} 
0

的Arduino(C++)版本的基础上Valentinos答案

unsigned long timeNow = 0; 
unsigned long mSecInHour = 3600000; 
unsigned long TimeNow =0; 
int millisecs =0; 
int seconds = 0; 
byte minutes = 0; 
byte hours = 0; 

void setup() { 
Serial.begin(9600); 
Serial.println (""); // because arduino monitor gets confused with line 1 
Serial.println ("hours:minutes:seconds.milliseconds:"); 
} 

void loop() { 
TimeNow = millis(); 
hours = TimeNow/mSecInHour; 
minutes = (TimeNow-(hours*mSecInHour))/(mSecInHour/60); 
seconds = (TimeNow-(hours*mSecInHour)-(minutes*(mSecInHour/60)))/1000; 
millisecs = TimeNow-(hours*mSecInHour)-(minutes*(mSecInHour/60))-  (seconds*1000); 

Serial.print(hours); 
Serial.print(":"); 
Serial.print(minutes); 
Serial.print(":"); 
Serial.print(seconds); 
Serial.print("."); 
Serial.println(millisecs); 
}