digitalmars.D.learn - How format UnixTime to "%Hh:%Mm:%Ss" ?
- Marcone (3/3) Nov 14 2020 auto mytime = Clock.currTime().toUnixTime()
- Anonymouse (8/11) Nov 14 2020 auto mytime = Clock.currTime;
- Marcone (20/32) Nov 14 2020 Your code get wrong hour result: 21hours, only minuts and seconds
- Anonymouse (7/25) Nov 14 2020 If you're looking at the run.dlang.io output, the server is
- Marcone (2/14) Nov 14 2020 I want to convert seconds to hour, minut and second.
- Anonymouse (18/19) Nov 15 2020 Do you want to convert a *duration* into hours/minutes/seconds,
auto mytime = Clock.currTime().toUnixTime() writeln(strftime("%Hh:%Mm:%Ss", mytime)); How can I make some like this in D?
Nov 14 2020
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
Nov 14 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: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)); }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
Nov 14 2020
On Sunday, 15 November 2020 at 03:08:48 UTC, Marcone wrote:On Sunday, 15 November 2020 at 02:29:20 UTC, Anonymouse wrote: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:30sOn Sunday, 15 November 2020 at 01:04:41 UTC, Marcone wrote:Your code get wrong hour result: 21hours, only minuts and seconds correct.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
Nov 14 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: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); }On Sunday, 15 November 2020 at 02:29:20 UTC, Anonymouse wrote: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:30sOn Sunday, 15 November 2020 at 01:04:41 UTC, Marcone wrote:Your code get wrong hour result: 21hours, only minuts and seconds correct.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
Nov 14 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:See my program for you understand better: Here is the picture of my program, see red mark: https://i.imgur.com/D5nsNHM.pngOn Sunday, 15 November 2020 at 02:29:20 UTC, Anonymouse wrote: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:30sOn Sunday, 15 November 2020 at 01:04:41 UTC, Marcone wrote:Your code get wrong hour result: 21hours, only minuts and seconds correct.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
Nov 14 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:I want to convert seconds to hour, minut and second.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
Nov 14 2020
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
Nov 15 2020