Download Reference Manual
The Developer's Library for D
About Wiki Forums Source Search Contact

Date and time with tango

Moderators: larsivi kris

Posted: 02/15/08 10:20:22

Hi Forum,

i try to switch from phobos to tango and need some translation help.

phobos:
// initialize time

char [] timenowstring = std.date.toString(std.date.getUTCtime());
Date timenow;
timenow.parse(timenowstring);

// /initialize time
tango: ?

I just need the whole time/date attributes in a struct to get things like timenow.weekday . Thanks.

Regards Engineer

Author Message

Posted: 02/15/08 10:52:11

You will find the time structures in tango.time.Time and some helper functionality in the same package.

See ChapterDateTime for some reference documentation.

If you still have problems, please ask again :)

Posted: 02/15/08 11:46:48

Thanks for your quick answer. That page helped me a lot.
Here's what I was looking for:
auto time =
WallClock?.toDate;

Regards Engineer

Posted: 02/21/08 10:45:17

I got another translation problem:

phobos:
File alertlist = new File;
try{alertlist.open(alertlistfilename,
FileMode?.In);}
catch(OpenException? e){logger("File not found");return -1;}
string linetemp;
while(!alertlist.eof())
{linetemp = alertlist.readLine();
if (linetemp != ""){...do something with line...}}

tango:
auto alertlistpath = new FilePath?("alertlist.txt");
if(alertlistpath.exists){?????need?each?file?line?????}
else{logger("File not found");return -1;}

I've read all the API informations (Streaming,LineIterator?,etc.) but since I have no C++ experience and just started to learn D it's hard to do without any examples. I managed to write into a text file but don't know how to read each single line into a variable.

Posted: 02/21/08 14:28:13

try:

import tango.io.stream.LineStream;
import tango.io.FileConduit;

...

foreach(line; new LineInput(new FileConduit(alertlistpath)))
{
   // use line as appropriate, the buffer is reused, so make
   // copies if you plan to use the data outside the loop
}

I'd say use FileInput?, but that doesn't support opening from a FilePath? (kinda strange that that's not the case).

-Steve

Posted: 02/21/08 16:56:52

Ok now I get the lines, but at the end of the file I get an error message:
object.Exception: Access Violation

Posted: 02/22/08 02:36:35

I don't know if this is related or not, since I don't know what you're doing with those lines after they're read in. But, keep in mind that Tango generally return slices of data instead of copies. That can create issue in some cases if you're not aware of that distinction

Posted: 02/22/08 18:24:26

I wanted to put the lines into an array. But even if there's no code in the foreach loop (like schveiguy posted it) I get this error message and the program aborts.
foreach(line;new
LineIterator?!(char)(new FileConduit?(logpath)))
{

//nothing

}
or
auto input = new LineInput?(new FileConduit?(logpath));
foreach (line; input)
{

//nothing

}
or
foreach(line; new LineInput?(new FileConduit?(logpath)))
{

//nothing

}
or
foreach(line; new LineInput?(new FileInput?("log.txt")))
{

//nothing

}
Everytime the same error.

Posted: 02/22/08 23:55:45 -- Modified: 02/23/08 03:45:56 by
kris -- Modified 2 Times

Can you post an example, please? Note that there's an example of this in the Tango distro, also:

private import  tango.io.Console,
                tango.io.FileConduit;

private import  tango.text.stream.LineIterator;

/*******************************************************************************

        Read a file line-by-line, sending each one to the console. This
        illustrates how to bind a conduit to a stream iterator (iterators
        also support the binding of a buffer). Note that stream iterators
        are templated for char, wchar and dchar types.

*******************************************************************************/

void main (char[][] args)
{
        if (args.length is 2)
           {
           // open a file for reading
           scope file = new FileConduit (args[1]);

           // process file one line at a time
           foreach (line; new LineIterator!(char)(file))
                    Cout (line).newline;
           }
        else
           Cout ("usage: lineio filename").newline;
}

Posted: 02/24/08 20:04:56

I already tried this example, same error message. Here's an example of my code:

scope progpath = new FilePath(args[0]);
string progname = progpath.name();
string progdirectory = progpath.path();
auto logpath = new FilePath(progdirectory~"log.txt");

string [] textfilecontent;
textfilecontent.length = 1;
int counter = 0;
foreach(line; new LineInput(new FileConduit(logpath)))
{
    textfilecontent[counter] = line;
    Stdout(textfilecontent[counter]).newline;
    counter++;
}

Posted: 02/25/08 05:32:59

You say the Tango example throws an exception? Then, please post the exception text and so on.

The code you posted above has some serious errors in it. You're incrementing counter and subsequently trying to write past the end of your array (because you have not allocated sufficient space there). Also, you must have been using -O or -release or something to disable the useful error messages the compiler would otherwise have given you (such as ArrayIndexOutOfBounds).

Unfortunately, you shot yourself in both feet ;)

Posted: 02/26/08 05:56:53

Its also not duplicating the content of line..

scope progpath = new FilePath(args[0]);
string progname = progpath.name();
string progdirectory = progpath.path();
auto logpath = new FilePath(progdirectory~"log.txt");

string [] textfilecontent;
foreach(line; new LineInput(new FileConduit(logpath)))
{
    textfilecontent ~= line.dup;
    Stdout(line).newline;
}
auto counter = textfilecontent.length;

or perhaps you just intended to read a single line?

Posted: 02/26/08 19:32:30

@kris: I am using Code::Blocks without any parameters like -o or -release and the newest tango with included DMD. The Object Exception is always the same: object.Exception: Access Violation. There was no Array error with that code.

@r.lph50: With your code I got: tango.core.Exception.OutOfMemoryException?: Memory allocation failed. When I comment out the "textfilecontent ~= line.dup;" line the error object.Exception: Access Violation appears again.

Posted: 02/27/08 00:28:22

Engineer - before we get any further you'll need to resolve three things:

1) you say that the Tango example shown above fails. If this is the case it would indicate a broken build, which we would need to resolve regardless.

2) item #1 aside, you still have an fatal condition in your code as noted above (plus one non-fatal), which has nothing to do with Tango itself (you're writing past the end of your array).

3) I suggest that a -release flag is likely set somewhere in your IDE config, since you should get an ArrayOutOfBoundsError? for the code you've posted above. Getting an AccessViolation? instead is an indicator that array-bounds-checking has been disabled, meaning -release was set by something somewhere.

Posted: 02/29/08 21:05:03

Problem solved. I reinstalled the whole tango/dmd package and everything works fine now. Thank you guys for the good support.

Posted: 02/29/08 22:41:54

Great! :)