digitalmars.D.learn - get current time and convert it to string
- Abby (J.P.) (17/17) Apr 26 2006 Hello,
-
Stewart Gordon
(5/7)
Apr 26 2006
- C. Dunn (2/5) Aug 02 2007 Nice! I like the hashmap.
- Tydr Schnubbis (13/32) Apr 26 2006 import std.date;
- Abby (J.P.) (6/45) Apr 26 2006 Thanks, it works fine.
- Derek Parnell (13/16) Apr 26 2006 This is an undocumented 'feature' of std.date.
- Carlos Santander (4/23) Apr 27 2006 --
-
Stewart Gordon
(12/16)
Apr 27 2006
- Carlos Santander (7/27) Apr 27 2006 Some problems with std.date have been reported in the past (including by...
- Abby (J.P.) (23/43) Apr 27 2006 I'm programming on WindowsXP home. The time (the clock on my tackbar) is...
- Derek Parnell (7/8) Apr 27 2006 It isn't documented but the input to toTimeString() is assumed to be UTC...
- Abby (J.P.) (6/18) Apr 27 2006 Oh I see, so it adds two times the GMT+2 shift !
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
Abby (J.P.) wrote:Hello, I want to get current time and put it into a string, I tried this :<snip> 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
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
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
Tydr Schnubbis wrote:Abby (J.P.) wrote: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. Thanks again for your anwsers.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
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
Derek Parnell escribió:On Wed, 26 Apr 2006 21:58:48 +0200, Abby (J.P.) wrote:I think this shouldn't be allowed: std.date.LocalTZA should be private.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();-- Carlos Santander Bernal
Apr 27 2006
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.<snip> 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
Stewart Gordon escribió:Abby (J.P.) wrote: <snip>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 BernalThanks, 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.<snip> 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
Stewart Gordon wrote:Abby (J.P.) wrote: <snip>I'm programming on WindowsXP home. The time (the clock on my tackbar) is 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) -- AbbyThanks, 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.<snip> 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
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
Derek Parnell wrote:On Fri, 28 Apr 2006 00:37:28 +1000, Abby (J.P.) <the.ueabraham laposte.net> wrote:Oh I see, so it adds two times the GMT+2 shift ! GMT+2 (UTCtoLocalTime()) + GMT+2 (toTimeString()) = GMT+4 ! Thanks :) -- Abbychar[] 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
Abby (J.P.) wrote:Derek Parnell wrote: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 ); }On Fri, 28 Apr 2006 00:37:28 +1000, Abby (J.P.) <the.ueabraham laposte.net> wrote:Oh I see, so it adds two times the GMT+2 shift ! GMT+2 (UTCtoLocalTime()) + GMT+2 (toTimeString()) = GMT+4 ! Thanks :) -- Abbychar[] 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
kris wrote:Abby (J.P.) wrote: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 +1Derek Parnell wrote: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 ); }On Fri, 28 Apr 2006 00:37:28 +1000, Abby (J.P.) <the.ueabraham laposte.net> wrote:Oh I see, so it adds two times the GMT+2 shift ! GMT+2 (UTCtoLocalTime()) + GMT+2 (toTimeString()) = GMT+4 ! Thanks :) -- Abbychar[] 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