Unix timestamp: everywhere the same? - unix-timestamp

If I request some Unix timestamps at the same time, in any system, programming language, anywhere on the world (on universe), will they always be the same? Or is it possible that values differ?
As a precondition I assume that each system has to have their time configured correctly. Additional question: nowadays, can I assume devices with an internet connection have the correct time?
So, how reliable is the usage of the Unix timestamp? E.g. if I'd like so set an alert for different users on the world at a certain time and I broadcast just the timestamp, can I assume that the alerts happen in the same second?
(Journeys with speed of light should be disregarded here, I guess.)

Unix timestamps are the number of seconds elapsed since 01-01-1970 00:00:00 UTC so if the system time is set correctly it should be equal everywhere.

Related

Saving time in Postgres with or without TZ?

I have a column in the DB that supposed to represent a deadline for ordering.
Let's assume the value is 11am EST.
I've saved in the DB as UTC during Winter so it was saved as 4PM UTC.
Problem is, when DST starts, the conversion back to EST was 12PM...
How can I solve this? on the DB level? if so how would I save a constant time and convert it to the correct value? If I use timestamp with TZ, will it then be read as 5PM UTC?
Thanks.
The scenario you described is caused by converting a future time to UTC and back again, using two different UTC offsets (UTC-5 and UTC-4). In general, one should not store future times (whether recurring or single-instance) in terms of UTC, unless the original reference point is already in terms of UTC. The advice "Always use UTC" applies only for past/present timestamps. It does not apply for future values.
PostgreSQL has several different date/time data types. The "with time zone" types will convert to/from UTC implicitly. The "without time zone" types will not.
In your case, it sounds like you would like to have a time-only value that describes the deadline that applies every day. Thus, you should use a time (aka time without time zone) data type for that field. You should then also store the IANA time zone ID in a separate character field (varchar(50) should be sufficient). For US Eastern Time, you would store 'America/New_York'. That assumes the cutoff is based on the same time zone for all users. If rather you are cutting it off at their time zone, then it could be different per-user, and you will need to determine the user's time zone.
When evaluating whether the deadline has passed, you would take the current UTC timestamp and convert it to the local date and time in the target time zone to have the current date and time in that zone. You'd then take a copy of that and replace the time part with your deadline time. Then compare those two values to see if the deadline has passed.
You should also be thinking about when does the deadline reset for the next day. Is it strictly based on the local date? If they try to order past that deadline, do you disallow it (and if so when do you allow it again), or does it apply to the next date? Only you can answer such questions, as it will vary based on your business needs.
The manipulations I described could be done directly in PostgreSQL, using functions like AT TIME ZONE and others, but generally you are better off doing them in your application layer. Most programming platforms have functions for manipulating dates and times, and for working with time zones.

How can I calculate exact difference between a server time and GMT time?

How can I calculate exact difference between a server time and GMT time ?
I want to send a POST request to a server on specific time but I know the server time is not accurate so I want to calculate inaccuracy of the server clock (mili seconds) to send my request on time.(if I send request earlier server will block me )
I try this code on ubuntu but it's only show server time.
curl -I --silent www.server.com | grep "^Date"
if I can calculate difference between my pc and server clock it's very helpful for me.
There are many options, of course. Here’s a suggestion.
Write a shell script or .bat file that runs your curl and grep commands at feeds the result into a program that you write.
Write the program in Java or another language that runs on the Java Virtual Machine since this probably has the best support for handling date and time, though all the major programming languages can.
In your program you may use DateTimeFormatter.RFC_1123_DATE_TIME for parsing the server time into an OffsetDateTime. Get the PC time as another OffsetDateTime and use Duration.between() for finding the difference (positive or negative).
Beware that there will be a delay from reading the server time until reading the PC time, so results will not be accurate. If you want a better estimate of upper and lower bound somehow read the PC time both before and after reading the server time.
Links
Oracle tutorial: Date Time explaining how to use java.time.
Documentation:
DateTimeFormatter.RFC_1123_DATE_TIME
OffsetDateTime
Duration.between()

Postgresql now() returning incorrect value

I have two postgresql servers - one running local on my windows machine and one running on a beta linux server.
I am running this command on both (at very close to the same time):
select current_setting('TIMEZONE'), now();
Local DB result:
"US/Pacific";"2015-10-09 12:29:51.053-07"
Beta DB result:
"US/Pacific";"2015-10-09 12:23:00.121953-07"
As you can see, the timezones are the same, but the times are not- the time on the local database is correct, but the time on the beta server appears to be about 6 minutes and 51 seconds slow.
EDIT based on answers:
Also, I checked the dates and times on both operating systems and both are correct.
The selects are not a part of a big transaction. I am using pgAdmin to run just those statements. Also, I ran the timeofday() with the now() function and they returned the exact same times as the now() function calls.
Any idea how this is happening?
Are you sure you checked the current date/time on the OS in both machines? It looks like the clock on them are not "synchronized"...
Also, are you running that "select" inside "long running" transaction? "now()" function returns always the time "frozen" in the beginning of the transaction. To get running time inside a transaction, use timeofday() function.
You checked the dates on both machines - but did you also check the times? The best way to keep the time in sync is using NTP across all your machines. Also if you are unsure about the transactions, you can use timeofday() to get the current system time.
SELECT timeofday();
PostgreSQL directly calls the underlying operating system's date and time routines to get the timestamp, adjusting only for time zone (if needed) and epoch - which is fixed.
Most likely your clocks are not in fact in sync between the two hosts. Set up network time sync. The problem will go away. If you're really, really sure they are, check the output of the date +%H:%M:%S command on the unix system, and time /T on the Windows command line. Are they really the same?
If they're different in PostgreSQL but not as reported by the operating system and you have set up network time sync, please report a bug to pgsql-bugs or follow up here. But please be very sure they're truly the same, and do make sure network time sync is active and working.

Persistent increment counter

I have a concept for a special albeit simple kind of clock that would display the number of seconds since a certain point in time (which would never change). What would be the best way of storing, incrementing and displaying this persistent value?
If the start point never changes, you only need to save that. Concept wise, that would be the same as Unix time.
Simply get the current system time and calculate the difference to the beginning of your epoch.
This was done via the Unix epoch. You would just need to create your own version of the epoch perhaps BobeEpoch. You could store this value somewhere that your application can retrieve it, then you would invoke the current system time. Once you had the current system time you would subtract this value from BobeEpoch and display that to the user.

100+ dates in different timezones, calculate which 2 dates present day is between...more calculate

I am really having a heck of a time figuring out which way I should do this. Been coding objective-c for 4 months now, well trying to at least.
I have about 100+ different dates spanning 2012, down to the second, in multiple timezones. What I need is:
to grab the present time/date, see which 2 dates in my 100+ list it is between and give me time spent and time remaining.
to know which 2 dates it is between no matter what timezone a user is in.
all calculations need to take in consideration of daylight savings time. Which the dates and times of DST change is different depending on the timezone and country.
a user in Hawaii will have the same time remaining and spent as a user in England.
ablility to convert all times to local user time.
have this all realtime. have the clock or timer counting down to the second.
I have tried NSDate. Then I searched this site and found NSDateFormatter. I played with that for what seems like days. Then another search I found NSDateComponents. Do I put my 100+ dates in a multidimensional array. Do I convert everything to GMT first or can xcode do that for me. Or do I convert everytime to seconds since 1970. I am just lost on what would be the best most practical way of doing this.
Any help, thanks so much!!
I am not an iOS programmer, but if you could convert everything to the same time zone (MST, EST, GMT, whatever), then that would make your job far easier. Converting between time zones runs in constant time as there is nothing more involved than simple addition/subtraction.
As for DST, if you convert to MST or EST (as opposed to MDT or EDT), you inherently remove DST. What exactly do you mean by "take in consideration of DST?" I could help much more if you could provide that.