Porting a DOS function which gets the Systemtime (written in C) to Linux

Porting a DOS function which gets the Systemtime (written in C) to Linux

Post by Mo » Mon, 17 Aug 1998 04:00:00



Hi!

Probably this problem is easy to solve. Unfortunetately I couldn't
solve it.

Background:
I need to get the Systemtime actually part of it into three variables
with the month, year and day value in order to save them into the
header of a database file so I know when the file was "last accessed".
For all that entire database I found in the public library a few
examples. These were though written for Borland and M$ Compilers which
run under DOS. I though only have my gnu Linux compiler and intent to
write that program for Linux.
So the headerfile "dos.h" does not exsit for me and also not the
functions used from that headerfile.

The actual Problem:
This is just part of the example of one of the books, I need to get
the same result but with a function of the C-library of Linux, I just
type now the entire example which runs for M$ and Borland

What I need  is a struct like the date struct or the dosdate_t struct
in a headerfile and also a function which sets the actual date then to
my created object of one of these structs like it works in the
example.
Please don't just tell me to look in the man pages or "try ctime". I
have looked up some stuff in the manpages and also in to "time.h".
What I tried did not work, brought me coredumps or is not what I need
(like getting the time since 1970 in miliseconds or so). Please try to
help me with a short example.  Thanks in advance,

Moh.

PS: Please answer me also via email, as I might not found this again.
Here is the Example:

......
int Now[3];

#ifdef TURBO_C_1_0
        struct date ToDay;
        getdate(&ToDay);
        Now[0]=ToDay.da_year-1900;
        Now[1]=ToDay.da_mon;
        Now[2]=ToDay.da_day;
#endif
#ifdef MS_C_5_1
        struct dosdate_t ToDay;
        _dos_getdate(&ToDay);
        Now[0]=ToDay.year-1900;
        Now[1]=ToDay.mon;
        Now[2]=ToDay.day;
#endif
......

 
 
 

Porting a DOS function which gets the Systemtime (written in C) to Linux

Post by Robert Wues » Mon, 17 Aug 1998 04:00:00



> Hi!

> Probably this problem is easy to solve. Unfortunetately I couldn't
> solve it.

> Background:
> I need to get the Systemtime actually part of it into three variables
> with the month, year and day value in order to save them into the
> header of a database file so I know when the file was "last accessed".

use stat(2) - it will give you three times, including last access
(assuming you don't do this with the noatime patch applied which
specifically defeats this for high performance purposes)

The times are of type time_t.

Quote:> For all that entire database I found in the public library a few
> examples. These were though written for Borland and M$ Compilers which
> run under DOS. I though only have my gnu Linux compiler and intent to
> write that program for Linux.
> So the headerfile "dos.h" does not exsit for me and also not the
> functions used from that headerfile.

> The actual Problem:
> This is just part of the example of one of the books, I need to get
> the same result but with a function of the C-library of Linux, I just
> type now the entire example which runs for M$ and Borland

> What I need  is a struct like the date struct or the dosdate_t struct
> in a headerfile and also a function which sets the actual date then to
> my created object of one of these structs like it works in the
> example.
> Please don't just tell me to look in the man pages or "try ctime". I

Well, I wouldn't ell you to use ctime 'cause it doesn't do what (I
think) you want.

- Show quoted text -

Quote:> have looked up some stuff in the manpages and also in to "time.h".
> What I tried did not work, brought me coredumps or is not what I need
> (like getting the time since 1970 in miliseconds or so). Please try to
> help me with a short example.  Thanks in advance,

> Moh.

> PS: Please answer me also via email, as I might not found this again.
> Here is the Example:

> ......
> int Now[3];

> #ifdef TURBO_C_1_0
>         struct date ToDay;
>         getdate(&ToDay);
>         Now[0]=ToDay.da_year-1900;
>         Now[1]=ToDay.da_mon;
>         Now[2]=ToDay.da_day;
> #endif
> #ifdef MS_C_5_1
>         struct dosdate_t ToDay;
>         _dos_getdate(&ToDay);
>         Now[0]=ToDay.year-1900;
>         Now[1]=ToDay.mon;
>         Now[2]=ToDay.day;
> #endif
> ......

Ok, here's a shot at the code (no guarantees, but if it core dumps, let
me know and I'll test before posting :-).  I don't really like the code
above because it is not Y2K compliant :-(.  How about putting the three
values in an int array, 0 is the year, 1 is the month and 2 is the day.
(BTW, I know you don't want to hear this, BUT: see man 2 stat and man 3
localtime)

#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
#include <errno.h>

int **get_last_access_date( char *filename )
{
        static int Now[3];

        struct stat sbuf;
        struct tm *ptm;

        if( stat(filename, &sbuf ) != 0 ) {
                fprintf( stderr, "stat failed\n");
                exit( errno );
        }
        ptm = localtime( &sbuf.st_atime );
        Now[0] = ptm->tm_year;
        Now[1] = ptm->tm_mon;
        Now[2] = ptm->tm_mday;

        return Now;

Quote:}

--
Robert Wuest, PE             Empowered
Sirius Engineering Company          by

Digital        - The art of counting on your fingers

 
 
 

Porting a DOS function which gets the Systemtime (written in C) to Linux

Post by Robert Wues » Mon, 17 Aug 1998 04:00:00



> int **get_last_access_date( char *filename )
> {
>         static int Now[3];

Oops, that should return (int *)

--
Robert Wuest, PE             Empowered
Sirius Engineering Company          by

Digital        - The art of counting on your fingers

 
 
 

Porting a DOS function which gets the Systemtime (written in C) to Linux

Post by Jim Peterso » Mon, 17 Aug 1998 04:00:00


Hi,

  The program appended to the end of this message, when compiled and
  run, prints out the following for me:

7/16/98 23:09:13

  Hope this helps,

--Jim
(more info. on struct tm can be found on the man page for localtime).

=======================================================================
#include <time.h>
#include <stdio.h>

int main()
{
  time_t the_time;
  struct tm *x;

  time(&the_time);
  x=localtime(&the_time);

  printf("%d/%d/%d %d:%02d:%02d\n",
         x->tm_mon,x->tm_mday,x->tm_year,
         x->tm_hour,x->tm_min,x->tm_sec);
  return 0;

Quote:}

 
 
 

Porting a DOS function which gets the Systemtime (written in C) to Linux

Post by Jon Plew » Tue, 18 Aug 1998 04:00:00




Quote:> Hi!

> Probably this problem is easy to solve. Unfortunetately I couldn't
> solve it.

[ snip ]

> ......
> int Now[3];

> #ifdef TURBO_C_1_0
>    struct date ToDay;
>    getdate(&ToDay);
>    Now[0]=ToDay.da_year-1900;
>    Now[1]=ToDay.da_mon;
>    Now[2]=ToDay.da_day;
> #endif
> #ifdef MS_C_5_1
>    struct dosdate_t ToDay;
>    _dos_getdate(&ToDay);
>    Now[0]=ToDay.year-1900;
>    Now[1]=ToDay.mon;
>    Now[2]=ToDay.day;
> #endif
> ......

----
# include <time.h>
...
time_t t;
struct tm *tm;

        time(&t);
        tm = localtime(&t);
        Now[0] = tm->tm_year - 1900;
        Now[1] = tm->tm_mon;
        Now[2] = tm->tm_mday;
...
----

This assumes DOS scales month and day from 0.

Jon Plews.

 
 
 

Porting a DOS function which gets the Systemtime (written in C) to Linux

Post by David Wils » Wed, 19 Aug 1998 04:00:00



>Hi,
>  The program appended to the end of this message, when compiled and
>  run, prints out the following for me:
>7/16/98 23:09:13
>  Hope this helps,
>--Jim
>(more info. on struct tm can be found on the man page for localtime).
>=======================================================================
>#include <time.h>
>#include <stdio.h>
>int main()
>{
>  time_t the_time;
>  struct tm *x;
>  time(&the_time);
>  x=localtime(&the_time);
>  printf("%d/%d/%d %d:%02d:%02d\n",
>         x->tm_mon,x->tm_mday,x->tm_year,
>         x->tm_hour,x->tm_min,x->tm_sec);
>  return 0;
>}

Given that you ran your code this month, it should have printed 8/16/98 23:09:13

You need to add 1 to the value of x->tm_mon as it is the # of months since Jan.
--

 
 
 

Porting a DOS function which gets the Systemtime (written in C) to Linux

Post by Greg Frank » Tue, 25 Aug 1998 04:00:00



> Hi!

> Probably this problem is easy to solve. Unfortunetately I couldn't
> solve it.

While you wait for an answer, look up the book by the famous authour
Manual....

Quote:> Background:
> I need to get the Systemtime actually part of it into three variables
> with the month, year and day value in order to save them into the

man time
man localtime
man strftime

--

 _`\<,_    Systems Engineering, Carleton University,   |O\   -^\<;^\<,
(*)/ (*)       Ottawa, Ontario, Canada  K1S 5B6.       (*)--(*)%---/(*)
          "Where do you want to go today?"   Outside.

 
 
 

Porting a DOS function which gets the Systemtime (written in C) to Linux

Post by Greg Frank » Tue, 25 Aug 1998 04:00:00



>   time(&the_time);
>   x=localtime(&the_time);

>   printf("%d/%d/%d %d:%02d:%02d\n",
>          x->tm_mon,x->tm_mday,x->tm_year,
>          x->tm_hour,x->tm_min,x->tm_sec);

strftime(3) is a better choice, as in:

    strftime( buf, bufsize, "%D %T" );
    printf( buf );

Strftime takes care of a lot of horrible details.
--

 _`\<,_    Systems Engineering, Carleton University,   |O\   -^\<;^\<,
(*)/ (*)       Ottawa, Ontario, Canada  K1S 5B6.       (*)--(*)%---/(*)
          "Where do you want to go today?"   Outside.