www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - get current time and convert it to string

reply "Abby (J.P.)" <the.ueabraham laposte.net> writes:
Hello,
I want to get current time and put it into a string, I tried this :


int* the_time;
std.c.time.time(the_time);

char* str_time_ptr;
str_time_ptr = std.c.time.ctime(the_time);

char[] anwser = "Current time is: " ~ *str_time_ptr;


But it does only concatenate the first letter of the array return by 
ctime. I looked for C examples, but they all use printf:
time_t rawtime;
time ( &rawtime );
printf ( "Current date and time are: %s", ctime (&rawtime) );

In my case I can't use printf, because char[] anwser has to be sent on a 
socket and printed to the screen. Do you have any solution (I'm quite 
bad at pointers, I must have missed something) ?

--
Abby.
Apr 26 2006
next sibling parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
Abby (J.P.) wrote:
 Hello,
 I want to get current time and put it into a string, I tried this :

Look at std.date or any of various third-party libraries, such as mine: http://pr.stewartsplace.org.uk/d/sutil/ Stewart.
Apr 26 2006
parent C. Dunn <cdunn2001 gmail.com> writes:
Stewart Gordon Wrote:


 Look at std.date or any of various third-party libraries, such as mine:
 
 http://pr.stewartsplace.org.uk/d/sutil/

Nice! I like the hashmap.
Aug 02 2007
prev sibling parent reply Tydr Schnubbis <fake address.dude> writes:
Abby (J.P.) wrote:
 Hello,
 I want to get current time and put it into a string, I tried this :
 
 
 int* the_time;
 std.c.time.time(the_time);
 
 char* str_time_ptr;
 str_time_ptr = std.c.time.ctime(the_time);
 
 char[] anwser = "Current time is: " ~ *str_time_ptr;
 

import std.date; char[] answer = "Current time is: " ~ std.date.toTimeString(std.date.getUTCtime()); Use the Date struct if you want more control over the format.
 
 But it does only concatenate the first letter of the array return by 
 ctime. I looked for C examples, but they all use printf:
 time_t rawtime;
 time ( &rawtime );
 printf ( "Current date and time are: %s", ctime (&rawtime) );
 

In D you won't be using zero-terminated strings except for interfacing with C functions. If you dereference a char pointer (like *str_time_ptr), you get a char. Just like a you would in C. If you call a C function that returns a pointer to a zero-terminated string, you can convert it into a D string like this: char[] str_timeto = std.string.toString(str_time_ptr); and the other way around: char* str_timeto_ptr = std.string.toStringz(str_time);
Apr 26 2006
parent reply "Abby (J.P.)" <the.ueabraham laposte.net> writes:
Tydr Schnubbis wrote:
 Abby (J.P.) wrote:
 Hello,
 I want to get current time and put it into a string, I tried this :


 int* the_time;
 std.c.time.time(the_time);

 char* str_time_ptr;
 str_time_ptr = std.c.time.ctime(the_time);

 char[] anwser = "Current time is: " ~ *str_time_ptr;

import std.date; char[] answer = "Current time is: " ~ std.date.toTimeString(std.date.getUTCtime()); Use the Date struct if you want more control over the format.
 But it does only concatenate the first letter of the array return by 
 ctime. I looked for C examples, but they all use printf:
 time_t rawtime;
 time ( &rawtime );
 printf ( "Current date and time are: %s", ctime (&rawtime) );

In D you won't be using zero-terminated strings except for interfacing with C functions. If you dereference a char pointer (like *str_time_ptr), you get a char. Just like a you would in C. If you call a C function that returns a pointer to a zero-terminated string, you can convert it into a D string like this: char[] str_timeto = std.string.toString(str_time_ptr); and the other way around: char* str_timeto_ptr = std.string.toStringz(str_time);

By the way, is there a way to set the time shifting used for the function std.date.UTCtoLocalTime(long t); ? It does automatically put me at GMT+4, and that's not valid. Thanks again for your anwsers.
Apr 26 2006
next sibling parent reply Derek Parnell <derek psych.ward> writes:
On Wed, 26 Apr 2006 21:58:48 +0200, Abby (J.P.) wrote:


 By the way, is there a way to set the time shifting used for the function
 std.date.UTCtoLocalTime(long t); ?
 It does automatically put me at GMT+4, and that's not valid.

This is an undocumented 'feature' of std.date. To set the local time zone you must set the public variable in std.date. For example, to set the time zone to -4 hours from UTC... std.date.LocalTZA = -4 * msPerHour; To get the local time zone as defined in your system's parameters ... d_time realLTZ = std.date.getLocalTZA(); -- Derek (skype: derek.j.parnell) Melbourne, Australia "Down with mediocracy!" 27/04/2006 10:33:20 AM
Apr 26 2006
parent Carlos Santander <csantander619 gmail.com> writes:
Derek Parnell escribió:
 On Wed, 26 Apr 2006 21:58:48 +0200, Abby (J.P.) wrote:
 
 
 By the way, is there a way to set the time shifting used for the function
 std.date.UTCtoLocalTime(long t); ?
 It does automatically put me at GMT+4, and that's not valid.

This is an undocumented 'feature' of std.date. To set the local time zone you must set the public variable in std.date. For example, to set the time zone to -4 hours from UTC... std.date.LocalTZA = -4 * msPerHour;

I think this shouldn't be allowed: std.date.LocalTZA should be private.
 To get the local time zone as defined in your system's parameters ...
 
    d_time realLTZ = std.date.getLocalTZA();
 

-- Carlos Santander Bernal
Apr 27 2006
prev sibling parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
Abby (J.P.) wrote:
<snip>
 Thanks, it works fine.
 By the way, is there a way to set the time shifting used for the function
 std.date.UTCtoLocalTime(long t); ?
 It does automatically put me at GMT+4, and that's not valid.

It seems that either: - your computer is misconfigured - there's a bug somewhere in Phobos - there's a bug somewhere in your code Looking at your headers of your posts, you appear to be in GMT+2 and using Windows, but is this the same system that you're programming on? Otherwise, which OS is it happening in? Please post a complete code sample that demonstrates it. Stewart.
Apr 27 2006
next sibling parent Carlos Santander <csantander619 gmail.com> writes:
Stewart Gordon escribió:
 Abby (J.P.) wrote:
 <snip>
 Thanks, it works fine.
 By the way, is there a way to set the time shifting used for the function
 std.date.UTCtoLocalTime(long t); ?
 It does automatically put me at GMT+4, and that's not valid.

It seems that either: - your computer is misconfigured - there's a bug somewhere in Phobos - there's a bug somewhere in your code Looking at your headers of your posts, you appear to be in GMT+2 and using Windows, but is this the same system that you're programming on? Otherwise, which OS is it happening in? Please post a complete code sample that demonstrates it. Stewart.

Some problems with std.date have been reported in the past (including by me), but since I'm not using DMD I don't know if they're fixed already. Searching the archives will show some test cases that failed and could be tested again to see if there have been improvements. -- Carlos Santander Bernal
Apr 27 2006
prev sibling parent reply "Abby (J.P.)" <the.ueabraham laposte.net> writes:
Stewart Gordon wrote:
 Abby (J.P.) wrote:
 <snip>
 Thanks, it works fine.
 By the way, is there a way to set the time shifting used for the function
 std.date.UTCtoLocalTime(long t); ?
 It does automatically put me at GMT+4, and that's not valid.

It seems that either: - your computer is misconfigured - there's a bug somewhere in Phobos - there's a bug somewhere in your code Looking at your headers of your posts, you appear to be in GMT+2 and using Windows, but is this the same system that you're programming on? Otherwise, which OS is it happening in? Please post a complete code sample that demonstrates it. Stewart.

set on GMT+1 (I live in France), but I also use the auto adjusting for summer time and winter time (so I'm GMT+1 during the winter, and GMT+2 during summer). So it looks like my system is well configured. Code example: import std.c.time; import std.date; int main(char[][] args) { char[] thetime = toTimeString(UTCtoLocalTime(getUTCtime())); printf("Local time: " ~ thetime); char[] thetime2 = toTimeString(getUTCtime()); printf("\nUTC time: " ~ thetime2); return 0; } Output: Local time: 18:34:33 GMT+0200 UTC time: 16:34:33 GMT+0200 Press any key to continue. (It is now 16h35, so the Local time is shift from +2 hours) -- Abby
Apr 27 2006
parent reply "Derek Parnell" <derek psych.ward> writes:
On Fri, 28 Apr 2006 00:37:28 +1000, Abby (J.P.)  
<the.ueabraham laposte.net> wrote:



      char[] thetime = toTimeString(UTCtoLocalTime(getUTCtime()));

It isn't documented but the input to toTimeString() is assumed to be UTC, and it outputs a Local time string. -- Derek Parnell Melbourne, Australia
Apr 27 2006
parent reply "Abby (J.P.)" <the.ueabraham laposte.net> writes:
Derek Parnell wrote:
 On Fri, 28 Apr 2006 00:37:28 +1000, Abby (J.P.) 
 <the.ueabraham laposte.net> wrote:
 
 
 
      char[] thetime = toTimeString(UTCtoLocalTime(getUTCtime()));

It isn't documented but the input to toTimeString() is assumed to be UTC, and it outputs a Local time string. --Derek Parnell Melbourne, Australia

GMT+2 (UTCtoLocalTime()) + GMT+2 (toTimeString()) = GMT+4 ! Thanks :) -- Abby
Apr 27 2006
parent reply kris <foo bar.com> writes:
Abby (J.P.) wrote:
 Derek Parnell wrote:
 
 On Fri, 28 Apr 2006 00:37:28 +1000, Abby (J.P.) 
 <the.ueabraham laposte.net> wrote:



      char[] thetime = toTimeString(UTCtoLocalTime(getUTCtime()));

It isn't documented but the input to toTimeString() is assumed to be UTC, and it outputs a Local time string. --Derek Parnell Melbourne, Australia

Oh I see, so it adds two times the GMT+2 shift ! GMT+2 (UTCtoLocalTime()) + GMT+2 (toTimeString()) = GMT+4 ! Thanks :) -- Abby

Alternatively, use the Mango library. There's the 'traditional' approach shown below, and there's also a rather powerful "locale" package, with all kinds of calanders and locale-sensitive formatters ... ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ private import mango.io.Print; private import mango.sys.Epoch; void main () { Epoch.Fields fields; // get current time and convert to local fields.setLocalTime (Epoch.utcMilli); // get GMT difference int tz = Epoch.tzMinutes; char sign = '+'; if (tz < 0) tz = -tz, sign = '-'; // format fields Println ("%.3s %.3s %02d %02d:%02d:%02d GMT%c%02d%02d %d", fields.toDowName, fields.toMonthName, fields.day, fields.hour, fields.min, fields.sec, sign, tz / 60, tz % 60, fields.year ); }
Apr 27 2006
parent John C <johnch_atms hotmail.com> writes:
kris wrote:
 Abby (J.P.) wrote:
 
 Derek Parnell wrote:

 On Fri, 28 Apr 2006 00:37:28 +1000, Abby (J.P.) 
 <the.ueabraham laposte.net> wrote:



      char[] thetime = toTimeString(UTCtoLocalTime(getUTCtime()));

It isn't documented but the input to toTimeString() is assumed to be UTC, and it outputs a Local time string. --Derek Parnell Melbourne, Australia

Oh I see, so it adds two times the GMT+2 shift ! GMT+2 (UTCtoLocalTime()) + GMT+2 (toTimeString()) = GMT+4 ! Thanks :) -- Abby

Alternatively, use the Mango library. There's the 'traditional' approach shown below, and there's also a rather powerful "locale" package, with all kinds of calanders and locale-sensitive formatters ... ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ private import mango.io.Print; private import mango.sys.Epoch; void main () { Epoch.Fields fields; // get current time and convert to local fields.setLocalTime (Epoch.utcMilli); // get GMT difference int tz = Epoch.tzMinutes; char sign = '+'; if (tz < 0) tz = -tz, sign = '-'; // format fields Println ("%.3s %.3s %02d %02d:%02d:%02d GMT%c%02d%02d %d", fields.toDowName, fields.toMonthName, fields.day, fields.hour, fields.min, fields.sec, sign, tz / 60, tz % 60, fields.year ); }

With mango.locale, it's as simple as this: DateTime.now.toString("ddd, dd MMMM yyyy HH:mm:ss z"); Which outputs this (for the en-gb locale): Thu, 27 April 2006 18:20:47 +1
Apr 27 2006