将秒表 "hh:mm:ss:ms" 转换为存储在数据库中
Convert stopwatch "hh:mm:ss:ms" to store in database
我正在尝试在我的数据库中保存可变长度的时间。最长的可能包括数小时,较短的可能只有几分钟。较短的字符串只会显示为“mm:ss.ms”或“:ss.ms”,因此整体长度会更短。
我试过DateTime
,但它需要年月日。我尝试将日期设置为 0000-00-00,但没有成功。
我尝试对所有内容进行硬编码,但希望有更优雅的解决方案。这是我得到的结果:
// for shorter string of type ":mm.ms"
if (myString.length >= 3) AND (myString.length <= 4) {
if (myString.contains(':')) {
myString.substring(1); //remove initial semicolon
}
int _seconds = int.parse(myString.substring(0,1));
int _milliseconds = int.parse(myString.substring(3,3));
}
任何指点将不胜感激。
如果我解决了你的问题,这可能是适合你的解决方案:
String test = ":23.24";
String minutes = test.split(":")[0];
String seconds = test.split(":")[1].split(".")[0];
String milliseconds = test.split(":")[1].split(".")[1];
Duration duration = Duration(
minutes: int.tryParse(minutes) ?? 0,
seconds: int.tryParse(seconds) ?? 0,
milliseconds: int.tryParse(milliseconds) ?? 0);
print(duration.inMilliseconds);
编辑:我找到了另一种方法,它也允许几个小时而且更可靠:
int parseTimeString(String string, {int currentLayer = 0}) {
if ((string?.length ?? 0) == 0) return 0;
String splitString = currentLayer == 0 ? "." : ":";
int parsed =
int.tryParse(string.substring(string.lastIndexOf(splitString) + 1)) ?? 0;
Duration duration;
switch (currentLayer) {
case 0:
duration = Duration(milliseconds: parsed);
break;
case 1:
duration = Duration(seconds: parsed);
break;
case 2:
duration = Duration(minutes: parsed);
break;
case 3:
duration = Duration(hours: parsed);
break;
}
return duration.inMilliseconds +
parseTimeString(
string.substring(
0,
string.lastIndexOf(splitString) == -1
? 0
: string.lastIndexOf(splitString)),
currentLayer: currentLayer + 1);
}
Edit2:我认为这对你有用。
int parseTimeString(String string, {int currentLayer = 0}) {
if ((string?.length ?? 0) == 0) return 0;
String splitString = currentLayer == 0 ? "." : ":";
String substring = string.substring(string.lastIndexOf(splitString) + 1);
if(currentLayer == 0){
while(substring.length<3){
substring += "0";
}
}
int parsed =
int.tryParse(substring) ?? 0;
Duration duration;
switch (currentLayer) {
case 0:
duration = Duration(milliseconds: parsed);
break;
case 1:
duration = Duration(seconds: parsed);
break;
case 2:
duration = Duration(minutes: parsed);
break;
case 3:
duration = Duration(hours: parsed);
break;
}
return duration.inMilliseconds +
parseTimeString(
string.substring(
0,
string.lastIndexOf(splitString) == -1
? 0
: string.lastIndexOf(splitString)),
currentLayer: currentLayer + 1);
}
String timeToString(int millis){
Duration duration = Duration(milliseconds: millis);
String result = duration.toString();
while(result.substring(result.length-1)=="0"){
result = result.substring(0,result.length-1);
}
if(result.substring(result.length-1)==".")
result += "0";
return result;
}
我正在尝试在我的数据库中保存可变长度的时间。最长的可能包括数小时,较短的可能只有几分钟。较短的字符串只会显示为“mm:ss.ms”或“:ss.ms”,因此整体长度会更短。
我试过DateTime
,但它需要年月日。我尝试将日期设置为 0000-00-00,但没有成功。
我尝试对所有内容进行硬编码,但希望有更优雅的解决方案。这是我得到的结果:
// for shorter string of type ":mm.ms"
if (myString.length >= 3) AND (myString.length <= 4) {
if (myString.contains(':')) {
myString.substring(1); //remove initial semicolon
}
int _seconds = int.parse(myString.substring(0,1));
int _milliseconds = int.parse(myString.substring(3,3));
}
任何指点将不胜感激。
如果我解决了你的问题,这可能是适合你的解决方案:
String test = ":23.24";
String minutes = test.split(":")[0];
String seconds = test.split(":")[1].split(".")[0];
String milliseconds = test.split(":")[1].split(".")[1];
Duration duration = Duration(
minutes: int.tryParse(minutes) ?? 0,
seconds: int.tryParse(seconds) ?? 0,
milliseconds: int.tryParse(milliseconds) ?? 0);
print(duration.inMilliseconds);
编辑:我找到了另一种方法,它也允许几个小时而且更可靠:
int parseTimeString(String string, {int currentLayer = 0}) {
if ((string?.length ?? 0) == 0) return 0;
String splitString = currentLayer == 0 ? "." : ":";
int parsed =
int.tryParse(string.substring(string.lastIndexOf(splitString) + 1)) ?? 0;
Duration duration;
switch (currentLayer) {
case 0:
duration = Duration(milliseconds: parsed);
break;
case 1:
duration = Duration(seconds: parsed);
break;
case 2:
duration = Duration(minutes: parsed);
break;
case 3:
duration = Duration(hours: parsed);
break;
}
return duration.inMilliseconds +
parseTimeString(
string.substring(
0,
string.lastIndexOf(splitString) == -1
? 0
: string.lastIndexOf(splitString)),
currentLayer: currentLayer + 1);
}
Edit2:我认为这对你有用。
int parseTimeString(String string, {int currentLayer = 0}) {
if ((string?.length ?? 0) == 0) return 0;
String splitString = currentLayer == 0 ? "." : ":";
String substring = string.substring(string.lastIndexOf(splitString) + 1);
if(currentLayer == 0){
while(substring.length<3){
substring += "0";
}
}
int parsed =
int.tryParse(substring) ?? 0;
Duration duration;
switch (currentLayer) {
case 0:
duration = Duration(milliseconds: parsed);
break;
case 1:
duration = Duration(seconds: parsed);
break;
case 2:
duration = Duration(minutes: parsed);
break;
case 3:
duration = Duration(hours: parsed);
break;
}
return duration.inMilliseconds +
parseTimeString(
string.substring(
0,
string.lastIndexOf(splitString) == -1
? 0
: string.lastIndexOf(splitString)),
currentLayer: currentLayer + 1);
}
String timeToString(int millis){
Duration duration = Duration(milliseconds: millis);
String result = duration.toString();
while(result.substring(result.length-1)=="0"){
result = result.substring(0,result.length-1);
}
if(result.substring(result.length-1)==".")
result += "0";
return result;
}