Usenet comp.unix.tru64 (2000-2013)
 help / color / mirror / Atom feed
From: michael.mauch@gmx.de (Michael Mauch)
Subject: Re: Can ls show seconds?
Date: Mon, 9 Apr 2001 20:20:41 +0200	[thread overview]
Message-ID: <phusa9.qp.ln@elmicha.333200002251-0001.dialin.t-online.de> (raw)
In-Reply-To: gvu1dt8o9qh5fep01imins1kq0o7j3j5i6@4ax.com

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;
}


  parent reply	other threads:[~2001-04-09 18:20 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2001-04-11 13:19 ` larry.gensch
2001-04-12  4:40 ` Mark Garrett
read-only archive

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