Usenet comp.unix.tru64 (2000-2013)
 help / color / mirror / Atom feed
* Can ls show seconds?
@ 2001-04-09  0:07 The Juggernaut
  2001-04-09 14:56 ` Bob Harris
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: The Juggernaut @ 2001-04-09  0:07 UTC (permalink / raw)


I've been trying to find a way to use ls to show the file creation
time in seconds.  It mentions reading LC_TIME, but can this be changed
to force ls to show seconds?  Are the seconds even stored?


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Can ls show seconds?
  2001-04-09  0:07 Can ls show seconds? The Juggernaut
@ 2001-04-09 14:56 ` Bob Harris
  2001-04-09 18:20 ` Michael Mauch
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Bob Harris @ 2001-04-09 14:56 UTC (permalink / raw)


[[ This message was both posted and mailed: see
   the "To," "Cc," and "Newsgroups" headers for details. ]]

In article <gvu1dt8o9qh5fep01imins1kq0o7j3j5i6@4ax.com>, The Juggernaut
<never@mind.com> wrote:

X  I've been trying to find a way to use ls to show the file creation
X  time in seconds.  It mentions reading LC_TIME, but can this be changed
X  to force ls to show seconds?  Are the seconds even stored?

I do not know how to make ls display seconds (I suspect that if you
mess around long enough with the locale stuff, it might be possible to
hack something so that time will display seconds).

However, in answer to your question "Are the seconds even stored?", the
answer is YES.  The ls command uses lstat(2) to obtain file information
including time.  The full time values are stored in st_mtime (seconds
since 1970) and st_umtime (microseconds).

You could write a program to extract and display the time values if you
need more precision than is available via the ls command.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>


main( int argc, char **argv )
{
    struct stat st;

    while( argv[1] != NULL ) {
        if ( lstat( argv[1], &st ) != 0 ) {
            perror( argv[1] );
            exit( EXIT_FAILURE );
        }
        printf( "%s: %s", argv[1], ctime( &st.st_mtime ) );
        argv++;
    }
}


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Can ls show seconds?
  2001-04-09  0:07 Can ls show seconds? The Juggernaut
  2001-04-09 14:56 ` Bob Harris
@ 2001-04-09 18:20 ` Michael Mauch
  2001-04-11 13:19 ` larry.gensch
  2001-04-12  4:40 ` Mark Garrett
  3 siblings, 0 replies; 5+ messages in thread
From: Michael Mauch @ 2001-04-09 18:20 UTC (permalink / raw)


The Juggernaut <never@mind.com> wrote:
> 
> I've been trying to find a way to use ls to show the file creation
> time in seconds.  It mentions reading LC_TIME, but can this be changed
> to force ls to show seconds?

Ah come on, real men don't need such accuracy, they have more fun
fiddling with this odd timestamp format ;-)

Ever tried to find out the time of a file that is more than a half year
old?

GNU ls has "ls -l --full-time", but of course that's GNUish featuritis
and nobody needs that (nobody besides you and me and the developers of
GNU ls of course).

>  Are the seconds even stored?

Yes. See the quick and dirty code below; sorry for the long lines.
(One known flaw of that code is that you can't use formats like "%20f"
for formatted filenames.)

Regards...
		Michael

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>


int Usage(void)
{
  fprintf(stderr,"Syntax:  lsstrftime [-c|-a|-m] strftime-format-string filename(s)\n\n");
  fprintf(stderr,"List directory contents with strftime(3) formatted timestamps.\n\n");
  fprintf(stderr,"Use %%f to tell lsstrftime where to put the filename (only once!),\n");
  fprintf(stderr,"see \"man strftime\" for the time and date formats.\n");
  fprintf(stderr,"Use -c|-a|-m to get the creation/access/modification time (default: -m).\n\n");
  fprintf(stderr,"Returns 0 on success, errno(3) on error (if you specify more than one file,\n");
  fprintf(stderr,"the last file determines the return code).\n\n");
  fprintf(stderr,"Example:\n\t\tlsstrftime \"%%c %%f\" *\n\n");
  return EXIT_FAILURE;
}


int ShowTimeStamp(char camFlag, char* Format,char* Filename)
{
  struct stat Buffer;
  time_t Time;
  char s[1024],*p;

  if(-1==stat(Filename,&Buffer))
  {
    perror(Filename);
    return errno;
  }
  switch(camFlag)
  {
    case 'c':  Time = Buffer.st_ctime; break;
    case 'a':  Time = Buffer.st_atime; break;
    default:   Time = Buffer.st_mtime; break;
  }

  if(!strftime(s, sizeof(s),Format,localtime(&Time)))
    strcpy(s,"???");
  strcat(s,"\n");

  if((p = strstr(s,"%f")))
  {
    p[1] = 's';
    if(strstr(s,"%f"))
    {
      fprintf(stderr,"lsstrftime: only one %%f allowed.\n");
      exit(EXIT_FAILURE);
    }

    printf(s,Filename);
  }
  else
    printf("%s",s);
  return 0;
}


int main(int argc, char* argv[])
{
  int FirstNonOption = 1;
  char camFlag = 0;
  int i = 0, r = 0;

  if(argc<3)
    exit(Usage());

  if('-'==argv[1][0])
  {
    ++FirstNonOption;
    camFlag = argv[1][1];
    if(!strchr("cam",camFlag))
      camFlag = 0;
  }

  for(i = FirstNonOption+1; i<argc; i++)
    r = ShowTimeStamp(camFlag,argv[FirstNonOption],argv[i]);

  return r;
}


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Can ls show seconds?
  2001-04-09  0:07 Can ls show seconds? The Juggernaut
  2001-04-09 14:56 ` Bob Harris
  2001-04-09 18:20 ` Michael Mauch
@ 2001-04-11 13:19 ` larry.gensch
  2001-04-12  4:40 ` Mark Garrett
  3 siblings, 0 replies; 5+ messages in thread
From: larry.gensch @ 2001-04-11 13:19 UTC (permalink / raw)


In article <gvu1dt8o9qh5fep01imins1kq0o7j3j5i6@4ax.com>,
	The Juggernaut <never@mind.com> writes:

> I've been trying to find a way to use ls to show the file creation
> time in seconds.  It mentions reading LC_TIME, but can this be changed
> to force ls to show seconds?  Are the seconds even stored?

Try the ELS (Enhanced LS) utility at:

    ftp://perseus.elen.utah.edu/pub/markb

It has even more options than ls (believe it or not), and unlike ls,
its time format is constant within a single run (eg, 'ls' will
sometimes give you the year that a file was created, but not if it was
created "recently").

I've found this particular little utility very useful, especially
within scripts and aliases.

-- 
Larry Gensch					   Larry.Gensch@compaq.com
Compaq Computer Corporation                        As usual, the standard
110 Spit Brook Road ZKO3-2/W17                     disclaimers apply, etc.
Nashua, NH 03062                                   Yada yada yada


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Can ls show seconds?
  2001-04-09  0:07 Can ls show seconds? The Juggernaut
                   ` (2 preceding siblings ...)
  2001-04-11 13:19 ` larry.gensch
@ 2001-04-12  4:40 ` Mark Garrett
  3 siblings, 0 replies; 5+ messages in thread
From: Mark Garrett @ 2001-04-12  4:40 UTC (permalink / raw)


in article gvu1dt8o9qh5fep01imins1kq0o7j3j5i6@4ax.com, The Juggernaut at
never@mind.com wrote on 09/04/2001 10:07:

> I've been trying to find a way to use ls to show the file creation
> time in seconds.  It mentions reading LC_TIME, but can this be changed
> to force ls to show seconds?  Are the seconds even stored?

To ask the obvious question, why do  you want to do this?



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2001-04-12  4:40 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2001-04-09  0:07 Can ls show seconds? The Juggernaut
2001-04-09 14:56 ` Bob Harris
2001-04-09 18:20 ` Michael Mauch
2001-04-11 13:19 ` larry.gensch
2001-04-12  4:40 ` Mark Garrett

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox