Thread: Get last modified date and time

  1. #1
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352

    Get last modified date and time

    Hello, everyone. I was wondering how to get the date and time for when a file was last modified. I am not writing for a PC, but I have some Unix libraries available. I would just like to get the date and time in MM/DD/YYYY HH:MM .

    Also, it would be great if I could store the results in integers.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    If you have Unix libraries available, look into the stat() function if you have it. It will return details of the file, including the st_mtime field, which is the last modified time. It's stored as a time_t, which should be seconds since the Unix epoch, so you can use various functions from time.h to get your MM/DD/YYYY and HH:MM.

    Of course, the availability of such data regardless of library function is dependent on the underlying file system.

  3. #3
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Alright, I found an example and was able to get MM/DD/YYYY and HH:MM.

    Code:
    #include <sys/stat.h>
    #include <unistd.h>
    #include <time.h>
    #include <stdio.h>
    
    struct tm *foo;
    struct stat attrib;
    
    int main() {
    	stat("file.txt", &attrib);
    	foo = gmtime(&(attrib.st_mtime));
    	printf("Year: %d\n", foo->tm_year);
    	printf("Month: %d\n", foo->tm_mon);
    	printf("Day: %d\n", foo->tm_mday);
    	printf("Hour: %d\n", foo->tm_hour);
    	printf("Minute: %d\n", foo->tm_min);
    	return 0;
    }
    Thanks, anduril. I have yet to test this on my platform yet, but it should work.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ date time
    By rahulsk1947 in forum C++ Programming
    Replies: 2
    Last Post: 05-22-2007, 02:06 PM
  2. Date & Time
    By g4j31a5 in forum C++ Programming
    Replies: 10
    Last Post: 08-15-2006, 09:25 AM
  3. Date Last Modified
    By magic.mike in forum Windows Programming
    Replies: 5
    Last Post: 09-23-2005, 05:13 AM
  4. Getting time and date
    By winsonlee in forum C++ Programming
    Replies: 3
    Last Post: 08-18-2004, 11:31 PM
  5. Last modified date?
    By Hoxu in forum C++ Programming
    Replies: 3
    Last Post: 11-15-2001, 12:32 PM