Thread overview | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
November 15, 2020 How format UnixTime to "%Hh:%Mm:%Ss" ? | ||||
---|---|---|---|---|
| ||||
auto mytime = Clock.currTime().toUnixTime() writeln(strftime("%Hh:%Mm:%Ss", mytime)); How can I make some like this in D? |
November 15, 2020 Re: How format UnixTime to "%Hh:%Mm:%Ss" ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marcone | 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 |
November 15, 2020 Re: How format UnixTime to "%Hh:%Mm:%Ss" ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anonymouse | 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)); } |
November 15, 2020 Re: How format UnixTime to "%Hh:%Mm:%Ss" ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anonymouse | 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
I want to convert seconds to hour, minut and second.
|
November 15, 2020 Re: How format UnixTime to "%Hh:%Mm:%Ss" ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marcone | 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
|
November 15, 2020 Re: How format UnixTime to "%Hh:%Mm:%Ss" ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anonymouse | 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); } |
November 15, 2020 Re: How format UnixTime to "%Hh:%Mm:%Ss" ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anonymouse | 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 See my program for you understand better: Here is the picture of my program, see red mark: https://i.imgur.com/D5nsNHM.png |
November 15, 2020 Re: How format UnixTime to "%Hh:%Mm:%Ss" ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Marcone | On Sunday, 15 November 2020 at 03:14:07 UTC, Marcone wrote: > I want to convert seconds to hour, minut and second. Do you want to convert a *duration* into hours/minutes/seconds, or format a UNIX *timestamp* to hours/minutes/seconds? These are conceptually two different things. `Clock.currTime` will return a `SysTime` (a point in time) in your local timezone. If you want to get the hour/minute/second parts of a duration of time (for instance, in seconds), use a `Duration` and `split`. // Função strftime() string strftime(string fmt, int tempo){ long H, M, S; tempo.seconds.split!("hours", "minutes", "seconds")(H, M, S); return fmt.replace("%H", H.text).replace("%M", M.text).replace("%S", S.text); } writeln(strftime("%H:%M:%S", 4783)); // 1:19:43 https://run.dlang.io/is/0L5yqP https://dlang.org/articles/intro-to-datetime.html |
Copyright © 1999-2021 by the D Language Foundation