[Solved] How to check time and date on a file

General FreeBASIC programming questions.
St_W
Posts: 1470
Joined: Feb 11, 2009 14:24
Location: Austria
Contact:

Re: How to check time and date on a file

Postby St_W » Jul 10, 2017 7:21

TeeEmCee wrote:What's wrong with using FB's builtin FileDateTime function?
Nothing as long as only the last modification timestamp is needed. In that case I'd also suggest to use FB's function.
St_W wrote:I recommend writing C code instead of FB (and linking it to your FB program) when you want to write portable code that accesses functions in Unix system headers, because the FB headers just can't be trusted.
I quite agree that there are so many conditions and FB's headers probably will be never equivalent. I guess the binary layout of that "stat" structure isn't standardized, is it? That's where a built-in C-parser in the compiler could help.
TeeEmCee
Posts: 259
Joined: Jul 22, 2006 0:54
Location: Auckland

Re: How to check time and date on a file

Postby TeeEmCee » Jul 10, 2017 9:44

Right, POSIX is an API (source code-level) standard, not an ABI (binary-level) standard. And Unices often also add additional members to POSIX structs as extensions.

Also, the ABI changes between versions of a system library (such as glibc) or the OS. For example on OSX various functions like fstat (IIRC) were replaced in 10.6 (IIRC) with different versions which better matched POSIX semantics. So the FB headers have to make the decision whether to target OS 10.4+ or OS 10.6+. Maybe at some point the old versions will be dropped, just like linux distros usually no longer include old versions of glibc needed to run programs compiled long ago.
There are also headers for non-system libraries like iconv under crt/, and they tend to break backwards-compatibility much faster!
Munair
Posts: 836
Joined: Oct 19, 2017 15:00
Location: 't Zand, NL
Contact:

Re: How to check time and date on a file

Postby Munair » Jan 04, 2018 11:28

If FileDateTime() can return the modification date, it should also be able to return the creation date, perhaps with an optional parameter.

That's a feature request. ;-)
Munair
Posts: 836
Joined: Oct 19, 2017 15:00
Location: 't Zand, NL
Contact:

Re: How to check time and date on a file

Postby Munair » Jan 04, 2018 12:00

@st_w: Your library works on Linux, but the values returned by st_atime, st_mtime and st_ctime are not correct. The first returns a non-sensical value. The other two return 1970-01-01 instead of 2018-01-04 (tested on a file created today).

Edit: Checking fb_FileDateTime.c, virtually the same code is used, so st_mtime should return the same as FileDateTime().
marcov
Posts: 2762
Joined: Jun 16, 2005 9:45
Location: Eindhoven, NL
Contact:

Re: How to check time and date on a file

Postby marcov » Jan 04, 2018 14:26

(IMHO fixing the cross platform header files is easier than keeping a multi language buildsystem working, which usually forces you to the cmdline, and deal with ugly systems like make and configure) Unless you really have 20+ targets illusions. But then your app is probably so big that it is all irrelevant anyway.
Munair
Posts: 836
Joined: Oct 19, 2017 15:00
Location: 't Zand, NL
Contact:

Re: How to check time and date on a file

Postby Munair » Jan 04, 2018 15:46

It seems I found the problem. The type definition _stat must be updated to include sub-seconds. Furthermore, in the header files only Windows is supported. Trying to include stat.bi on Linux generates an unsupported platform error.

Can someone please update the header file? The working type definition for Linux:

Code: Select all

#pragma once

extern "C"

type _stat
   st_dev as dev_t
   st_ino as ino_t
   st_mode as mode_t
   st_nlink as nlink_t
   st_uid as uid_t
   st_gid as gid_t
   st_rdev as dev_t
   st_size as off_t
   st_atime as time_t
   st_atime_usec as time_t
   st_mtime as time_t
   st_mtime_usec as time_t
   st_ctime as time_t
   st_ctime_usec as time_t
   st_blksize as uinteger ' blksize_t
   st_blocks as blkcnt_t
end type

declare function stat (byval as zstring ptr, byval as _stat ptr) as long

end extern
Munair
Posts: 836
Joined: Oct 19, 2017 15:00
Location: 't Zand, NL
Contact:

Re: How to check time and date on a file

Postby Munair » Jan 04, 2018 15:53

Here is a working example for Linux. I'm curious if it would also run on Windows.

Code: Select all

' stat.bas, by Munair <contact@basicstudio.org>
#Include "crt.bi"
#include "datetime.bi"

extern "C"
   type _stat
      st_dev as dev_t
      st_ino as ino_t
      st_mode as mode_t
      st_nlink as nlink_t
      st_uid as uid_t
      st_gid as gid_t
      st_rdev as dev_t
      st_size as off_t
      st_atime as time_t
      st_atime_usec as time_t
      st_mtime as time_t
      st_mtime_usec as time_t
      st_ctime as time_t
      st_ctime_usec as time_t
      st_blksize as uinteger 'blksize_t
      st_blocks as blkcnt_t
   end type
   declare function stat (byval as zstring ptr, byval as _stat ptr) as long
end extern

dim buf as _stat
dim tinf as tm ptr
dim serial as double
dim filename as string

filename = "stat.bas"
if stat(filename, @buf) = 0 then
   tinf = localtime(@buf.st_mtime)
   serial = DateSerial(1900 + tinf->tm_year, 1 + tinf->tm_mon, tinf->tm_mday) _
      + TimeSerial(tinf->tm_hour, tinf->tm_min, tinf->tm_sec )
   print year(serial); " "; month(serial); " "; day(serial); _
      hour(serial); " "; minute(serial); " "; second(serial)
else
   print "error"
endif
end
Gablea
Posts: 1049
Joined: Apr 06, 2010 0:05
Location: Northampton, United Kingdom
Contact:

Re: How to check time and date on a file

Postby Gablea » Feb 13, 2019 23:21

I would like to say First off Sorry for the late Replay on here I have been very busy and only just remembered to update you all

I would like to say thank-you to dodicat as his code works in Windows as well as DOS (not tried on linux yet)

how do you mark the thread as solved?
TJF
Posts: 3481
Joined: Dec 06, 2009 22:27
Location: N47°, E15°
Contact:

Re: How to check time and date on a file

Postby TJF » Feb 14, 2019 3:14

Gablea wrote:how do you mark the thread as solved?

Edit the 'subject' line in the first post.

Return to “General”

Who is online

Users browsing this forum: Bing [Bot] and 4 guests