Jump to page: 1 2
Thread overview
Date formatting in D
Mar 07, 2017
aberba
Mar 07, 2017
ikod
Mar 08, 2017
aberba
Mar 08, 2017
Adam D. Ruppe
Mar 08, 2017
aberba
Mar 08, 2017
Adam D. Ruppe
Mar 07, 2017
aberba
Mar 07, 2017
Daniel Kozak
Mar 07, 2017
Daniel Kozak
Mar 07, 2017
Jonathan M Davis
Mar 07, 2017
Daniel Kozak
March 07, 2017
I've been trying to figure out an inbuilt functionality in phobos for formatting date. In my use case, I've been trying to format current Unix timestamp to something like "Thu, 08 Mar 2017 12:00:00 GMT".

How do I go by this easily (Currently, long concatenation of strings is what I'm thinking)?

I saw this thread[1] from 2011 on similar issue. Has it been resolved? What is the state of toCustomString?

[1] http://forum.dlang.org/post/mailman.673.1320367887.24802.digitalmars-d-learn@puremagic.com
March 07, 2017
On Tuesday, 7 March 2017 at 20:29:07 UTC, aberba wrote:
> I've been trying to figure out an inbuilt functionality in phobos for formatting date. In my use case, I've been trying to format current Unix timestamp to something like "Thu, 08 Mar 2017 12:00:00 GMT".
>
> How do I go by this easily (Currently, long concatenation of strings is what I'm thinking)?
>
> I saw this thread[1] from 2011 on similar issue. Has it been resolved? What is the state of toCustomString?
>
> [1] http://forum.dlang.org/post/mailman.673.1320367887.24802.digitalmars-d-learn@puremagic.com

Straightforward solution:

https://dlang.org/library/core/stdc/time/strftime.html
http://man7.org/linux/man-pages/man3/strftime.3.html

Maybe there is something better.
March 07, 2017
On Tuesday, 7 March 2017 at 20:29:07 UTC, aberba wrote:
> I've been trying to figure out an inbuilt functionality in phobos for formatting date. In my use case, I've been trying to format current Unix timestamp to something like "Thu, 08 Mar 2017 12:00:00 GMT".
>
> How do I go by this easily (Currently, long concatenation of strings is what I'm thinking)?
>
> I saw this thread[1] from 2011 on similar issue. Has it been resolved? What is the state of toCustomString?
>
> [1] http://forum.dlang.org/post/mailman.673.1320367887.24802.digitalmars-d-learn@puremagic.com

Just found this http://code.dlang.org/packages/datetimeformat.

Why is Phobos not still not having this in 2017?
March 07, 2017
I do not know? Is this some ISO/ANSI format for dates? If yes than we should add it. If no there is no reason.


Dne 7.3.2017 v 22:07 aberba via Digitalmars-d-learn napsal(a):
> On Tuesday, 7 March 2017 at 20:29:07 UTC, aberba wrote:
>> I've been trying to figure out an inbuilt functionality in phobos for formatting date. In my use case, I've been trying to format current Unix timestamp to something like "Thu, 08 Mar 2017 12:00:00 GMT".
>>
>> How do I go by this easily (Currently, long concatenation of strings is what I'm thinking)?
>>
>> I saw this thread[1] from 2011 on similar issue. Has it been resolved? What is the state of toCustomString?
>>
>> [1] http://forum.dlang.org/post/mailman.673.1320367887.24802.digitalmars-d-learn@puremagic.com
>
> Just found this http://code.dlang.org/packages/datetimeformat.
>
> Why is Phobos not still not having this in 2017?

March 07, 2017
Dne 7.3.2017 v 22:07 aberba via Digitalmars-d-learn napsal(a):

> On Tuesday, 7 March 2017 at 20:29:07 UTC, aberba wrote:
>> I've been trying to figure out an inbuilt functionality in phobos for formatting date. In my use case, I've been trying to format current Unix timestamp to something like "Thu, 08 Mar 2017 12:00:00 GMT".
>>
>> How do I go by this easily (Currently, long concatenation of strings is what I'm thinking)?
>>
>> I saw this thread[1] from 2011 on similar issue. Has it been resolved? What is the state of toCustomString?
>>
>> [1] http://forum.dlang.org/post/mailman.673.1320367887.24802.digitalmars-d-learn@puremagic.com
>
> Just found this http://code.dlang.org/packages/datetimeformat.
>
> Why is Phobos not still not having this in 2017?

I have looked at some of our many years old codebase and we use
http://dlang.org/phobos/std_format.html#.formattedRead

and

http://dlang.org/phobos/std_format.html#.formattedWrite

March 07, 2017
Dne 7.3.2017 v 21:29 aberba via Digitalmars-d-learn napsal(a):

> I've been trying to figure out an inbuilt functionality in phobos for formatting date. In my use case, I've been trying to format current Unix timestamp to something like "Thu, 08 Mar 2017 12:00:00 GMT".
>
> How do I go by this easily (Currently, long concatenation of strings is what I'm thinking)?
>
> I saw this thread[1] from 2011 on similar issue. Has it been resolved? What is the state of toCustomString?
>
> [1] http://forum.dlang.org/post/mailman.673.1320367887.24802.digitalmars-d-learn@puremagic.com
Sorry for my previous comments. I have not been reading this post carefully. AFAIK you want some universal (general) format method for dates. This make sense. So if you write pull request I believe it will be accepted.
March 07, 2017
On Tuesday, March 07, 2017 22:15:39 Daniel Kozak via Digitalmars-d-learn wrote:
> I do not know? Is this some ISO/ANSI format for dates? If yes than we should add it. If no there is no reason.

The ISO formats are already there. There's to/fromISOString and to/fromISOExtString on SysTime, DateTime, Date, and TimeOfDay. What's missing is custom date/time formatting, and it's on my todo list, but I've had enough other stuff going on that I haven't gotten around to it even though I should have done it ages ago.

- Jonathan M Davis

March 08, 2017
On Tuesday, 7 March 2017 at 20:52:39 UTC, ikod wrote:
> On Tuesday, 7 March 2017 at 20:29:07 UTC, aberba wrote:
>> I've been trying to figure out an inbuilt functionality in phobos for formatting date. In my use case, I've been trying to format current Unix timestamp to something like "Thu, 08 Mar 2017 12:00:00 GMT".
>>
>> How do I go by this easily (Currently, long concatenation of strings is what I'm thinking)?
>>
>> I saw this thread[1] from 2011 on similar issue. Has it been resolved? What is the state of toCustomString?
>>
>> [1] http://forum.dlang.org/post/mailman.673.1320367887.24802.digitalmars-d-learn@puremagic.com
>
> Straightforward solution:
>
> https://dlang.org/library/core/stdc/time/strftime.html
> http://man7.org/linux/man-pages/man3/strftime.3.html
>
> Maybe there is something better.

Having to do these stuff with C is a punch in the face.

Or PHP was:

date(format, timestamp);

Almost every other language (ive used) has this equivalent.
March 08, 2017
On Wednesday, 8 March 2017 at 15:29:11 UTC, aberba wrote:
> Having to do these stuff with C is a punch in the face.
>
> Or PHP was:
>
> date(format, timestamp);

The  PHP function is basically just (translated to D):

string date(string format, time_t timestamp) {
    char[256] buffer;
    auto ret = strftime(buffer.ptr, buffer.ptr, toStringz(format), gmtime(&timestamp);
    return buffer[0 .. ret].idup;
}


in other words, it is a thin wrapper around the C function.

Let's see, how do we get a time_t out of D's std.datetime?

http://dpldocs.info/locate?q=time_t

The SysTime "toUnixTime" looks good:

http://dpldocs.info/experimental-docs/std.datetime.SysTime.toUnixTime.html



So an overload might be


string date(string format, SysTime time) {
    date(format, time.toUnixTime());
}
March 08, 2017
On Wednesday, 8 March 2017 at 15:46:42 UTC, Adam D. Ruppe wrote:
> On Wednesday, 8 March 2017 at 15:29:11 UTC, aberba wrote:
>> [...]
>
> The  PHP function is basically just (translated to D):
>
> string date(string format, time_t timestamp) {
>     char[256] buffer;
>     auto ret = strftime(buffer.ptr, buffer.ptr, toStringz(format), gmtime(&timestamp);
>     return buffer[0 .. ret].idup;
> }
>
>
> in other words, it is a thin wrapper around the C function.
>
> Let's see, how do we get a time_t out of D's std.datetime?
>
> http://dpldocs.info/locate?q=time_t
>
> The SysTime "toUnixTime" looks good:
>
> http://dpldocs.info/experimental-docs/std.datetime.SysTime.toUnixTime.html
>
>
>
> So an overload might be
>
>
> string date(string format, SysTime time) {
>     date(format, time.toUnixTime());
> }

Your docs page is really effective, not pretty though.
« First   ‹ Prev
1 2