SunOS 5.10 (Solaris 10) is pretty old. There are some things it doesn't do.
In particular, I note that its date
command relies on strftime()
for formatting, and according to the man page, Solaris 10's strftime does not support %s
. Which explains the results in the test you did in your question.
If you really just want a YYYYMMDD format, the following should work:
$ date '+%Y%m%d'
If you're looking for an epoch second, then you might have to use some other tool. Nawk, for example, should be installed on your system:
$ nawk 'BEGIN{print srand}'
You can man nawk
and search for srand
to see why this works.
Alternately, you could use Perl, if you've installed it:
$ perl -e 'print time . "\n";'
These are strategies to get the current epoch second, but your initial date
command is the correct way to get the date formatted the way you suggested.
Based on other comments, it also appears you're looking to get the timestamp of certain files. That's not something the date
command will do. In other operating systems, like Linux or FreeBSD, you'd use the stat
command, which does not appear as a separate shell command in Solaris 10.
But you can do it yourself in a pretty short C program that uses the fstat(2)
system call. While it may be beyond the scope required for this question, you can probably compile this using /usr/sfw/bin/gcc
:
#include <stdio.h>
#include <time.h>
#include <fcntl.h>
#include <sys/stat.h>
int main(int argc, char **argv)
{
struct tm info;
char buf[80];
struct stat fileStat;
time_t epoch;
if(argc != 2)
return 1;
int file=0;
if((file=open(argv[1],O_RDONLY)) < -1)
return 1;
if(fstat(file,&fileStat) < 0)
return 1;
info = *localtime( &fileStat.st_mtime );
epoch = mktime(&info);
printf("%s:%ld\n", argv[1], (long) epoch );
return 0;
}
For example:
$ gcc -o filedate filedate.c
$ ./filedate /bin/ls
/bin/ls:1309814136
$ perl -e 'use POSIX; print POSIX::strftime("%F %T\n", gmtime(1309814136));'
2011-07-04 21:15:36
$ ls -l /bin/ls
-r-xr-xr-x 1 root bin 18700 Jul 4 2011 /bin/ls
$