我需要从毫秒到(小时,分钟,秒,毫秒)的元组表示相同的时间量。 E.g:
10799999ms = 2h 59m 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 :(得分:105)
以下是我在Java中的表现:
int seconds = (int) (milliseconds / 1000) % 60 ;
int minutes = (int) ((milliseconds / (1000*60)) % 60);
int hours = (int) ((milliseconds / (1000*60*60)) % 24);
答案 1 :(得分: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()
。
答案 2 :(得分: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;
}
答案 3 :(得分:3)
不是真正优雅的,但会更短一些
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)
答案 4 :(得分:0)
#menu-bar {
width: 100%;
line-height: 40px;
position:relative;
z-index:999;
}
#menu-bar li {
background-color:black;
float: left;
position: relative;
list-style: none;
}
#menu-bar a {
font-weight: bold;
font-family: arial;
font-style: normal;
font-size: 12px;
color: #E7E5E5;
text-decoration: none;
display: block;
padding: 6px 10px 4px 10px;
margin: 0;
}
#menu-bar li ul li a {
margin: 0;
}
#menu-bar li:hover .img {
visibility: hidden;
}
#menu-bar .nav-button-home a {
padding: 10px;
}
.test {
padding: 5px 10px;
}
这是groovy,但我觉得这对你来说不是问题。方法工作完善。
答案 5 :(得分: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
}
}
我希望我的简短方法可以帮助你
答案 6 :(得分:0)
基于Valentinos答案的Arduino(c ++)版本
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);
}
答案 7 :(得分:0)
另一个Java示例:
long dayLength = 1000 * 60 * 60 * 24;
long dayMs = System.currentTimeMillis() % dayLength;
double percentOfDay = (double) dayMs / dayLength;
int hour = (int) (percentOfDay * 24);
int minute = (int) (percentOfDay * 24 * 60) % 60;
int second = (int) (percentOfDay * 24 * 60 * 60) % 60;
一个优点是,如果您调整dayLength
答案 8 :(得分:0)
Kotlin 示例,小时/分钟/秒小于 10 的前导零。因此,如果您想在 UI 中将该值用作字符串,则最终会得到相同的持续时间。
这给出了 01:57:01 而不是 1:57:1,这可能会混淆 hh:mn:ss 注释。
val timeInMilliSec = 45600030
val hours = timeInMilliSec.div(3600).rem(24)
val minutes = timeInMilliSec.div(60).rem(60)
val seconds = timeInMilliSec.rem(60)
val hoursFormatted = if (hours < 10) "0$hours" else "$hours"
val minutesFormatted = if (minutes < 10) "0$minutes" else "$minutes"
val secondsFormatted = if (seconds < 10) "0$seconds" else "$seconds"
"$hoursFormatted:$minutesFormatted:$secondsFormatted"