* getdate(3) - format date
@ 2006-09-21 20:36 HIToC
2006-09-22 7:28 ` Steve Graegert
0 siblings, 1 reply; 6+ messages in thread
From: HIToC @ 2006-09-21 20:36 UTC (permalink / raw)
To: linux-c-programming
Hello list,
I am using the getdate(3) function to convert a string date in its tm structure, but
I have tried several formats of string-dates and it always returns a NULL pointer.
All this dates I suppose invalid for the getdate(3):
Fri, 19 Nov 82 16:14:55 EST
Wed, 20 Sep 2006 15:03:53 GMT
Thu, 21 Sep 2006 08:59:25 +0400
19 Sep 2006 15:52:25 -0700
19 Sep 2006 15:52:25 EST
Do you know what are the correct string formats for this function and if under
Unix there are routines to read ascii string dates such in mail text messages
or USENET articles?
Thank you in advance
HIToC
--
With regards,
HIToC
hitoc_mail@yahoo.it
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: getdate(3) - format date
2006-09-21 20:36 getdate(3) - format date HIToC
@ 2006-09-22 7:28 ` Steve Graegert
2006-09-22 20:40 ` HIToC
0 siblings, 1 reply; 6+ messages in thread
From: Steve Graegert @ 2006-09-22 7:28 UTC (permalink / raw)
To: linux-c-programming
On 9/21/06, HIToC <hitoc_mail@yahoo.it> wrote:
> Hello list,
> I am using the getdate(3) function to convert a string date in its tm structure, but
> I have tried several formats of string-dates and it always returns a NULL pointer.
>
> All this dates I suppose invalid for the getdate(3):
> Fri, 19 Nov 82 16:14:55 EST
> Wed, 20 Sep 2006 15:03:53 GMT
> Thu, 21 Sep 2006 08:59:25 +0400
> 19 Sep 2006 15:52:25 -0700
> 19 Sep 2006 15:52:25 EST
Sure, they are valid, but getdate(3) requires a template file to be
present, with each line in the file representing a date format to
parse. From getdate(3):
User-supplied templates are used to parse and interpret the
input string. The templates are text files created by the
user and identified via the environment variable DATEMSK.
Each line in the template represents an acceptable date
and/or time specification using conversion specifications
similar to those used by strftime(3) and strptime(3).
Consider the following example which illustrates the usage of getdate(3)
--- BEGIN script ---
#!/bin/sh
#
# create template file
#
cat >.date <<EOF
%m
%A %B %d, %Y, %H:%M:%S
%A
%B
%m/%d/%y %I %p
%d, %m, %Y %H:%M
at %A the %dst of %B in %Y
run job at %I %p, %B %dnd
&A den %d. %B %Y %H.%M Uhr
EOF
DATEMSK=.date
export DATEMSK
--- END script ---
--- BEGIN C Source ---
#include <stdio.h>
#include <time.h>
#define BUF 512
void daterr(int err) {
switch(err) {
case 1: printf("The DATEMSK environment variable is null or
undefined.\n");
break;
case 2: printf("The template file cannot be opened for reading.\n");
break;
case 3: printf("Failed to get file status information.\n");
break;
case 4: printf("The template file is not a regular file.\n");
break;
case 5: printf("An error is encountered while reading the
template file.\n");
break;
case 6: printf("Memory allocation failed (not enough memory
available.\n");
break;
case 7: printf("There is no line in the template that matches
the input.\n");
break;
case 8: printf("Invalid input specification\n");
break;
default: printf("unknown\n");
}
exit(1);
}
int main(void) {
struct tm *tm;
char buf[BUF];
tm = getdate("09/22/06");
if (getdate_err != 0)
daterr(getdate_err);
strftime(buf,BUF,"%a %Y %H:%M:%S\n",tm);
printf("%s",buf);
return 1;
}
--- END C Source ---
\Steve
--
Steve Grägert <steve@graegert.com>
Jabber xmpp://graegerts@jabber.org
Internet http://eth0.graegert.com, http://blog.graegert.com
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: getdate(3) - format date
2006-09-22 7:28 ` Steve Graegert
@ 2006-09-22 20:40 ` HIToC
2006-09-25 8:53 ` Henry Margies
0 siblings, 1 reply; 6+ messages in thread
From: HIToC @ 2006-09-22 20:40 UTC (permalink / raw)
To: Steve Graegert; +Cc: linux-c-programming
Yes Steve!
The problem was the absence of both DATEMSK and the template file becouse
the getdate_err was setted to 1.
Now I have tried your script but it does not exports the DATEMSK to the
environment: if next the execution of the script I type
echo $DATEMSK
it prints nothing.
If I execute the C program from the script it runs but the getdate_err said that there
is no line in the template that matches the input [error 7].
I believe that the getdate solution is very unportable.
Thanks anyway!
On Friday 22 September 2006 09:28, Steve Graegert wrote:
> On 9/21/06, HIToC <hitoc_mail@yahoo.it> wrote:
> > Hello list,
> > I am using the getdate(3) function to convert a string date in its tm
> > structure, but I have tried several formats of string-dates and it always
> > returns a NULL pointer.
> >
> > All this dates I suppose invalid for the getdate(3):
> > Fri, 19 Nov 82 16:14:55 EST
> > Wed, 20 Sep 2006 15:03:53 GMT
> > Thu, 21 Sep 2006 08:59:25 +0400
> > 19 Sep 2006 15:52:25 -0700
> > 19 Sep 2006 15:52:25 EST
>
> Sure, they are valid, but getdate(3) requires a template file to be
> present, with each line in the file representing a date format to
> parse. From getdate(3):
>
> User-supplied templates are used to parse and interpret the
> input string. The templates are text files created by the
> user and identified via the environment variable DATEMSK.
> Each line in the template represents an acceptable date
> and/or time specification using conversion specifications
> similar to those used by strftime(3) and strptime(3).
>
> Consider the following example which illustrates the usage of getdate(3)
>
> --- BEGIN script ---
>
> #!/bin/sh
> #
> # create template file
> #
> cat >.date <<EOF
> %m
> %A %B %d, %Y, %H:%M:%S
> %A
> %B
> %m/%d/%y %I %p
> %d, %m, %Y %H:%M
> at %A the %dst of %B in %Y
> run job at %I %p, %B %dnd
> &A den %d. %B %Y %H.%M Uhr
> EOF
> DATEMSK=.date
> export DATEMSK
>
> --- END script ---
>
> --- BEGIN C Source ---
>
> #include <stdio.h>
> #include <time.h>
>
> #define BUF 512
>
> void daterr(int err) {
> switch(err) {
> case 1: printf("The DATEMSK environment variable is null or
> undefined.\n");
> break;
>
> case 2: printf("The template file cannot be opened for
> reading.\n"); break;
>
> case 3: printf("Failed to get file status information.\n");
> break;
>
> case 4: printf("The template file is not a regular file.\n");
> break;
>
> case 5: printf("An error is encountered while reading the
> template file.\n");
> break;
>
> case 6: printf("Memory allocation failed (not enough memory
> available.\n");
> break;
>
> case 7: printf("There is no line in the template that matches
> the input.\n");
> break;
>
> case 8: printf("Invalid input specification\n");
> break;
>
> default: printf("unknown\n");
> }
>
> exit(1);
> }
>
> int main(void) {
> struct tm *tm;
> char buf[BUF];
>
> tm = getdate("09/22/06");
>
> if (getdate_err != 0)
> daterr(getdate_err);
>
> strftime(buf,BUF,"%a %Y %H:%M:%S\n",tm);
> printf("%s",buf);
>
> return 1;
> }
>
> --- END C Source ---
>
> \Steve
--
With regards,
HIToC
hitoc_mail@yahoo.it
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: getdate(3) - format date
2006-09-22 20:40 ` HIToC
@ 2006-09-25 8:53 ` Henry Margies
2006-09-26 17:27 ` HIToC
0 siblings, 1 reply; 6+ messages in thread
From: Henry Margies @ 2006-09-25 8:53 UTC (permalink / raw)
To: linux-c-programming
Hello HIToc,
On Fri, 2006-09-22 at 22:40 +0200, HIToC wrote:
> Now I have tried your script but it does not exports the DATEMSK to the
> environment: if next the execution of the script I type
> echo $DATEMSK it prints nothing.
How did you execute the script? In order to have the variable exported,
you have to source this shell script. Try something like this:
. ./script
or
source ./script
Best regards,
Henry
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: getdate(3) - format date
2006-09-25 8:53 ` Henry Margies
@ 2006-09-26 17:27 ` HIToC
2006-09-28 7:38 ` Henry Margies
0 siblings, 1 reply; 6+ messages in thread
From: HIToC @ 2006-09-26 17:27 UTC (permalink / raw)
To: Henry Margies; +Cc: linux-c-programming
Hello Henry,
I have used "sh": sh script
But using ./script has the same problem: DATEMSK is not exported.
Thank you for the emails
HIToC
On Monday 25 September 2006 10:53, Henry Margies wrote:
> Hello HIToc,
>
> On Fri, 2006-09-22 at 22:40 +0200, HIToC wrote:
> > Now I have tried your script but it does not exports the DATEMSK to the
> > environment: if next the execution of the script I type
> > echo $DATEMSK it prints nothing.
>
> How did you execute the script? In order to have the variable exported,
> you have to source this shell script. Try something like this:
>
> . ./script
>
> or
>
> source ./script
>
>
>
> Best regards,
>
> Henry
>
>
> -
> To unsubscribe from this list: send the line "unsubscribe
> linux-c-programming" in the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
--
With regards,
HIToC
hitoc_mail@yahoo.it
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: getdate(3) - format date
2006-09-26 17:27 ` HIToC
@ 2006-09-28 7:38 ` Henry Margies
0 siblings, 0 replies; 6+ messages in thread
From: Henry Margies @ 2006-09-28 7:38 UTC (permalink / raw)
To: linux-c-programming
On Tue, 2006-09-26 at 19:27 +0200, HIToC wrote:
> Hello Henry,
>
> I have used "sh": sh script
> But using ./script has the same problem: DATEMSK is not exported.
>
> Thank you for the emails
> HIToC
>
If you execute a script via sh script.sh or ./script.sh it is executed
in a sub shell. So every exported variable is only set in all sub-sub
shells.
Try this script to see what I mean
#!/bin/sh
export V=Test
echo $V
Now execute it in a sub shell:
sh script.sh
echo $V
and execute it in the current shell (via sourcing it)
. ./script.sh
echo $V
or
source script.sh
echo $V
Best regards,
Henry
^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2006-09-28 7:38 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-09-21 20:36 getdate(3) - format date HIToC
2006-09-22 7:28 ` Steve Graegert
2006-09-22 20:40 ` HIToC
2006-09-25 8:53 ` Henry Margies
2006-09-26 17:27 ` HIToC
2006-09-28 7:38 ` Henry Margies
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).