get current time and convert it to string

kris foo at bar.com
Thu Apr 27 09:48:56 PDT 2006


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
                  );
}



More information about the Digitalmars-d-learn mailing list