I'm writing software that will be tracking files and if they are changed, should I be using UTC - utc

I'm writing some software to track if a file has been checked out and changed. The file could be checked out and changed by various people in several different time zones.
So, simply put I would be doing something like this:
if ( checked_out_file.last_modified_date > my_database_record_of_the_last_modified_date ) {
// file has changed since last sync so do something
}
The above is just pseudo-code so don't get hung up on what language and things like that. What I am basically wondering is should I store the my_database_record_of_the_last_modified_date as the UTC time, and when I do my checked_out_file.last_modified_date comparison, as illustrated in my pseudo-code above, should I also use the UTC time for that?

Time is a pain to work with. Time zones are even worse. There's not much you can do about that. But if you use UTC everywhere, you can make time a little more bearable.
Store all your dates in UTC. Do all your date comparisons in UTC. Do everything you possibly can in UTC. Ideally, the only time a date won't be in UTC is when it's being formatted for display to a user. Time zones are a rabbit hole you do not want to go down.
Hope this helps!
PS: Yes, I've had bad experiences with time zones in the past. Could you tell?

Related

How do I determine GMT offset corrected for DST? (Embedded system, no O/S but HTTP.)

Wow! Tons of posts on converting GMT to local time, including correction for DST. But it seems my need is different.
As the title says, I have a stand-alone embedded system with no O/S. I'm using NTP to get UTC. That is used to tag events with an accurate date/time. I can correct UTC for the current time zone but cannot automatically adjust for DST.
Since there is no O/S, I don't have any of the Windows/Linux data such as time zone. So there is no way to locally adjust for the GMT offset.
It seems the only way for me to do this is to use an http call to find the offset, and the only way I can think of doing this is using the lon/lat or address. It would be possible for me to add lon/lat or address to the configuration so this seems like the only option.
I've seen references to sites which return the GMT offset based on location. Do these sites also automatically adjust for DST? To do that, they would have to use one of the solutions posted in many places in this forum, but that should be easy enough.
Thanks for the advice and help!
Dave
If you need to only convert a specific single timestamp to local time, then yes - you can use services such as those listed here. At least the ones offered by Microsoft and Google do convert a timestamp to the local time in the time zone given, in addition to providing the IANA time zone id.
Additionally, you'll find that the gettimezonebycoordinates function in the Microsoft Azure LBS Time Zone API returns a PosixTz value, such as "PST+8PDT,M3.2.0,M11.1.0". This is ideal for embedded systems, as you can set your TZ environment variable to this value and then many local APIs (such as with C, and others) will use this value in their conversions. This approach works best when you may be converting many different local time values and don't want to make an http call for each one.
Be aware, however, that using a POSIX time zone string has some limitations, such as being restricted to a single set of DST transition rules. They generally work ok for near-current time ranges, but not for historical purposes.

Sending Notification to different time zones

I have a server in Usa and I have clients in different parts of the world, Australia, South america, Usa, Canada, Europe.
So I need to send notification of events one hour before the event take place.
So In sql server I have a table with different events those events are stored in Utc(2015-12-27 20:00:00.0000000). and in other table the timezone that belongs to every event ("Australia/Sydney").
So how could I calculate in a query when to send the notifications? or maybe I would have to do it with a server side language.
Could any one could help me with a possible solution.
Thanks
You've asked very broadly, so I can only answer with generalities. If you need a more specific answer, please edit your question to be more specific.
A few things to keep in mind:
Time zone conversions are best done in the application layer. Most server-side application platforms have time zone conversion functions, either natively or via libraries, or both.
If you must convert at the database layer (such as when using SSRS or SSAS, or complex stored procs, etc.) and you are using SQL Server, then there are two approaches to consider:
SQL Server 2016 CTP 3.1 adds native support for time zone conversions via the AT TIME ZONE statement. However, they work with Windows time zone identifiers, such as "AUS Eastern Standard Time", rather than IANA/Olson identifiers, such as the "Australia/Sydney" you specified.
You might use third-party support for time zones, such as my SQL Server Time Zone Support project, which does indeed support IANA/Olson time zone identifiers. There are other similar projects out there as well.
Regardless of whether you convert at the DB layer or at the application layer, the time zone of your server should be considered irrelevant. Always get the current time in UTC rather than local time. Always convert between UTC and a specific time zone. Never rely on the server's local time zone setting to be anything in particular. On many servers, the time zone is intentionally set to UTC, but you should not depend on that.
Nothing in your question indicates how you plan on doing scheduling or notifications, but that is actually the harder part. Specifically, scheduling events into the future should not be based on UTC, but rather on the event's specific time zone. More about this here.
You might consider finding a library for your application layer that will handle most of this for you, such as Quartz (Java) or Quartz.Net (.NET). There are probably similar solutions for other platforms.
You should read the large quantity of material already available on this subject here on Stack Overflow, including the timezone tag wiki and Daylight saving time and time zone best practices.

How to know the TimeZone StandardName or DayLightName from TimeZoneOffset in Sql Server

I am using Sql Sever 2008 R2. Is there a way to identify the time zone Standard name or daylight name from timezoneoffSet.
For example I have "2013-09-26 03:00:00.0000000 -04:00" and need
"Eastern Daylight Time" from above.
How can I accomplish this in SQL server ?
Any suggestions will be appreciated.
If you're talking about getting the local system's time zone, I've investigated that heavily in the past, and it isn't possible without resorting to either "unsafe" SQL CLR calls, or unsupported xp_regread calls to read this out of the registry. The general recommendation is to do that in application code, and not in the database.
However, if what you are saying is that you have an offset of -4:00 in your input value, and you want to translate that to a time zone name, I'm afraid that isn't possible at all, neither in SQL, nor in your application code.
The reason is that there are many time zones that share the same offset. For example, see this Wikipedia page that shows all of the zones that use -04:00.
Even if you limit the scope to just the United States, it still won't work in certain cases due to daylight saving time. For example, consider the time stamp of 2013-11-03T01:00:00-05:00. Is this Eastern Standard Time? Or Central Daylight Time? There's no way to tell, because at this moment it could be either one.
In the USA (unlike Europe), each time zone transitions at 2AM in its own local time. So it's like a wave that moves from the east to the west. While the US time zones are normally one hour spaced apart, during the spring-forward transition they can be two hours apart, and during the fall-back transition they can have the same exact local time.

Auto Daylight Savings Time behavior (isolated system)

First time on SO and I have a question that I'd like to get your guys' opinion on:
I work on isolated machines (linux OS, no network connections) and one of the issues I'm running across is what should happen when the user Enables an auto DST setting that I've made.
Since not all areas observe DST, the settings requires an 'on/off' type of setting. Furthermore, since the start and stop days seem to vary with the whims of politicians, it can be configured with start month/day/(1st,2nd,3rd,4th) along with stop month/day/(1st,2nd,3rd,4th). Yes, I left out '5th' but I don't envision that ever being a start/stop condition. So now the start and stop periods can be defined as flexibly as '1st sunday of march'.
Now the meat of the question: What then should happen when DST is enabled/disabled? I think I would expect it to auto-change the current time if the date & month falls within the DST period. At the same time I have a few users saying that it's a bit strange when the following workflow happens: Set time to 11:00 AM, Set date, Enable DST, time jumps to 12:00PM. Would it make more sense to not change the time upon enable/disable and just wait until the next DST date to correct it?
Looking forward to hearing some opinions :) TIA
I don't know that there's a best choice here. I think it comes down to user preference. Personally I'd probably apply the adjustment when the option is changed, but also try to make the resulting (possible) jump in the time obvious to the user so that if it's not what they expected, they'll know right away that they need to adjust the clock.
Depending on which locales you may need to work with, you should also be aware that not everywhere uses the "first Sunday in March" style for determining when to switch to/from DST. Some places use arbitrary dates hand-picked each year, others may use a specific day of the month. The time of day that the change occurs at varies as well, and some places base their changeover time & date on UTC while others base it on local time. You may need to account for several variations on how the changeover dates are determined unless you're only dealing with a limited set of locations that are more consistent in how they do this.
Even just in Canada, there are places that change at midnight and places that change at 2:00AM, and some that don't change at all.

How to convert every time zone in GMT +5.30?

I am creating an application. I am running a countdown timer and saving time into a database by converting time into a floating point value (double). Suppose I start the timer at 2.00 PM and set the countdown for 3 hours. After 1 hour I quit the application and on relaunch I am getting time stored from database. Count the difference between stored time and current time I restore the timer according to that, but what if I quit the application and change the time zone to something else?
How do I handle this or how do I fix a specific time zone so that even user changes the time zone the timer always works perfect?
I don't know the iPhone SDK, but the traditional way of doing this would be to find the time in UTC - often expressed in terms of "milliseconds since Jan 1st 1970" or something similar.
That way the time zone of the device is irrelevant.
EDIT: Looking at the docs for NSDate, I think timeIntervalSince1970 is what you want. That should always be in terms of UTC.