How format UnixTime to "%Hh:%Mm:%Ss" ?

Marcone marcone at email.com
Sun Nov 15 03:08:48 UTC 2020


On Sunday, 15 November 2020 at 02:29:20 UTC, Anonymouse wrote:
> On Sunday, 15 November 2020 at 01:04:41 UTC, Marcone wrote:
>> auto mytime = Clock.currTime().toUnixTime()
>>
>> writeln(strftime("%Hh:%Mm:%Ss", mytime)); How can I make some 
>> like this in D?
>
> auto mytime = Clock.currTime;
> writefln("%02dh:%02dm:%02ds", mytime.hour, mytime.minute, 
> mytime.second);
>
> auto mytimeFromUnix = SysTime.fromUnixTime(1234567890);
> writefln("%02dh:%02dm:%02ds", mytimeFromUnix.hour, 
> mytimeFromUnix.minute, mytimeFromUnix.second);
>
> https://run.dlang.io/is/TAO6Q9

Your code get wrong hour result: 21hours, only minuts and seconds 
correct.
I'm using this example:

import std;
import core.thread;

// Functiono strftime()
string strftime(string text, int tempo){
	int H = to!int(tempo / (60*60));
	int M = to!int(tempo % (60*60) / 60);
	int S = to!int(tempo % (60*60) % 60);
	return text.replace("%H", H.to!string).replace("%M", 
M.to!string).replace("%S", S.to!string);
}

void main(){
	int starttime = Clock.currTime().toUnixTime();
	Thread.sleep(5.seconds);
	int endtime = Clock.currTime().toUnixTime() - starttime;
	writeln(strftime("%Hh:%Mm:%Ss", endtime));
}


More information about the Digitalmars-d-learn mailing list