I see a strange behavior while fetching the modification time of a file.
we have been calling _stat64 method to fetch the file modification in our project as following.
int my_win_stat( const char *path, struct _stati64 *buf)
{
if(_stati64( path, buf) == 0)
{
std::cout<<buf->st_mtime << std::endl; //I added to ensure if value is not changing elsewhere in the function.
}
...........
...........
}
When I convert the epoch time returned by st_mtime variable using epoch convertor, it shows 2:30 hrs ahead of current time set on my system.
When I call same API as following from different test project, I see the correct mtime (i.e. according to mtime of file shown by my system).
if (_stat64("D:\\engine_cost.frm", &buffer) == 0)
std::cout << buffer.st_mtime << std::endl;
Even I called GetFileTime() and converted FILETIME to epoch time with the help of this post. I get the correct time according to time set the system.
if (GetFileTime(hFile, &ftCreate, &ftAccess, &ftWrite))
{
ULARGE_INTEGER ull;
ull.LowPart = ftWrite.dwLowDateTime;
ull.HighPart = ftWrite.dwHighDateTime;
std::cout << (ull.QuadPart / 10000000ULL - 11644473600ULL);
}
What I am not able to figure out is why does the time mtime differ when called through my existing project?
What are the parameters that could affect the output of mtime ?
What else I could try to debug the problem further ?
Note
In VS2013, _stati64 is a macro which is replaced replaced by _stat64.
File system is NTFS on windows 7.
Unix time is really easy to deal with. It's the number of seconds since Jan 1, 1970 (i.e. 0 represents that specific date).
Now, what you are saying is that you are testing your time (mtime) with a 3rd party tool in your browser and expects that to give you the right answer. So... let's do a test, the following number is Sept 1, 2015 at 00:00:00 GMT.
1441065600
If you go to Epoch Converter and use that very value, does it give you the correct GMT? (if not, something is really wrong.) Then look at the local time value and see whether you get what you would expect for GMT midnight. (Note that I use GMT since Epoch Converter uses that abbreviation, but the correct abbreviation is UTC.)
It seems to me that it is very likely that your code extracts the correct time, but the epoch convertor fails on your computer for local time.
Note that you could just test in your C++ program with something like this:
std::cerr << ctime(&buf->st_mtime) << std::endl;
(just watch out as ctime() is not thread safe)
That will give you the data according to your locale on your computer at runtime.
To better control the date format, use strftime(). That function makes use of a tm structure so you have to first call gmtime or localtime.
An year later I ran into the similar problem but scenario is little different. But this time I understand why there was a +2:30Hrs of gap. I execute the C++ program through a perl script which intern sets the timezone 'GMT-3' and my machine had been in timezone 'GMT+5:30'. As a result there was a difference of '2:30Hrs'.
Why ? As Harry mentioned in this post
changing the timezone in Perl is probably causing the TZ environment variable to be set, which affects the C runtime library as per the documentation for _tzset.
Related
I have some simple code attempting to get the last modified date of the users recycle bin. This code seems to work on other files however it does not work on the recycle bin. I've done some looking into and it seems the recycle bin is a special file as in it's not a regular file. This would explain why I'm getting Unix epoch as the result. Here is my code:
struct tm* tmDateModified;
struct stat attribute;
stat("C:\\$Recycle.Bin\\Recycle Bin", &attribute);
tmDateModified = gmtime(&(attribute.st_mtime));
std::string date = asctime(tmDateModified);
String^ date2 = marshal_as<String^>(date);
MessageBox::Show(date2);
When I execute this code, the MessageBox displays Thu Jan 1 00:00:00 1970 as the result, again that's Unix epoch. I know this is possible as I've seen it done before in other applications, however, I don't have access to their source so I can't exactly figure out how they did it. If anyone knows why Unix epoch is being displayed rather than the actual date modified and how to fix it, I'll take any help.
In Python or Java you can get the UTC offset (at present time) given the IANA name of the timezone ("America/Los_Angeles" for instance). See Get UTC offset from time zone name in python for example.
How can you do the same using C/C++ on Ubuntu 14.04?
EDIT: Preferably in a thread-safe way (no environment variables).
You alluded to this fact, but it's important to note that the offset between UTC and the time in a time zone is not necessarily constant. If the time zone performs daylight saving (summer) time adjustments, the offset will vary depending on the time of year.
One way to find the offset is to take the time you're interested in, hand it to the localtime() function, then look at the tm_gmtoff field. Do this with the TZ environment variable set to the zone name you're interested in. Here's an example that does so for the current time:
#include <time.h>
#include <stdio.h>
int main()
{
setenv("TZ", "America/Los_Angeles", 1);
time_t t = time(NULL);
struct tm *tmp = localtime(&t);
printf("%ld\n", tmp->tm_gmtoff);
}
At the moment this prints -25200, indicating that Los Angeles is 25200 seconds, or 420 minutes, or 7 hours west of Greenwich. But next week (actually tomorrow) the U.S goes off of DST, at which point this code will start printing -28800.
This isn't guaranteed to work, since the tm_gmtoff field is not portable. But I believe all versions of Linux will have it. (You might have to compile with -D_BSD_SOURCE or something, or refer to the field as __tm_gmtoff. But in my experience it tends to work by default, as plain tm_gmtoff.)
The other way is to go back and forth with gmtime and mktime, as described in Sam Varshavchik's answer.
Addendum: You asked about not using environment variables. There is a way, but unfortunately it's even less standard. There are BSD functions tzalloc and localtime_rz which do the job, but they do not exist on Linux. Here's how the code looks:
timezone_t tz = tzalloc("America/Los_Angeles");
if(tz == NULL) return 1;
time_t t = time(NULL);
struct tm tm, *tmp = localtime_rz(tz, &t, &tm);
printf("%ld\n", tmp->tm_gmtoff);
For me this prints -28800 (because PDT fell back to PST just a few minutes ago).
If you had it, you could also use localtime_rz along with mktime in Sam Varshavchik's answer. And of course Howard Hinnant's library is pretty obviously thread-safe, not requiring mucking with TZ at all.
EDIT (OP): The code for localtime_rz and tzalloc can be downloaded from https://www.iana.org/time-zones and works on Ubuntu 14.04.
You could use this free open source C++11/14 library to do it like this:
#include "chrono_io.h"
#include "tz.h"
#include <iostream>
int
main()
{
using namespace date;
using namespace std::chrono;
auto zt = make_zoned("America/Los_Angeles", system_clock::now());
std::cout << zt.get_info().offset << '\n';
}
This currently outputs:
-25200s
Or you could format it differently like this:
std::cout << make_time(zt.get_info().offset) << '\n';
which currently outputs:
-07:00:00
The factory function make_zoned creates a zoned_time using the IANA name "America/Los_Angeles" and the current time from std::chrono::system_clock. A zoned_time has a member getter to get the information about the timezone at that time. The information is a type called sys_info which contains all kinds of useful information, including the current UTC offset.
The UTC offset is stored as a std::chrono::seconds. The header "chrono_io.h" will format durations for streaming. Or the make_time utility can be used to format the duration into hh:mm:ss.
The program above is thread-safe. You don't have to worry about some other process changing TZ out from under you, or changing the current time zone of the computer in any other way. If you want information about the current time zone, that is available too, just use current_zone() in place of "America/Los_Angeles".
If you wanted to explore other times, that is just as easy. For example beginning at Nov/6/2016 at 2am local time:
auto zt = make_zoned("America/Los_Angeles", local_days{nov/6/2016} + 2h);
The output changes to:
-28800s
-08:00:00
More information about this library was presented at Cppcon 2016 and can be viewed here:
https://www.youtube.com/watch?v=Vwd3pduVGKY
Use gmtime(), first, to convert the current epoch time into UTC time, then use mktime() to recalculate the epoch time, then compare the result to the real epoch time.
gmtime() calculates the struct tm in UTC, while mktime() assumes that the struct tm represents the current local calendar time. So, by making this round-about calculation, you indirectly figure out the current timezone offset.
Note that mktime() can return an error if struct tm cannot be convert to epoch time, which will happen during certain transitions between standard time and alternate time. It's up to you to figure out what that means, in your case.
The recipe looks something like this:
time_t t = time(NULL);
struct tm *tmp = gmtime(&t);
time_t t2 = mktime(tmp);
int offset = t - t2;
See the documentation of these library functions for more information.
To use a specific time zone, either set the TZ environment variable, or you can try using localtime_rz as in Steve Summit's answer. As mentioned, beware that mktime can sometimes return -1 for unconvertible times.
Basically, I want to create a program that will check the month, the day and the year and will execute code if both the month and the day criteria is met.
For example, let's say the date was July 8th, 2016.
Let's say I had some code that simply wanted the program to output "Hello world!" on this date.
I would want this code to execute on July 8, 2016 and no other date. How would I go about this?
To run your program at a certain time, you have to rely on external tools such as cron or the Windows task scheduler. A program cannot run itself if it's not already running :-)
If your code is running and you just want it to delay action until some specific time, that's what all the stuff in the ctime header is for.
You can use time() and localtime() to get your local time into a struct tm, then examine the fields to check if some specific time is current. If so, do your action. If not, loop around and try again (with a suitable delay if needed).
By way of example, here's a program that outputs the time but only on five-second boundaries:
#include <iostream>
#include <iomanip>
#include <ctime>
using namespace std;
int main() {
time_t now;
struct tm *tstr;
// Ensure first one is printed.
int lastSec = -99;
// Loop until time call fails, hopefully forever.
while ((now = time(0)) != (time_t)-1) {
// Get the local time into a structire.
tstr = localtime(&now);
// Print, store seconds if changed and multiple of five.
if ((lastSec != tstr->tm_sec) && ((tstr->tm_sec % 5) == 0)) {
cout << asctime(tstr);
lastSec = tstr->tm_sec;
}
}
return 0;
}
I would use std::this_thread::sleep_until(time_to_execute); where time_to_execute is a std::chrono::system_clock::time_point.
Now the question becomes: How do you set the system_clock::time_point to the correct value?
Here is a free, open-source library for easily setting a system_clock::time_point to a specific date. Using it would look like:
using namespace date;
std::this_thread::sleep_until(sys_days{jul/8/2016});
This would trigger at 2016-07-08 00:00:00 UTC. If you would rather trigger based on your local time, or some arbitrary time zone, here is a companion library to accomplish that.
You can also drop down to the C API and set a std::tm's field values, convert that to a time_t and then convert that to a system_clock::time_point. It is uglier, more error prone, and doesn't require a 3rd party library.
I have been wracking my head crazy trying to figure this out with this API
My original implementation was something like:
// TimezonePtr is just a share_ptr to the timezone
std::tm getGMT(const std::tm& rawtime, TimezonePtr tz)
{
std::tm result = rawtime;
const auto loct = mktime_z(tz.get(), &result);
gmtime_r(&loct, &result);
return result;
}
However, this does not take into account DST. For example, if I feed it a date of Sep 28 2012 15:54:24 I get back Sep 28 2012 20:54:24, which is incorrect. It looks like I want to use localtime_rz, except that takes an epoch, which is driving me nuts because if I could get the epoch then I'd already have my answer. :(
How can I accomplish this?
mktime_z takes a struct tm as one of its arguments. If you don't know whether DST is in effect for the input date, you want to set the tm_isdst member of that tm to -1 to signify that the system should figure out whether DST is in effect for that date/time/timezone when you call mktime.
At least for me, this seems to work correctly (i.e., it correctly concludes that at least in my time zone, DST was in effect in September of 2012).
In addition to Jerry Coffin's correct (and up-voted) answer, I wanted to show how this computation could be done with a modern C++11/14 library (free and open source).
I've kept the API the same in the interest of making the code easy to compare:
template <class Duration>
auto
getGMT(date::local_time<Duration> rawtime, const date::time_zone* tz)
{
return tz->to_sys(rawtime);
}
This returns a std::chrono::time_point<system_clock, Duration> where Duration is the finer of the input Duration and seconds. If the ragtime doesn't have a unique mapping to UTC according to the indicated time zone, an exception will be thrown. Such an event can occur (for example) if rawtime is during a daylight saving transition and occurs twice, or not at all. If desired, there exists API for avoiding the exception if you want to "pre-decide" how you would like to map ambiguous and non-existent local times into UTC.
This function can be exercised like this:
#include "tz.h"
#include <iostream>
int
main()
{
using namespace date;
using namespace std::chrono_literals;
std::cout << getGMT(local_days{sep/28/2012} + 15h + 54min + 24s,
current_zone()) << " UTC\n";
std::cout << getGMT(local_days{sep/28/2012} + 15h + 54min + 24s,
locate_zone("America/New_York")) << " UTC\n";
}
This exercises the code twice:
With whatever the current time zone is for this computer.
With the time zone "America/New_York"
For me these are both the same time zone, resulting in the following output:
2012-09-28 19:54:24 UTC
2012-09-28 19:54:24 UTC
Not only is it simpler to use this library than the BSD ctime API,
but this API has type safety. For example the UTC time point and local time point are represented by different types, allowing the compiler to tell you if you accidentally use the wrong one. In contrast the BSD ctime API uses the same type (tm) for both local time and UTC.
[Updated]
I am trying to find to correct way to convert a timestamp in UTC to different timezones.
The exact problem: I have timestamps in my program and its always stored in UTC so its fine. however, i have to be able to display them (actually write them to files) in different timezones according to user preferences.
I am working on linux but I would like to write pateform-independant code.
I don't want to use boost libraries (we are already using Qt which does not provide as much functions as boost for dates).
I would like to write thread-safe code
I would like to identify the timezones like US/Eastern to simplify the configuration (it is done by users and im not very confident that they would make correct use of the abbreviations like EST, CET, CEST...).
I already looked on Internet and found some more or less working code but
most of the time it uses the TZ env variable which is said to be a not thread-safe method.
It uses abbreviations for the timezone (like EST, CET...).
Could anybody indicate me a good approach?
Here is what I have now (found on Internet some days ago and modified to used my Qt library in this example).
This code is probably not thread-safe.
NEW VERSION:
Still not thread-safe but it more or less do the job.
Probably not easily portable to window environment.
It handles the daylight change
see bellow example (daylight change in Paris happens on 25 Mars 2012 at 01H00 UTC (passing from 02H00 localtime at 03H00 localtime).
It is an example to convert Timestamp from UTC to Paris (has daylight change) & Kuala_Lumpur (does not have daylight change).
#include <QtCore/QCoreApplication>
#include <QDateTime>
#include <stdio.h>
#include <stdlib.h>
#include <QDebug>
void treatTimestamp(QString timestamp,QString format);
int main(int argc, char *argv[])
{
QString format = "MM:dd:yyyy hh:mm:ss";
treatTimestamp("03:25:2012 00:59:59",format);
qDebug()<<"---------------------";
treatTimestamp("03:25:2012 01:00:00",format);
return 0;
}
void treatTimestamp(QString timestamp_s,QString format)
{
unsetenv("TZ");
setenv("TZ", "UTC", 1);
QDateTime timestamp = QDateTime::fromString(timestamp_s, format);
qDebug()<<"CUSTOM TS UTC:"<<timestamp.toUTC().toString(format).toStdString().c_str();;
time_t tmp = timestamp.toUTC().toTime_t();
setenv("TZ", ":Asia/Kuala_Lumpur", 1);
qDebug()<<"CUSTOM TS KL:"<<QDateTime::fromTime_t(tmp).toString(format);
setenv("TZ", "Europe/Paris", 1);
qDebug()<<"CUSTOM TS Paris:"<<QDateTime::fromTime_t(tmp).toString(format);
unsetenv("TZ");
}
Output (First : one second before the timechange, Second: one second after).
CUSTOM TS LOC: 03:25:2012 01:00:00
CUSTOM TS UTC: 03:25:2012 01:00:00
CUSTOM TS KL: "03:25:2012 09:00:00"
CUSTOM TS Paris: "03:25:2012 03:00:00"
CUSTOM TS LOC: 03:25:2012 03:00:00
CUSTOM TS UTC: 03:25:2012 03:00:00
CUSTOM TS KL: "03:25:2012 11:00:00"
CUSTOM TS Paris: "03:25:2012 05:00:00"
According to this thread, using QDateTime it is possible to do dateTime.addSecs(3600*timeZoneOffset); where dateTime is QDateTime.
According to gmtime reference, there's no built-in timezone support in C library, but you can "kinda" simulate them by adding requiring offset to tm->tm_hour. Which won't adjust date correctly (unlike QDateTime method), by the way.
According to mktime reference, mktime will "normalize" datetime values, so you could add time offset to tm_hour, call mktime. However, it isn't specified HOW mktime adjusts fileds of struct tm - if you say, set tm_hour to 27, will it clamp tm_hour to 23 or will set tm_hour to 3, increasing tm_day (and possibly month/year)?
If I were you, I'd simply use QDateTime::addSecs method.
I give +1 to SigTerm as his answers have been constructive.
In the end i validate with users that one running process will only need 2 timezones : a specified one and the UTC one (mostly for logging).
So in the end I use this at the top of the program
unsetenv("TZ");
setenv("TZ", "", 1);
Then in the specific parts where i need UTC time i always call the Qt toUTC method.
It is really not satisfying but the full software is about data acquisition and the timestamp is an important part of it so i didn't want to make my own calculation in the code.
I heard that Qt5 will have an implementation of timezone manipulation similar to what exists in the boost library. Maybe ill refactor the code when it will be out,.