Skip to content

date() should not append a final "\n" ?!?

8 messages · Friedrich Leisch, Kurt Hornik, Peter Dalgaard +1 more

#
Between R 0.63.2 and 0.64, the behavior of 
  date()
has been changed in order to become platform independent.
It now uses POSIX calls, basically

    time_t t;
    time(&t);
    return ctime(&t);


This currently returns (for me on Sun SPARC Solaris)

  > date()
  [1] "Thu Aug 19 10:36:28 1999\n"

where I think the final "\n" is really UNdesired.

---
Is this just my (Sun's) version of ctime(), i.e.,
does anyone *NOT* get the final new line ?

Thanks for your feedback.
Martin Maechler <maechler@stat.math.ethz.ch>	http://stat.ethz.ch/~maechler/
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
MM> Between R 0.63.2 and 0.64, the behavior of 
MM>   date()
MM> has been changed in order to become platform independent.
MM> It now uses POSIX calls, basically

MM>     time_t t;
MM>     time(&t);
MM>     return ctime(&t);


MM> This currently returns (for me on Sun SPARC Solaris)
MM>   [1] "Thu Aug 19 10:36:28 1999\n"

MM> where I think the final "\n" is really UNdesired.

Yes!


MM> ---
MM> Is this just my (Sun's) version of ctime(), i.e.,
MM> does anyone *NOT* get the final new line ?


Debian Linux:

R> date()
[1] "Thu Aug 19 10:47:17 1999\n"


.f
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
MM> Between R 0.63.2 and 0.64, the behavior of 
    MM> date()
    MM> has been changed in order to become platform independent.
    MM> It now uses POSIX calls, basically

    MM> time_t t;
    MM> time(&t);
    MM> return ctime(&t);


    MM> This currently returns (for me on Sun SPARC Solaris)

    >>> date()
    MM> [1] "Thu Aug 19 10:36:28 1999\n"

    MM> where I think the final "\n" is really UNdesired.

    FrL> Yes!

Okay,
I checked  Lewine (1991) "POSIX Programmer's Guide" :

POSIX specifies that ctime() has a final "\n" 
(before the string terminator \0)
and always length 26 (incl. terminator)

Hence, we have to drop the final "\n",
already in function R_Date() which is [in src/main/platform.c]
currently
    char *R_Date()
    {
	time_t t;
	time(&t);
	return ctime(&t);
    }

How can we drop is this done in the most elegant way?
(without having to allocate a  char[26] ?)


 
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
MM> Between R 0.63.2 and 0.64, the behavior of 
MM> date()
MM> has been changed in order to become platform independent.
MM> It now uses POSIX calls, basically

MM> time_t t;
MM> time(&t);
MM> return ctime(&t);


MM> This currently returns (for me on Sun SPARC Solaris)
MM> [1] "Thu Aug 19 10:36:28 1999\n"

MM> where I think the final "\n" is really UNdesired.

FrL> Yes!
Be brutal, do

	date <- function() substring(.Internal(date()), 1, 24)

-k
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
MM> Between R 0.63.2 and 0.64, the behavior of 
    MM> date()
    MM> has been changed in order to become platform independent.
    MM> It now uses POSIX calls, basically

    MM> time_t t;
    MM> time(&t);
    MM> return ctime(&t);


    MM> This currently returns (for me on Sun SPARC Solaris)

    >>>>> date()
    MM> [1] "Thu Aug 19 10:36:28 1999\n"

    MM> where I think the final "\n" is really UNdesired.

    FrL> Yes!

    >> Okay,
    >> I checked  Lewine (1991) "POSIX Programmer's Guide" :

    >> POSIX specifies that ctime() has a final "\n" 
    >> (before the string terminator \0)
    >> and always length 26 (incl. terminator)

    >> Hence, we have to drop the final "\n",
    >> already in function R_Date() which is [in src/main/platform.c]
    >> currently
    >> char *R_Date()
    >> {
    >>   time_t t;
    >>   time(&t);
    >>   return ctime(&t);
    >> }

    >> How can we drop is this done in the most elegant way?
    >> (without having to allocate a  char[26] ?)

    KH> Be brutal, do

    KH> date <- function() substring(.Internal(date()), 1, 24)

Kurt, I said
		"most elegant way"
about 5 lines above..

And also for the C API, we want  R_Date() to be correct.

{{I really wonder that the POSIX people where thinking when they
  appended the "\n";  
  appending afterwards is always much easier than removing....
}}
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
MM> Between R 0.63.2 and 0.64, the behavior of 
MM> date()
MM> has been changed in order to become platform independent.
MM> It now uses POSIX calls, basically

MM> time_t t;
MM> time(&t);
MM> return ctime(&t);


MM> This currently returns (for me on Sun SPARC Solaris)
MM> [1] "Thu Aug 19 10:36:28 1999\n"

MM> where I think the final "\n" is really UNdesired.

FrL> Yes!
KH> Be brutal, do

KH> date <- function() substring(.Internal(date()), 1, 24)
But you did not define `elegant' ... :-)
Define `correct'.  That would be without the trailing newline?
Suggestion:

	return strtok(ctime(&t), "\n");

-k
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
#
Kurt Hornik <Kurt.Hornik@ci.tuwien.ac.at> writes:
s<-ctime(&t); s[25] = '\0'; return s;

This won't work if s is read-only (which I don't think it is), but
neither will strtok since it pokes a null in the same spot....
#
...

    MM> Between R 0.63.2 and 0.64, the behavior of date() has been changed
    MM> in order to become platform independent.  It now uses POSIX calls,
    MM> basically

    MM> time_t t; time(&t); return ctime(&t);


    MM> This currently returns (for me on Sun SPARC Solaris)

    >>>>>>> date()
    MM> [1] "Thu Aug 19 10:36:28 1999\n"

    MM> where I think the final "\n" is really UNdesired.

    FrL> Yes!

    >>>> Okay, I checked Lewine (1991) "POSIX Programmer's Guide" :

    >>>> POSIX specifies that ctime() has a final "\n" (before the string
    >>>> terminator \0) and always length 26 (incl. terminator)

    >>>> Hence, we have to drop the final "\n", already in function
    >>>> R_Date() which is [in src/main/platform.c] currently char
    >>>> *R_Date() { time_t t; time(&t); return ctime(&t); }

    >>>> How can we drop is this done in the most elegant way?  (without
    >>>> having to allocate a char[26] ?)

 <...>

    KH> Suggestion:

    KH> 	return strtok(ctime(&t), "\n");

Thanks Kurt, 
this works (and *is* elegant!);  

I've applied and committed it for 0.65.

Martin
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._