c++.command-line - dmcs gettime() is 2 hours off ?
- "Lars Hansen" <lars.o.hansen gmx.de> Apr 26 2003
- "Lars Hansen" <lars.o.hansen gmx.de> Apr 26 2003
- "Lars Hansen" <lars.o.hansen gmx.de> Apr 26 2003
- "Lars Hansen" <lars.o.hansen gmx.de> Apr 26 2003
- Cesar Rabak <csrabak uol.com.br> Apr 26 2003
- "Lars Hansen" <lars.o.hansen gmx.de> Apr 26 2003
- Cesar Rabak <csrabak uol.com.br> Apr 27 2003
- "Walter" <walter digitalmars.com> Apr 26 2003
- "Lars Hansen" <lars.o.hansen gmx.de> Apr 27 2003
the C code attached at the end should produce an executable which appends the
current date and time to the end of the file passed on the command-line.
But the executable created by DMC appends a time which is 2 hours less than
the actual time.
Borland Commandline Compiler 5.5 creates an execuatble which does the expected
thing correctly but crashes.
What's the reason, and how to correct this problem?
DMC version: 8.33 DMC commandline: sc %1 -mn -o+all -6
BCC version: 5.5 BCC commandline: -6 -RT- -O -P -O2 -Oc -Oi -OS -Ov -d -k-
code:
#include <stdio.h>
#include <dos.h> /* for date and time functions */
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[]) /* program appends date and time to argv[1]
and sends argv[1] to argv[2] */
{
FILE *f;
char b[17]; /* will hold the date and time text character string */
struct time t;
struct date d;
if(argc!=3) return 1;
b[16]=' ';
gettime(&t);
b[15]=t.ti_min%10+'0'; /* convert int to text character */
t.ti_min/=10;
b[14]=t.ti_min%10+'0';
b[13]=':';
b[12]=t.ti_hour%10+'0';
t.ti_hour/=10;
b[11]=t.ti_hour%10+'0';
b[10]=' ';
getdate(&d);
b[9]=d.da_year%10+'0';
d.da_year/=10;
b[8]=d.da_year%10+'0';
b[7]='.';
b[6]=d.da_mon%10+'0';
d.da_mon/=10;
b[5]=d.da_mon%10+'0';
b[4]='.';
b[3]=d.da_day%10+'0';
d.da_day/=10;
b[2]=d.da_day%10+'0';
b[1]=10; /* new lines */
b[0]=10;
f=fopen(argv[1],"a"); /* open and append */
fwrite(b,sizeof(char),17,f);
fclose(f);
strcat(argv[2]," "); /* prepare routing to editing app. */
strcat(argv[2],argv[1]);
return system(argv[2]);
}
the executables produced are in a follow-up to this post.
thanks for any help, Lars
Apr 26 2003
the exe created by BCC 5.5 with -6 -RT- -O -P -O2 -Oc -Oi -OS -Ov -d -k-
Apr 26 2003
the exe created by DMC 8.33 with sc %1 -mn -o+all -6
Apr 26 2003
Borland Commandline Compiler 5.5 creates an execuatble which does the expected thing correctly but crashes.
the crash only occurrs when the executable is launched via a file extension associated with this exe. it does not crash if it is used on the command line; now this is Borland specific; nevertheless: anyone knows a fix? thanks, Lars
Apr 26 2003
Lars Hansen escreveu:the C code attached at the end should produce an executable which appends the current date and time to the end of the file passed on the command-line.
So then, why are putting this last line in your code?return system(argv[2]); } the executables produced are in a follow-up to this post.
-- Cesar Rabak GNU/Linux User 52247. Get counted: http://counter.li.org/
Apr 26 2003
the C code attached at the end should produce an executable which appends the current date and time to the end of the file passed on the command- line.
So then, why are putting this last line in your code?return system(argv[2]); }
because after appending, the file is directed to the program given in argv[2]; this program is just an intermediate step to append the date and time before the actual editing of the file occurs.
Apr 26 2003
Lars Hansen escreveu:the C code attached at the end should produce an executable which appends the current date and time to the end of the file passed on the command- line.
So then, why are putting this last line in your code?return system(argv[2]); }
because after appending, the file is directed to the program given in argv[2]; this program is just an intermediate step to append the date and time before the actual editing of the file occurs.
gettime returning UTC instead of local time, and the need to build a string in allocated for catenating argv[2] and arvv[1], I also suggest you consider using asctime or if you need finer control strtime and/or strdate. HTH -- Cesar Rabak GNU/Linux User 52247. Get counted: http://counter.li.org/
Apr 27 2003
1) The crash is due to the strcat() into unallocated storage. 2) The hours are off because gettime() returns the time in UTC, which is probably 2 hours off from your local time zone. It should probably return local time, I'll see about fixing it. -Walter "Lars Hansen" <lars.o.hansen gmx.de> wrote in message news:b8etnl$2vfv$1 digitaldaemon.com...the C code attached at the end should produce an executable which appends
current date and time to the end of the file passed on the command-line. But the executable created by DMC appends a time which is 2 hours less
the actual time. Borland Commandline Compiler 5.5 creates an execuatble which does the
thing correctly but crashes. What's the reason, and how to correct this problem? DMC version: 8.33 DMC commandline: sc %1 -mn -o+all -6 BCC version: 5.5 BCC
code: #include <stdio.h> #include <dos.h> /* for date and time functions */ #include <stdlib.h> #include <string.h> int main(int argc, char *argv[]) /* program appends date and time to
and sends argv[1] to argv[2] */ { FILE *f; char b[17]; /* will hold the date and time text character string
struct time t; struct date d; if(argc!=3) return 1; b[16]=' '; gettime(&t); b[15]=t.ti_min%10+'0'; /* convert int to text character */ t.ti_min/=10; b[14]=t.ti_min%10+'0'; b[13]=':'; b[12]=t.ti_hour%10+'0'; t.ti_hour/=10; b[11]=t.ti_hour%10+'0'; b[10]=' '; getdate(&d); b[9]=d.da_year%10+'0'; d.da_year/=10; b[8]=d.da_year%10+'0'; b[7]='.'; b[6]=d.da_mon%10+'0'; d.da_mon/=10; b[5]=d.da_mon%10+'0'; b[4]='.'; b[3]=d.da_day%10+'0'; d.da_day/=10; b[2]=d.da_day%10+'0'; b[1]=10; /* new lines */ b[0]=10; f=fopen(argv[1],"a"); /* open and append */ fwrite(b,sizeof(char),17,f); fclose(f); strcat(argv[2]," "); /* prepare routing to editing app. */ strcat(argv[2],argv[1]); return system(argv[2]); } the executables produced are in a follow-up to this post. thanks for any help, Lars
Apr 26 2003
1) The crash is due to the strcat() into unallocated storage. 2) The hours are off because gettime() returns the time in UTC, which is probably 2 hours off from your local time zone. It should probably return local time, I'll see about fixing it.
thanks!
Apr 27 2003









"Lars Hansen" <lars.o.hansen gmx.de> 