-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

André,

On 10/10/2010 9:09 AM, André Warnier wrote:
> What would be really nice, is if someone wrote a quick Java equivalent
> to the perl script I submitted.

See below. There's actually more code than absolutely necessary, but
it's more straightforward this way.

> Now if you *really* insist, the modified version of the perl program,
> below, will explicitly use a couple of C functions, themselves using the
> builtin C structures to get the file's "last modified" time.
> 
> Running C in perl, scary stuff..

Are you submitting an application for an obfuscated C program, here?
Sheesh. What's wrong with a little C code?

Amazing: the C code is shorter than the Java code. *shrug*

Time.java (apologies for any re-formatting done by my emailer)
- ---------

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;

public class Time
{
    public static void main(String[] args)
        throws Exception
    {
        if(args.length < 1)
        {
            System.out.println("Usage: java Time <filename>");
            System.exit(1);
        }

        // GMT current time
        Calendar now = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
        System.out.println("Current GMT time (no DST): "
                           + format(now));

        // local current time
        now = Calendar.getInstance(); // default = local
        System.out.println("Current local time (with DST): "
                           + format(now));

        // File timestamp
        System.out.println("File [" + args[0] + "]");
        File file = new File(args[0]);

        long timestamp = file.lastModified();

        System.out.println("last modified timestamp: " + timestamp);
        System.out.println("the file was last modified "
                           + ((System.currentTimeMillis() - timestamp) /
1000)
                           + " seconds ago");
        Calendar tstamp = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
        tstamp.setTimeInMillis(file.lastModified());

        System.out.println("last modified as GMT time (no DST): "
                           + format(tstamp));

        tstamp = Calendar.getInstance(); // default=local
        tstamp.setTimeInMillis(file.lastModified());

        System.out.println("last modified as local time: "
                           + format(tstamp));
    }

    public static String format(Calendar c)
    {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd
HH:mm:ss z");
        //        format.setTimeZone(tz);
        format.setTimeZone(c.getTimeZone());

        return format.format(c.getTime());
    }
}

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

int main(int argc, char *argv[]) {
  time_t now;
  struct tm *gmt;
  struct tm *local;
  struct stat *fileinfo;
  int retval;
  char *filename;

  if(argc < 2) {
    printf("missing filename\n");
    return 1;
  }
  filename = argv[1];

  gmt   = (struct tm*)malloc(sizeof(struct tm));
  local = (struct tm*)malloc(sizeof(struct tm));

  time(&now);

  gmtime_r(&now, gmt);
  localtime_r(&now, local);

  /* note: asctime adds a newline */
  printf("System raw timestamp: %ld\n", now);
  printf("Current GMT time (no DST): %s", asctime(gmt));
  printf("Current local time (with DST): %s", asctime(local));

  fileinfo = (struct stat *)malloc(sizeof(struct stat));

  printf("File [%s]:\n", filename);

  if(stat(filename, fileinfo)) {
    perror(filename);
  } else {
    gmtime_r(&(fileinfo->st_mtime), gmt);
    localtime_r(&(fileinfo->st_mtime), local);

    printf("last modified timestamp: %ld\n", (long)fileinfo->st_mtime);
    printf("the file was last modified %ld seconds ago\n",
           (now - fileinfo->st_mtime));

    printf("last modified as GMT time (no DST): %s", asctime(gmt));
    printf("last modified as local time (with DST): %s", asctime(local));
  }
  return 0;
}

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAky0o+QACgkQ9CaO5/Lv0PBbvACeLdBAsYKcpP95KkKvJQCNhBP5
TbUAnAxjQk/yLYhSvoq/uWbcl6b6mbv2
=7yhg
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to