1999-08-16 21:50:52 +00:00
|
|
|
/*
|
2000-01-12 06:07:57 +00:00
|
|
|
**********************************************************************
|
|
|
|
* Copyright (C) 1998-2000, International Business Machines
|
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
**********************************************************************
|
1999-08-16 21:50:52 +00:00
|
|
|
*
|
|
|
|
* File date.c
|
|
|
|
*
|
|
|
|
* Modification History:
|
|
|
|
*
|
|
|
|
* Date Name Description
|
|
|
|
* 06/11/99 stephen Creation.
|
|
|
|
* 06/16/99 stephen Modified to use uprint.
|
|
|
|
*******************************************************************************
|
|
|
|
*/
|
|
|
|
|
2000-08-30 17:35:00 +00:00
|
|
|
#include <stdlib.h>
|
1999-08-16 21:50:52 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
1999-12-28 23:57:50 +00:00
|
|
|
#include "unicode/utypes.h"
|
|
|
|
#include "unicode/ustring.h"
|
2001-12-05 17:54:57 +00:00
|
|
|
#include "unicode/uclean.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
|
1999-12-28 23:57:50 +00:00
|
|
|
#include "unicode/ucnv.h"
|
|
|
|
#include "unicode/udat.h"
|
|
|
|
#include "unicode/ucal.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
#include "uprint.h"
|
|
|
|
|
2002-10-02 17:13:32 +00:00
|
|
|
int main(int argc, char **argv);
|
|
|
|
|
|
|
|
#if UCONFIG_NO_FORMATTING
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
printf("%s: Sorry, UCONFIG_NO_FORMATTING was turned on (see uconfig.h). No formatting can be done. \n", argv[0]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
/* Protos */
|
2001-02-05 19:14:50 +00:00
|
|
|
static void usage(void);
|
|
|
|
static void version(void);
|
1999-08-16 21:50:52 +00:00
|
|
|
static void date(const UChar *tz, UDateFormatStyle style, UErrorCode *status);
|
|
|
|
|
|
|
|
|
|
|
|
/* The version of date */
|
|
|
|
#define DATE_VERSION "1.0"
|
|
|
|
|
|
|
|
/* "GMT" */
|
|
|
|
static const UChar GMT_ID [] = { 0x0047, 0x004d, 0x0054, 0x0000 };
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc,
|
|
|
|
char **argv)
|
|
|
|
{
|
|
|
|
int printUsage = 0;
|
|
|
|
int printVersion = 0;
|
|
|
|
int optind = 1;
|
|
|
|
char *arg;
|
|
|
|
const UChar *tz = 0;
|
|
|
|
UDateFormatStyle style = UDAT_DEFAULT;
|
1999-10-07 00:07:53 +00:00
|
|
|
UErrorCode status = U_ZERO_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* parse the options */
|
|
|
|
for(optind = 1; optind < argc; ++optind) {
|
|
|
|
arg = argv[optind];
|
|
|
|
|
|
|
|
/* version info */
|
|
|
|
if(strcmp(arg, "-v") == 0 || strcmp(arg, "--version") == 0) {
|
|
|
|
printVersion = 1;
|
|
|
|
}
|
|
|
|
/* usage info */
|
|
|
|
else if(strcmp(arg, "-h") == 0 || strcmp(arg, "--help") == 0) {
|
|
|
|
printUsage = 1;
|
|
|
|
}
|
|
|
|
/* display date in gmt */
|
|
|
|
else if(strcmp(arg, "-u") == 0 || strcmp(arg, "--gmt") == 0) {
|
|
|
|
tz = GMT_ID;
|
|
|
|
}
|
|
|
|
/* display date in gmt */
|
|
|
|
else if(strcmp(arg, "-f") == 0 || strcmp(arg, "--full") == 0) {
|
|
|
|
style = UDAT_FULL;
|
|
|
|
}
|
|
|
|
/* display date in long format */
|
|
|
|
else if(strcmp(arg, "-l") == 0 || strcmp(arg, "--long") == 0) {
|
|
|
|
style = UDAT_LONG;
|
|
|
|
}
|
|
|
|
/* display date in medium format */
|
|
|
|
else if(strcmp(arg, "-m") == 0 || strcmp(arg, "--medium") == 0) {
|
|
|
|
style = UDAT_MEDIUM;
|
|
|
|
}
|
|
|
|
/* display date in short format */
|
|
|
|
else if(strcmp(arg, "-s") == 0 || strcmp(arg, "--short") == 0) {
|
|
|
|
style = UDAT_SHORT;
|
|
|
|
}
|
|
|
|
/* POSIX.1 says all arguments after -- are not options */
|
|
|
|
else if(strcmp(arg, "--") == 0) {
|
|
|
|
/* skip the -- */
|
|
|
|
++optind;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* unrecognized option */
|
|
|
|
else if(strncmp(arg, "-", strlen("-")) == 0) {
|
2002-04-15 23:32:30 +00:00
|
|
|
printf("icudate: invalid option -- %s\n", arg+1);
|
1999-08-16 21:50:52 +00:00
|
|
|
printUsage = 1;
|
|
|
|
}
|
|
|
|
/* done with options, display date */
|
|
|
|
else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* print usage info */
|
|
|
|
if(printUsage) {
|
|
|
|
usage();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* print version info */
|
|
|
|
if(printVersion) {
|
|
|
|
version();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* print the date */
|
|
|
|
date(tz, style, &status);
|
|
|
|
|
2001-12-05 17:54:57 +00:00
|
|
|
u_cleanup();
|
1999-10-18 22:48:32 +00:00
|
|
|
return (U_FAILURE(status) ? 1 : 0);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Usage information */
|
|
|
|
static void
|
|
|
|
usage()
|
|
|
|
{
|
2002-04-15 23:32:30 +00:00
|
|
|
puts("Usage: icudate [OPTIONS]");
|
1999-08-16 21:50:52 +00:00
|
|
|
puts("Options:");
|
|
|
|
puts(" -h, --help Print this message and exit.");
|
|
|
|
puts(" -v, --version Print the version number of date and exit.");
|
|
|
|
puts(" -u, --gmt Display the date in Greenwich Mean Time.");
|
|
|
|
puts(" -f, --full Use full display format.");
|
|
|
|
puts(" -l, --long Use long display format.");
|
|
|
|
puts(" -m, --medium Use medium display format.");
|
|
|
|
puts(" -s, --short Use short display format.");
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Version information */
|
|
|
|
static void
|
|
|
|
version()
|
|
|
|
{
|
2002-04-15 23:32:30 +00:00
|
|
|
printf("icudate version %s (ICU version %s), created by Stephen F. Booth.\n",
|
2000-01-12 06:07:57 +00:00
|
|
|
DATE_VERSION, U_ICU_VERSION);
|
2002-04-15 23:32:30 +00:00
|
|
|
puts(U_COPYRIGHT_STRING);
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Format the date */
|
|
|
|
static void
|
|
|
|
date(const UChar *tz,
|
|
|
|
UDateFormatStyle style,
|
|
|
|
UErrorCode *status)
|
|
|
|
{
|
|
|
|
UChar *s = 0;
|
|
|
|
int32_t len = 0;
|
|
|
|
UDateFormat *fmt;
|
|
|
|
|
2001-08-17 20:31:51 +00:00
|
|
|
fmt = udat_open(style, style, 0, tz, -1,NULL,0, status);
|
1999-08-16 21:50:52 +00:00
|
|
|
len = udat_format(fmt, ucal_getNow(), 0, len, 0, status);
|
1999-10-07 00:07:53 +00:00
|
|
|
if(*status == U_BUFFER_OVERFLOW_ERROR) {
|
|
|
|
*status = U_ZERO_ERROR;
|
1999-08-16 21:50:52 +00:00
|
|
|
s = (UChar*) malloc(sizeof(UChar) * (len+1));
|
|
|
|
if(s == 0) goto finish;
|
|
|
|
udat_format(fmt, ucal_getNow(), s, len + 1, 0, status);
|
1999-10-18 22:48:32 +00:00
|
|
|
if(U_FAILURE(*status)) goto finish;
|
1999-08-16 21:50:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* print the date string */
|
|
|
|
uprint(s, stdout, status);
|
|
|
|
|
|
|
|
/* print a trailing newline */
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
finish:
|
|
|
|
udat_close(fmt);
|
|
|
|
free(s);
|
|
|
|
}
|
2002-10-02 17:13:32 +00:00
|
|
|
#endif
|