Jump to page: 1 2
Thread overview
[phobos] User define datetime format
Jun 07, 2011
Jonathan M Davis
Jun 07, 2011
Jonathan M Davis
June 06, 2011
Hi Jonathan et al.,

Is there any plan to add support for user define format string. Windows, Linux and MacOS define the following function:

strftime - http://msdn.microsoft.com/en-us/library/fe06s4ak%28v=VS.100%29.aspx
            - http://linux.die.net/man/3/strftime
            - http://www.manpagez.com/man/3/strftime/

I would like to add support in std.log to customize the date and time format in a log line and it would be great if this was supported by std.datetime. Support for the inverse function strptime would be nice too.

Thanks,
-Jose
June 07, 2011
On 2011-06-06 17:10, Jose Armando Garcia wrote:
> Hi Jonathan et al.,
> 
> Is there any plan to add support for user define format string. Windows, Linux and MacOS define the following function:
> 
> strftime -
> http://msdn.microsoft.com/en-us/library/fe06s4ak%28v=VS.100%29.aspx -
> http://linux.die.net/man/3/strftime
> - http://www.manpagez.com/man/3/strftime/
> 
> I would like to add support in std.log to customize the date and time format in a log line and it would be great if this was supported by std.datetime. Support for the inverse function strptime would be nice too.

I do intend to add support for user-defined strings, but I'm pretty sure that strftime isn't going to cut it. It makes assumptions based on the local time zone, and I'm not sure that it'll work across the appropriate range of values even in 64-bit (and it definitely won't in 32-bit, since you'll have a 32-bit time_t). It's going to have to be designed and implemented in D. And the difficulty of doing that is why I haven't done it yet.

The first issue is a matter of API - in particular the format string. What does it accept? I have yet to look into the proper way to do that. If there's a standard set of escape sequences for date-time strings, then that that's what we should go with. But if there isn't, then I/we need to figure out what the best solution is. It may be that following what strftime does would be the way to go, but I don't know. And once that's figured out, then I have to implement it, and I have no idea how long that will take.

If you think that you need it for std.log, then I can make it a higher priority than I have, but it's not an easy problem. I've been putting it off while I get other, more generally useful stuff done with the idea that custom date-time formats are really more of a nice-to-have than a necessity.

- Jonathan M Davis
June 06, 2011
On Mon, Jun 6, 2011 at 9:35 PM, Jonathan M Davis <jmdavisProg at gmx.com> wrote:
> The first issue is a matter of API - in particular the format string. What does it accept? I have yet to look into the proper way to do that. If there's a standard set of escape sequences for date-time strings, then that that's what we should go with. But if there isn't, then I/we need to figure out what the best solution is. It may be that following what strftime does would be the way to go, but I don't know.

If you need some data points...

apache's mod_log_config says the following:

%...{format}t 	The time, in the form given by format, which should be
in strftime(3) format. (potentially localized)

python also seems to use a similar format:

http://docs.python.org/library/datetime.html#strftime-strptime-behavior

so does ruby:

http://www.ruby-doc.org/core/classes/Time.src/M000392.html

and java:

http://download.oracle.com/javase/1.5.0/docs/api/java/util/Formatter.html


std.log doesn't need this ASAP because support for this can be added later but it would be nice to have.
June 06, 2011
Jose,


I suggest you use simple Posix positional parameters. Jonathan has a few tips on how to make that fast. You should be able to get many formats that way.


Thanks,

Andrei

On 6/6/11 7:10 PM, Jose Armando Garcia wrote:
> Hi Jonathan et al.,
>
> Is there any plan to add support for user define format string. Windows, Linux and MacOS define the following function:
>
> strftime - http://msdn.microsoft.com/en-us/library/fe06s4ak%28v=VS.100%29.aspx
>              - http://linux.die.net/man/3/strftime
>              - http://www.manpagez.com/man/3/strftime/
>
> I would like to add support in std.log to customize the date and time format in a log line and it would be great if this was supported by std.datetime. Support for the inverse function strptime would be nice too.
>
> Thanks,
> -Jose
> _______________________________________________
> phobos mailing list
> phobos at puremagic.com
> http://lists.puremagic.com/mailman/listinfo/phobos
June 07, 2011
On Tue, Jun 7, 2011 at 12:03 AM, Andrei Alexandrescu <andrei at erdani.com> wrote:
> Jose,
>
>
> I suggest you use simple Posix positional parameters. Jonathan has a few tips on how to make that fast. You should be able to get many formats that way.
>

I am planning to use positional parameters internally but I didn't want the user to have to remember that %1$s is for timestamp, %2$x is for thread id, etc. So instead I was thinking of having the user input "%t %i" which internally gets translated to "%1$s %2$x" (I already have the code for this).

%t would map to SysTime.toISOString(). If they wanted to create a custom date/time format they use "%{...}t". So, for example "%{%a, %d %b %Y %T %z}t" would result in "Tue, 07 Apr 2011 00:36:04 +0000".

Andrei, do you plan to submit your changes to formattedWrite which allow a range of arguments. I can simplify some of my code with such a feature ;)!

What does everyone think?
-Jose
June 06, 2011
On 2011-06-06 20:41, Jose Armando Garcia wrote:
> On Tue, Jun 7, 2011 at 12:03 AM, Andrei Alexandrescu <andrei at erdani.com>
wrote:
> > Jose,
> > 
> > 
> > I suggest you use simple Posix positional parameters. Jonathan has a few tips on how to make that fast. You should be able to get many formats that way.
> 
> I am planning to use positional parameters internally but I didn't want the user to have to remember that %1$s is for timestamp, %2$x is for thread id, etc. So instead I was thinking of having the user input "%t %i" which internally gets translated to "%1$s %2$x" (I already have the code for this).
> 
> %t would map to SysTime.toISOString(). If they wanted to create a custom date/time format they use "%{...}t". So, for example "%{%a, %d %b %Y %T %z}t" would result in "Tue, 07 Apr 2011 00:36:04 +0000".

I would suggest that you either use toISOExtString or toString/toSimpleString rather than toISOString, because toISOString is nigh-on-unreadable. It's more compact, and it's standard, but no one is going to want to read it. toString/toSimpleString would be the easiest to read, but it isn't standard. toISOExtString is standard and somewhat readable, so take your pick. But I'd advise against toISOString unless you're specifically going for compactness over legibility.

- Jonathan M Davis
June 07, 2011
On Tue, Jun 7, 2011 at 1:05 AM, Jonathan M Davis <jmdavisProg at gmx.com> wrote:
> On 2011-06-06 20:41, Jose Armando Garcia wrote:
>> On Tue, Jun 7, 2011 at 12:03 AM, Andrei Alexandrescu <andrei at erdani.com>
> wrote:
>> > Jose,
>> >
>> >
>> > I suggest you use simple Posix positional parameters. Jonathan has a few tips on how to make that fast. You should be able to get many formats that way.
>>
>> I am planning to use positional parameters internally but I didn't want the user to have to remember that %1$s is for timestamp, %2$x is for thread id, etc. So instead I was thinking of having the user input "%t %i" which internally gets translated to "%1$s %2$x" (I already have the code for this).
>>
>> %t would map to SysTime.toISOString(). If they wanted to create a custom date/time format they use "%{...}t". So, for example "%{%a, %d %b %Y %T %z}t" would result in "Tue, 07 Apr 2011 00:36:04 +0000".
>
> I would suggest that you either use toISOExtString or toString/toSimpleString rather than toISOString, because toISOString is nigh-on-unreadable. It's more compact, and it's standard, but no one is going to want to read it. toString/toSimpleString would be the easiest to read, but it isn't standard. toISOExtString is standard and somewhat readable, so take your pick. But I'd advise against toISOString unless you're specifically going for compactness over legibility.
>
> - Jonathan M Davis
> _______________________________________________
> phobos mailing list
> phobos at puremagic.com
> http://lists.puremagic.com/mailman/listinfo/phobos
>

Thanks. I'll use toISOExtString. I would like the default log line to be easy to parse by both humans and computers.
June 07, 2011
On 6/6/11 10:41 PM, Jose Armando Garcia wrote:
> On Tue, Jun 7, 2011 at 12:03 AM, Andrei Alexandrescu<andrei at erdani.com>  wrote:
>> Jose,
>>
>>
>> I suggest you use simple Posix positional parameters. Jonathan has a few tips on how to make that fast. You should be able to get many formats that way.
>>
>
> I am planning to use positional parameters internally but I didn't want the user to have to remember that %1$s is for timestamp, %2$x is for thread id, etc. So instead I was thinking of having the user input "%t %i" which internally gets translated to "%1$s %2$x" (I already have the code for this).

Fine, but this is chaff IMHO. Changing the format of the log line is done by a specialist once, by looking at the manual. Inventing a new convention with its own quirks has its own problems - and people will need to look at the manual anyway!

> Andrei, do you plan to submit your changes to formattedWrite which allow a range of arguments. I can simplify some of my code with such a feature ;)!

Oops, I thought I did.


Andrei
June 07, 2011
On Mon, Jun 6, 2011 at 9:35 PM, Jonathan M Davis <jmdavisProg at gmx.com> wrote:
> If you think that you need it for std.log, then I can make it a higher priority than I have, but it's not an easy problem. I've been putting it off while I get other, more generally useful stuff done with the idea that custom date-time formats are really more of a nice-to-have than a necessity.
>

std.log doesn't need strftime anymore. Found it easier to just implement this myself.
June 07, 2011
On Tue, Jun 7, 2011 at 10:47 AM, Andrei Alexandrescu <andrei at erdani.com> wrote:
> On 6/6/11 10:41 PM, Jose Armando Garcia wrote:
>> Andrei, do you plan to submit your changes to formattedWrite which allow a range of arguments. I can simplify some of my code with such a feature ;)!
>
> Oops, I thought I did.
>

While on the subject what do you think of making formattedWrite's args lazy? This is quite useful in my case where I use positional parameters with a dynamic format string. It is very likely that in the common case most of those parameters wont need to be evaluated.

It would be nice to also fix all the std.stdio write* function but I can get around that if only formattedWrite is fixed.

Thanks!
-Jose
« First   ‹ Prev
1 2