Jump to page: 1 2
Thread overview
get current time and convert it to string
Apr 26, 2006
Abby (J.P.)
Apr 26, 2006
Stewart Gordon
Aug 02, 2007
C. Dunn
Apr 26, 2006
Tydr Schnubbis
Apr 26, 2006
Abby (J.P.)
Apr 27, 2006
Derek Parnell
Apr 27, 2006
Carlos Santander
Apr 27, 2006
Stewart Gordon
Apr 27, 2006
Carlos Santander
Apr 27, 2006
Abby (J.P.)
Apr 27, 2006
Derek Parnell
Apr 27, 2006
Abby (J.P.)
Apr 27, 2006
kris
Apr 27, 2006
John C
April 26, 2006
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.
April 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.
April 26, 2006
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);
April 26, 2006
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);
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.
April 27, 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
April 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.
April 27, 2006
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.
> <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.

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
April 27, 2006
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
April 27, 2006
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.
> <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.
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)

--
Abby
April 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
« First   ‹ Prev
1 2