Page 1 of 1

Get the UTC modification date of a file

Posted: Tue Aug 06, 2024 12:38 pm
by Lebostein
I found two options, but I get different results. Why?

Code: Select all

filename$ = "c:\Program Files\PureBasic\Examples\Sources\Data\Background.bmp"

Structure stats

  size.q
  mtim.q
  ctim.q

EndStructure

; ================================================

Procedure GetFileStats1(filename.s, *stats.stats)

  *stats\mtim = ConvertDate(GetFileDate(filename, #PB_Date_Modified), #PB_Date_UTC)
  *stats\ctim = ConvertDate(GetFileDate(filename, #PB_Date_Created ), #PB_Date_UTC)
  *stats\size = FileSize(filename)

EndProcedure

Define test1.stats
GetFileStats1(filename$, test1)
Debug test1\mtim

; ================================================

Structure stat64 Align #PB_Structure_AlignC

  st_dev.l    ; ID of device containing the file
  st_ino.u    ; Serial number for the file
  st_mode.u   ; Access mode and file type for the file
  st_nlink.w  ; Number of links to the file
  st_uid.w    ; User ID of file owner
  st_gid.w    ; Group ID of group owner
  st_rdev.l   ; Device ID (if the file is a character or block special device)
  st_size.q   ; File size in bytes (if the file is a regular file)
  st_atime.q  ; Time of last access
  st_mtime.q  ; Time of last data modification
  st_ctime.q  ; Time of last file status change

EndStructure

ImportC ""

  _stat64(path.p-utf8, *buf)

EndImport

Procedure GetFileStats2(filename.s, *stats.stats): Protected stat64.stat64

  _stat64(filename, stat64)
  *stats\mtim = stat64\st_mtime ; UTC-Time Modified
  *stats\ctim = stat64\st_ctime ; UTC-Time Created/Birth
  *stats\size = stat64\st_size

EndProcedure

Define test2.stats
GetFileStats2(filename$, test2)
Debug test2\mtim
1606217524
1606213924

Re: Get the UTC modification date of a file

Posted: Tue Aug 06, 2024 1:51 pm
by boddhi
I got the same dates (Win10 PB 6.11 x64):

Code: Select all

filename$ =#PB_Compiler_Home+"\Examples\Sources\Data\Background.bmp"
Structure stats
  size.q
  mtim.q
  ctim.q
EndStructure
; ================================================
Procedure GetFileStats1(filename.s, *stats.stats)
  *stats\mtim = ConvertDate(GetFileDate(filename, #PB_Date_Modified), #PB_Date_UTC)
  *stats\ctim = ConvertDate(GetFileDate(filename, #PB_Date_Created ), #PB_Date_UTC)
  *stats\size = FileSize(filename)
EndProcedure
Define test1.stats
GetFileStats1(filename$, test1)
Debug ""+test1\mtim+" => "+FormatDate("%mm/%dd/%yyyy %hh:%ii:%ss",test1\mtim)

; ================================================
Structure stat64 Align #PB_Structure_AlignC
  st_dev.l    ; ID of device containing the file
  st_ino.u    ; Serial number for the file
  st_mode.u   ; Access mode and file type for the file
  st_nlink.w  ; Number of links to the file
  st_uid.w    ; User ID of file owner
  st_gid.w    ; Group ID of group owner
  st_rdev.l   ; Device ID (if the file is a character or block special device)
  st_size.q   ; File size in bytes (if the file is a regular file)
  st_atime.q  ; Time of last access
  st_mtime.q  ; Time of last data modification
  st_ctime.q  ; Time of last file status change
EndStructure
ImportC ""
  _stat64(path.p-utf8, *buf)
EndImport
Procedure GetFileStats2(filename.s, *stats.stats): Protected stat64.stat64
  _stat64(filename, stat64)
  *stats\mtim = stat64\st_mtime ; UTC-Time Modified
  *stats\ctim = stat64\st_ctime ; UTC-Time Created/Birth
  *stats\size = stat64\st_size
EndProcedure
Define test2.stats
GetFileStats2(filename$, test2)
Debug ""+test2\mtim+" => "+FormatDate("%mm/%dd/%yyyy %hh:%ii:%ss",test2\mtim)
Image

EDIT 08/07/24 : Correction of a small error I made in the code, but which had no effect on the final result.

Re: Get the UTC modification date of a file

Posted: Tue Aug 06, 2024 2:03 pm
by Kiffi
boddhi wrote: Tue Aug 06, 2024 1:51 pm I got the same dates (Win10 PB 6.11 x64)
same here