Post a reply
Username:
Note:If not registered, provide any username. For more comfort, register here.
Subject:
Message body:
Enter your message here, it may contain no more than 60000 characters. 

Smilies
:D :) :( :o :shock: :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen:
Font size:
Font colour
Options:
BBCode is ON
[img] is ON
[flash] is OFF
[url] is ON
Smilies are ON
Disable BBCode
Disable smilies
Do not automatically parse URLs
Confirmation of post
To prevent automated posts the board requires you to enter a confirmation code. The code is displayed in the image you should see below. If you are visually impaired or cannot otherwise read this code please contact the %sBoard Administrator%s.
Confirmation code:
Enter the code exactly as it appears. All letters are case insensitive, there is no zero.
   

Topic review - Printing date/time in the output
Author Message
  Post subject:  Re: Printing date/time in the output  Reply with quote
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
Post Posted: Fri Sep 24, 2010 6:43 pm
  Post subject:  Re: Printing date/time in the output  Reply with quote
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
Post Posted: Fri Aug 27, 2010 3:22 pm
  Post subject:  Re: Printing date/time in the output  Reply with quote
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
Post Posted: Fri Aug 27, 2010 10:24 am
  Post subject:  Re: Printing date/time in the output  Reply with quote
Also this write command with ">" and ">>" redirections is really a great idea. I had never used/noticed it before! :-)

VInay
Post Posted: Thu Aug 26, 2010 5:52 am
  Post subject:  Re: Printing date/time in the output  Reply with quote
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
Post Posted: Thu Aug 26, 2010 5:46 am
  Post subject:  Re: Printing date/time in the output  Reply with quote
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);
Post Posted: Wed Aug 25, 2010 3:38 pm
  Post subject:  Re: Printing date/time in the output  Reply with quote
Essentially the question boils down to:

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


-- VInay
Post Posted: Wed Aug 25, 2010 8:47 am
  Post subject:  Printing date/time in the output  Reply with quote
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
Post Posted: Mon Aug 23, 2010 1:49 pm


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