Original article I have published at Code Project
I recently looked for photos and videos of an important event and couldn’t understand why I can’t find any video files, even though I recalled that my wife and my daughter took both photos and videos…
I then realized that our still camera (Nokia D5000) and camcorder (Sony) are both set up with the wrong time, each with a different wrong time… One was 7 hours and 36 minutes earlier and the other was 3 hours later. That was the reason for the confusion, and I spent a lot of time checking backups, etc. thinking my precious files were somehow deleted. I calculated the correct times of both video and photo files, adjusting their time stamp and happily found out that both occurred within the same time frame, so everything was OK. I then setup the time of the camcorder and the camera to the correct time. and looked for a way to fix the incorrect time stamps of my photos and video files. That is when I decided to program such tool myself…
Even though there can be many occasions in which a program like the one I am about to introduce, can be useful, I originally developed it for the purpose of adjusting wrong time stamp of photos and videos, which are a result of incorrect settings or timezone in the camcoder or camera. The idea is to define the following:
Path – where to look (will bring a path dialog box where the user can type or choose the start path to search. For example: c:\ or c:\users\myuser\documents\
Query – what to look for (for example all files ending with .AVI, or all files within a certain date, or files which contain a certain string in their name).
Date related attributes to apply to– which can be either:
- Date Created
- Date Last Modified
- Date Last Accessed
- Date Taken
Requested change – which can be either:
- Fixed date and time, for example: 7.7.2014 01:00
- Relative change of currently stamped date and time, for example: 7 and a half hours earlier (Dec 12, 2014 12:31AM will be adjusted to: Dec 11, 2014 5:01PM)
The Building Blocks
Searching for files based on a given criteria
Our application allows you to either state the extension of files searched for, the name (or part of it) but also to process only files having a certain date/time stamp. I will elaborate about the various attributes files have, which are date/time related in the next section, however, for the simplicity of this article, our program changes all of these attribute at once. A ComboBox is used to allow the user to make the selection among the above options.
Changing a file’s date/time related attributes
There are several attributes which are relevant for our goal, among them:
- Date Created- The date and time in which the file was first created.
- Date Modified- The date and time in which the file was last modified.
- Date Accessed – The date and time in which the file was last accessed.
for photos there is another important attribute: Date Taken. That is the date and time a photo was taken.
For the purpose of the article, “photos” are identified by their extension and include .jpg and .nef (Nokia) photos, but that of course can and should be enhanced.
The Date Taken Property
The Date Taken property appears as one of the optional columns Windows Explorer allows to choose. This property is only valid for photos. Unlike the other date related file properties, this one is taken from the EXIF (Exchange Image File format) of the image file.
In order to read and manipulate the EXIF of the file, I have used exif.cpp and exif.h written by Davide Pizzolato, which based his work on jhead-1.8 by Matthias Wandel.
When a file is identified as a photo, getTakenXap is called:
Calculating date and time differences
If you look at web sites like this one, you can check the possibilities covered in the code, for example changing the date/time stamp of a file so it will show 7 and a half hours backward.
To do such calculations from an application, we can use CTime for storing the time, and CTimeSpan for the calculations.
CTime class is used to hold an absolute time and date.
Microsoft provides 7 different constructors for the
CTime class which amongst others, allows you to do the following:
- Create a time class using a Standard Library
- Create a time class using a dos date and time.
- Create a time class using a Win32
- Create a time class using individual entries for year, month, day, hour, minute, and second.
By incorporating the ANSI
time_t data type, the
CTime class provides all the functionalities discussed above in section 1. It also has methods to get the time in
FILETIME or GMT format.
In addition this class also overloads the +, -, = , ==, <, <<, >> operators to provide many more useful features.
You can find the definition of
CTime class in afx.h header file and it is as following:
First you select the search creteria, and / or a folder where you wish to start…
Alternatively, files can be just dragged and dropped to the dialog box.
Note: in this version only one file can be dragged, but that will be fixed later.
For the purpose of this article, I have created a folder named “test” and copied there many files, and folders. These files are both photos (.jpg) and non photos (.txt).
After the files are found based on the search criteria, selected or dragged and dropped, the process starts. Each file is checked and its date/time attributes are changed.
- If a certain date and time are requested, the change takes into consideration the local time zone and whether day light savings is on or off.
- An “Undo” button allows reversing any change.
- The log of the changes that have taken place is displayed on screen, along with the date/time before and after the process.
- The user can check via check boxes which dates should be changed.
- The dates include the “Date Taken” attribute which is unique for photos (such as .jpg and camera specific files such as .NEF files (Nikon camera), etc. That is done via accessing the EXIF of the graphic file.
As part of my efforts to make my small application user friendly and easy to use, I have done the following:
Keeping last entered values:
Since there are two types of input from the user: a fixed date / time and a relative time (number of hours), which are indicated by setting a Combo box to either “Relative Date” or “Fixed Date”, it is important that when the user switches between the two, the last value entered will be show. For example, if you entered the fixed date “2000/01/01” and then entered 8:30 as a Relative Date, when you select “Fixed Date” again, the last value “2000/01/01” should be shown, and when switching back to Relative Date, the last relative value, “8:30” should be shown as well.
Allowing flexible data entry
It should be possible to enter the following as a fixed date:
- 2000/01/01 00:00:00
- 2000/01/01 00:00
It should be possible to enter a relative time in various ways:
- 10 (means 10 hours forward)
- -5 (means 5 hours backward)
- 10:30 (means 10 and a half hours forward)
and so on…
In case there is an error, such as a file being locked, the log entry of this specific file is marked as “Failed” and the process continues.
As I am involved with large scale projects, my software venture purchases a Code Signing Certificate from Verisign (they cost $499 a year, and are suitable also for Kernel drivers).
To sign an executable, I use a tool named kSign by Commodo.
The difference between signing your executables and not signing them can be explained by the warrning your customer will get when trying to download a non signed executable.
But if your executable is signed, the user will get this message:
Which is better. Obtaining a Verisign certificate means that your identity (or your company’s identity) are fully verified.
Thanks to Aha-Soft for the icon used for the demo application. Copyright © 2000-2014 Aha-Soft
If you find bugs, please feel free to send me the revised source code
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)