Seconds since 1970 converter
The algorithms don't count leap seconds. If you need that, it can be done, but requires a table lookup, and that table grows with time. The date algorithms deal only with units of days, and not nanoseconds.
To convert nanoseconds to days, divide by the same amount. The algorithms are in the public domain. Use them however you want. The date algorithms paper has several more useful date algorithms if needed e. These algorithms are wrapped up in an open source, cross platform, type-safe date library if needed. If timezone or leap second support is needed, there exists a timezone library built on top of the date library.
See how to convert among different time zones. Update: Are there any pitfalls to ignoring leap seconds when doing date calculations in this manner?
Answer: There are some pitfalls. And there are some benefits. It is good to know what they both are. Almost every source of time from an OS is based on Unix Time. Unix Time is a count of time since excluding leap seconds.
This is not a fact specified by the standard except it is specified by POSIX , but it is the de facto standard. In fact to take leap seconds into account in such a context would actually introduce errors. So it is good to know your source of time, and especially to know if it also neglects leap seconds as Unix Time does. If your source of time does not neglect leap seconds, you can still get the correct answer down to the second.
You just need to know the set of leap seconds that have been inserted. Here is the current list. For example if you get a count of seconds since UTC which includes leap seconds and you know that this represents "now" which is currently , the current number of leap seconds inserted between now and is So you could subtract 26 from your count, and then follow these algorithms, getting the exact result. This library can automate leap-second-aware computations for you. For example to get the number of seconds between UTC and UTC including leap seconds, you could do this:.
Neglecting leap seconds Unix Time looks like:. Between and 10 "leap seconds" were inserted, but in units smaller than a second, and not just at the end of Dec or Jun.
Documentation on exactly how much time was inserted and exactly when is sketchy, and I have not been able to track down a reliable source. Atomic time keeping services began experimentally in , and the first atomic-based international time standard TAI has an epoch of GMT what is now UTC. Prior to that the best we had was quartz-based clocks which were not accurate enough to worry about leap seconds. A value that approximates the number of seconds that have elapsed since the Epoch.
The relationship between the actual time of day and the current value for seconds since the Epoch is unspecified. How any changes to the value of seconds since the Epoch are made to align to a desired relationship with the current actual time is implementation-defined.
As represented in seconds since the Epoch, each and every day shall be accounted for by exactly seconds. Note: The last three terms of the expression add in a day for each year that follows a leap year starting with the first leap year since the Epoch. The first term adds a day every 4 years starting in , the second subtracts a day back out every years starting in , and the third adds a day back in every years starting in The divisions in the formula are integer divisions; that is, the remainder is discarded leaving only the integer quotient.
The rest in the formula is trivial. I've implemented a convertor in integer arithmetic in this answer. Took about 20 mins to write it and most of that was spent arguing with a friend as to whether I should include leap-seconds, nano-seconds, etc.
Pass it the seconds as the first parameter. The second parameter should be true for local time, false for GMT. The third parameter is a pointer to a structure to hold the response. The value is positive if daylight saving time is in effect, zero if it is not, and negative if the information is not available.
First of all, do not store your seconds as a float. You're going to need integers to do these calculations. It depends on your time zone DST rules, leap years, leap seconds , but I would say first get the number of days by integer dividing by Then find out what's left over, by modulo dividing by Now you can figure out how many years have passed by first integer dividing the number of days by , and then subtracting the number of leap days from the remaining days calculated by modulo dividing the number of days by You'll also want to subtract the number of leap seconds from the number of remaining seconds already calculated.
If that subtraction drives those numbers below zero, then subtract from the next biggest denomination. Then you can calculate the day of month using explicit logic for your calendar. Personally, I would just use Boost. Below is the C code that requires only a general 8-bit multiplication and a division by constant values 4 and Both on bit long operand.
The C code can be easily ported to the final framework. It gives the same result as DateTimeOffset. ToUnixTimeSeconds from. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Math to convert seconds since into date and vice versa Ask Question. Asked 10 years, 2 months ago. Active 2 years, 4 months ago. Viewed 61k times.
Howard Hinnant k 49 49 gold badges silver badges bronze badges. David David There must be a thousand implementations of this If you want precision, you want a large integral type representing microseconds or what-have-you.
You Don't want to use floating point if precision is what you're after. Too many strange corner-cases. Jump to top. GMT Local time. To display the epoch: Time. NET Framework 4. Unix More Go. Command line: perl -e "print time" If Perl is installed on your system.
To display add. Example code. Or using datetime: import datetime; datetime. AddSeconds epoch. PostgreSQL version 8. See Time Zones.
0コメント