Article View: aus.computers.java
Article #417Formatted date/time via environment variable?
From: Doug Rickard
Date: Mon, 09 Oct 2000 00:00
Date: Mon, 09 Oct 2000 00:00
90 lines
3776 bytes
3776 bytes
We have a range of software which is used in many countries around the
world. It has to be able to cope with outputting many different time
formats. Each individual user may set their own specific date/time format.
As the following C example will show, we use an environment variable
DATE_TIME_FORMAT to store a string which defines the actual output format.
This works well on many different operating systems, e.g. VMS, Unix, Linux,
MS-DOS, Win95/98, Win2000, WinME, WinNT, etc, and has served us well for
many years. Basically it uses the C functions "getenv" and "strftime" and
really is only a few lines long.
Now we are trying to do the same thing in Java. However, try as I might, I
cannot find a Java class that will return the contents of an environment
variable, or a class that will return a formatted date/time string given a
simple format definition string.
Can anyone suggest a way in which we can elegantly return a formatted
date/time string in Java using our existing environment variable?
Doug.
/*
* datetime.c
*
* Returns current date/time in a format defined by
* environment variable DATE_TIME_FORMAT. Note that
* normally upper case is required so that the system
* works on all operating systems, however this program
* checks for lower case as well.
*
* If DATE_TIME_FORMAT is defined as "%A %d-%B-%Y %H:%M"
* we get an output "Tuesday 25-July-2000 23:11". Look
* up 'strftime' for guidance on arguments.
*
* In the absence of any environment variable, return the
* time in ISO8601 standard format. Do not return the USA
* centric standard C format.
*
* DOS > SET DATE_TIME_FORMAT=%%A %%d-%%B-%%Y %%H:%%M
* Linux> DATE_TIME_FORMAT="%A %d-%B-%Y %H:%M" (Bash shell)
*
* Windows95/98 DOS :-
* Borland CPP V5.5
* >bcc32 -odatetime.exe datetime.c
* Microsoft C V6.00A
* >cl datetime.c
*
* Linux/Intel :-
* $ gcc -o datetime datetime.c
*
* Doug Rickard 25-July-2000
*/
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
struct tm *tm_struct;
time_t now;
char *environment_string;
char default_ISO8601_string[] = "%A %Y-%m-%d %H:%M";
char formatted_string[64];
void main() {
/*printf("Get the environment variable address.\n");*/
if ((environment_string = getenv("DATE_TIME_FORMAT")) == NULL ) {
if ((environment_string = getenv("date_time_format")) == NULL )
environment_string = default_ISO8601_string;
}
}
/*printf("Print out the environment variable.\n");*/
/*printf("Environment variable is - %s\n", environment_string);*/
/*printf("Get current time.\n");*/
time(&now);
/*printf("Convert time into tm structure.\n");*/
tm_struct = localtime(&now);
/*printf("Format the time according to environment variable
DATE_TIME_FORMAT.\n");*/
strftime(formatted_string, sizeof(formatted_string),environment_string,
tm_struct);
printf("Current date/time is - %s\n", formatted_string);
}
Technical Details
Message-ID:
<39e107db@news.iprimus.com.au>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 10d6db,e9a2d875ce6c6a1d,start X-Google-Attributes: gid10d6db,public From: "Doug Rickard" <rickard@primus.com.au> Subject: Formatted date/time via environment variable? Date: 2000/10/09 Message-ID: <39e107db@news.iprimus.com.au> X-Deja-AN: 679116682 X-Original-NNTP-Posting-Host: 203.134.47.186 X-Priority: 3 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300 X-Trace: news0.optus.net.au 971048924 203.134.64.67 (Mon, 09 Oct 2000 10:48:44 EST) Organization: Another Optus Customer X-Original-Trace: 9 Oct 2000 10:48:43 +1100, 203.134.47.186 X-MSMail-Priority: Normal NNTP-Posting-Date: Mon, 09 Oct 2000 10:48:44 EST Newsgroups: aus.computers.java