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

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


On Sunday, 15 November 2020 at 03:15:29 UTC, Anonymouse wrote:
> On Sunday, 15 November 2020 at 03:08:48 UTC, Marcone wrote:
>> 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.
>
> If you're looking at the run.dlang.io output, the server is 
> probably in a different timezone than yours.
>
> Tested locally at 04:14 instead of sleeping:
>
> $ dmd -run mytime.d
> 04h:14m:06s
> 00h:31m:30s

I do this:

// Função strftime()
string strftime(string text, int tempo){
	int H = to!int(dur!"seconds"(tempo).total!"hours");
	int M = to!int(dur!"seconds"(tempo % (60*60)).total!"minutes");
	int S = to!int(dur!"seconds"(tempo % (60*60) % 
60).total!"seconds");
	return text.replace("%H", H.to!string).replace("%M", 
M.to!string).replace("%S", S.to!string);
}


More information about the Digitalmars-d-learn mailing list