get current time and convert it to string

John C johnch_atms at hotmail.com
Thu Apr 27 10:21:00 PDT 2006


kris wrote:
> Abby (J.P.) wrote:
> 
>> Derek Parnell wrote:
>>
>>> On Fri, 28 Apr 2006 00:37:28 +1000, Abby (J.P.) 
>>> <the.ueabraham at 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



More information about the Digitalmars-d-learn mailing list