www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Date formatting in D

reply aberba <karabutaworld gmail.com> writes:
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
Mar 07 2017
next sibling parent reply ikod <geller.garry gmail.com> writes:
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.
Mar 07 2017
parent reply aberba <karabutaworld gmail.com> writes:
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.
Mar 08 2017
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
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()); }
Mar 08 2017
parent reply aberba <karabutaworld gmail.com> writes:
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.
Mar 08 2017
parent Adam D. Ruppe <destructionator gmail.com> writes:
On Wednesday, 8 March 2017 at 16:00:26 UTC, aberba wrote:
 Your docs page is really effective, not pretty though.
If you have specific complaints/suggestions, feel free to make a thread in the General forum, or email me destructionator gmail.com and I'll see what I can do (just don't want to get this thread too far off topic). I'm open to changing design, but I'll resist if it sacrifices readability. I'm not a very good designer but have been doing some usability testing here and don't want to go backward on that.
Mar 08 2017
prev sibling next sibling parent reply aberba <karabutaworld gmail.com> writes:
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?
Mar 07 2017
next sibling parent Daniel Kozak via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
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?
Mar 07 2017
prev sibling next sibling parent Daniel Kozak via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
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 and
Mar 07 2017
prev sibling parent Jonathan M Davis via Digitalmars-d-learn writes:
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
Mar 07 2017
prev sibling parent Daniel Kozak via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
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.
Mar 07 2017