Get the UTC modification date of a file

Just starting out? Need help? Post your questions and find answers here.
Lebostein
Addict
Addict
Posts: 833
Joined: Fri Jun 11, 2004 7:07 am

Get the UTC modification date of a file

Post 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
boddhi
Enthusiast
Enthusiast
Posts: 524
Joined: Mon Nov 15, 2010 9:53 pm

Re: Get the UTC modification date of a file

Post 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.
Last edited by boddhi on Wed Aug 07, 2024 11:41 am, edited 1 time in total.
If my English syntax and lexicon are incorrect, please bear with Google translate and DeepL. They rarely agree with each other!
Except on this sentence...
User avatar
Kiffi
Addict
Addict
Posts: 1509
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: Get the UTC modification date of a file

Post by Kiffi »

boddhi wrote: Tue Aug 06, 2024 1:51 pm I got the same dates (Win10 PB 6.11 x64)
same here
Hygge
Post Reply