April 27, 2006 Re: get current time and convert it to string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Derek Parnell | 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
|
April 27, 2006 Re: get current time and convert it to string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Abby (J.P.) | 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 ); } |
April 27, 2006 Re: get current time and convert it to string | ||||
---|---|---|---|---|
| ||||
Posted in reply to kris | 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
|
August 02, 2007 Re: get current time and convert it to string | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stewart Gordon | 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.
|
Copyright © 1999-2021 by the D Language Foundation