Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Printing date/time in the output
PostPosted: Mon Aug 23, 2010 1:49 pm 

Joined: Sat Oct 01, 2005 11:40 am
Posts: 40
Location: IIT Guwahati, Guwahati, Assam, INDIA
I want to print the date & time in the output file. I tried various methods. I could get a working solution, but can somebody suggest me a better alternative?
These are the things I have tried:
1.
Code:
write(lnk,system ("sh","date"));

Here lnk is a writable link to a text file.
This just prints the output of the system command to the link.

2. Then I tried to write to the file using shell redirections.
Code:
write(lnk,system ("sh","date > a"));

OR

write(lnk,system ("sh","date >> a"));

This really works...

But only if the file is not open! i.e. if the file is opened by some link, then this wont work.

So the only working solution I could find is:
Code:
link lnk=":a file.txt"            // Note the :a part. The link should be appendable, not just writable.
open(lnk);
do_your_process;
close(lnk);
write(lnk,system ("sh","date >> a"));             // Note the >> redirection. If you put just one > then it will erase the whole
                                                  // file and just put the date.
open(lnk);
do_remaining_proces;
close(lnk);


Thanks and regards

VInay


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Re: Printing date/time in the output
PostPosted: Wed Aug 25, 2010 8:47 am 

Joined: Sat Oct 01, 2005 11:40 am
Posts: 40
Location: IIT Guwahati, Guwahati, Assam, INDIA
Essentially the question boils down to:

How to get the output of the following command in string:
Code:
system("sh","date")


-- VInay


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Re: Printing date/time in the output
PostPosted: Wed Aug 25, 2010 3:38 pm 

Joined: Wed Mar 03, 2010 5:08 pm
Posts: 108
Location: Germany, Münster
To answer your questions:

1.) The command system returns the errorcode of the shell command,
where 0 means that no error occured. So
Code:
write(lnk,system ("sh","date"));

will only write a 0 to the link.

2.) By construction links in Singular bhave very rigid. Once you open
an ascii-file by a link command, you can not acess it simultaneously by >.

Since you only want to write the data to an ASCII file, you have two possibilities.

Either, you don't use link but access the file simply by its filename
with preceeding > (to open) and >> (to append):

Code:
string lnkname = "file.dat";   
write(">"+lnkname,"123");             // creates and write to file.date
write(">>"+lnkname,"hello");        // appends to the file
int errcode = system("sh","date >> " + lnkname);  // append the date to file.dat
write(">>"+lnkname,"the end");
read(lnkname);    // returns the whole file as a string


OR
work constantly work with link but to get the date as a string,
you have to write it to a file, then read this file into a string,
and finally remove the file to clean the filesystem.

Here is proc which does the job:

Code:
proc timestamp()
{
// returns the date as a string
int errcode;
string fnm =  "/tmp/date"+string(system("pid"))+".sig";  // create unique name
errcode = system("sh","date > "+fnm);
string date = read(fnm);
date[size(date)]="";                   // remove endining newline
errcode = system("sh","rm -f "+fnm);   // remove the date-file
return(date);
}

> timestamp();
Mi 25. Aug 15:21:10 CEST 2010


Then you can proceed as expceted:
Code:
link lnk=":a file.txt";  // Note the :a part. The link is  appendabble not  just writable.
open(lnk);
// do_your_process;
write(lnk,timestamp());   // write the date to the file
// do_remaining_proces;
close(lnk);


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Re: Printing date/time in the output
PostPosted: Thu Aug 26, 2010 5:46 am 

Joined: Sat Oct 01, 2005 11:40 am
Posts: 40
Location: IIT Guwahati, Guwahati, Assam, INDIA
Bingo!!! This timestamp() procedure works perfectly fine... This is what I was looking for!!!

I wonder how come it didnt strike to me to read the file into a string to which I was already writing :-)

Anyways, thanks a lot!

-- VInay


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Re: Printing date/time in the output
PostPosted: Thu Aug 26, 2010 5:52 am 

Joined: Sat Oct 01, 2005 11:40 am
Posts: 40
Location: IIT Guwahati, Guwahati, Assam, INDIA
Also this write command with ">" and ">>" redirections is really a great idea. I had never used/noticed it before! :-)

VInay


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Re: Printing date/time in the output
PostPosted: Fri Aug 27, 2010 10:24 am 
Site Admin

Joined: Wed Nov 12, 2008 5:09 pm
Posts: 20
There will be a new command in the next minor SINGULAR release called 'datetime' which will return a string of the format

Www Mmm dd hh:mm:ss yyyy

where Www is the weekday, Mmm the month in letters, dd the day of the month as digits, hh:mm:ss the time, and yyyy the year.

I thought it a good idea to have this, as the above proposed workaround is a bit involved, and there actually is a C function which just needed to be pushed through the SINGULAR interpreter...

Usage will be
> datetime;
or
> string s = datetime;

Regards,
Frank


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Re: Printing date/time in the output
PostPosted: Fri Aug 27, 2010 3:22 pm 

Joined: Sat Oct 01, 2005 11:40 am
Posts: 40
Location: IIT Guwahati, Guwahati, Assam, INDIA
Dear Frank,
Pushing date() function into Singular is certainly a good idea. In fact thinking it more generally, and from the prospective feature point of view, will it be possible to "push" any C function through Singular?

I dont know whether how to implement this technically. But sometime I find that some code can be executed with C better that Singular. The workaround for that I do is
Code:
system ("sh","compile_and_execute_batch_script");

But then doing this for small code is bit tedious...

-- VInay


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Re: Printing date/time in the output
PostPosted: Fri Sep 24, 2010 6:43 pm 

Joined: Wed May 25, 2005 4:16 pm
Posts: 275
datetime() is now part of standard.lib - we will not implement anything
which can be easily done in a library.

For adding your own C/C++ function to Singular
see
http://www.singular.uni-kl.de/DynMod.ps


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

It is currently Fri May 13, 2022 11:07 am
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group