Epoch Time - Page 1
Forum section: electronics / general

author: ElectricGuy
author_rank: Regular Contributor
timestamp: August 10, 2015, 01:16:17 am
content: Hi;Can somebody point me out a site where i can find the way how to convert epoch time to TimeStamp and vice versa?I been searching and i just find online converters!Thanks

author: nitro2k01
author_rank: Frequent Contributor
timestamp: August 10, 2015, 01:58:51 am
content: If by timestamp, you nean UNIX timestamp, they are the same! UNIX timestamp = epoch time. The number of seconds since 1970-01-01.

author: helius
author_rank: Super Contributor
timestamp: August 10, 2015, 02:04:04 am
content: I'll assume you mean conversion between unix timestamp (seconds since the epoch) and a standard time format like ISO8601. leap seconds make this challenging to do in general, and if you want to support zones other than UTC you're triply screwed.most people just use someone else's library to stay sane.

author: ElectricGuy
author_rank: Regular Contributor
timestamp: August 10, 2015, 02:05:49 am
content: yes is unix time... no DST... just formulas for convert unix time to time date, and vice versa

author: codeboy2k
author_rank: Super Contributor
timestamp: August 10, 2015, 03:27:17 am
content: Do you mean unix time to local time? best to use a library if you can. The standard C library has ctime(), gmtime() and localtime() functions for doing this, and they take into account DST.From the man page:The ctime(), gmtime() and localtime() functions all take an argument of data type time_t which represents calendar time. When interpreted as an absolute time value, it represents the number of seconds elapsed since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).For embedded systems, use musl, newlib-nano , dietlibc , or uClibcAll of these libraries have ctime() and many other time related functions.http://www.musl-libc.org/https://github.com/32bitmicro/newlib-nano-1.0http://www.fefe.de/dietlibc/http://www.uclibc.org/uClibc is the most configurable and most mature (I think)...but it is targeted toward embedded Linux (posix) systems, and you may have difficulty using the whole of uClib in a non-posix environment.However if all you need is ctime() then you can pull out the needed code from any library and make it work for you.You also have to consider license issues too.. always check with your lawyers, but musl is MIT licensed, newlib is GPL3, dietlibc is GPL2, and uClibc is LGPL 2.1.The MIT license is the best and least restrictive, followed by the LGPL, then GPL2, and last GPL3 being the most restrictive licensing terms.

author: ElectricGuy
author_rank: Regular Contributor
timestamp: August 10, 2015, 03:31:45 am
content: Thanks, but i don't want a library, i want to know how to do it.... been searching but cand find anything, only libraries and online converters.This is really just curiosity, its not for any language, i just want to know the formulas

author: codeboy2k
author_rank: Super Contributor
timestamp: August 10, 2015, 03:48:16 am
content: Quote from: ElectricGuy on August 10, 2015, 03:31:45 amThe best way is to read the source. I gave you a few links.There is no super-formula that will do it all for you. The code is just converting a seconds count since 00:00:00 Jan 1, 1970 UTC into your local time. The code just knows that there are 86400 seconds in a day, 3600 seconds in an hour, 60 seconds in a minute, etc.. and along the way the code makes corrections for global timezone changes and leap seconds and leap years etc... It has all this built-in knowledge of global time systems and dates of time system changes (i.e. if a country decides that it doesn't want to have a DST for one year, the code knows that)What you'll find is the code makes plenty of special cases for time system changes at several dates that occurred since the Epoch.

author: tggzzz
author_rank: Super Contributor
timestamp: August 10, 2015, 08:56:26 am
content: Quote from: mojo-chan on August 10, 2015, 08:42:28 amClearly you haven't done it. Please supply the simple answers to the following questions:how many days are there in a yearhow many months are there in a yearhow many days are there in a monthhow many hours are there in a dayhow many seconds are there in a minute

author: ruffy91
author_rank: Regular Contributor
timestamp: August 10, 2015, 09:14:31 am
content: Quote from: tggzzz on August 10, 2015, 08:56:26 amI'll leave the rest as an exercise to the reader.TLDR;Please don't code your own time functions, there are so many exceptions and pitfalls that you for sure won't get it right the first time. (Have a look at:http://www.theguardian.com/technology/2012/jul/02/leap-second-amadeus-qantas-reddit)For reference have a look at the folder "time" in the glibc sources.

author: sync
author_rank: Frequent Contributor
timestamp: August 10, 2015, 09:18:22 am
content: Quote from: ruffy91 on August 10, 2015, 09:14:31 am23-25 (daylight saving time)

author: tggzzz
author_rank: Super Contributor
timestamp: August 10, 2015, 09:24:33 am
content: Quote from: ruffy91 on August 10, 2015, 09:14:31 amNot always.QuoteGiven the previous answer, I actually don't know.QuoteNot always.QuoteQuoteJust so, please don't code your own; you will get it wrong.Consider, for example the question: "in the UK, in what years dideveryday have 24 hours?"

author: tggzzz
author_rank: Super Contributor
timestamp: August 10, 2015, 09:34:46 am
content: I've just found out there's another question: "how many days are there in a week?"Nasty noises await those that answer "7".And as for the number of days in a year, it can vary from 353 to 385.

author: helius
author_rank: Super Contributor
timestamp: August 10, 2015, 09:52:40 am
content: Anything related to timezones is obviously mad and virtually certain to have bugs.But since unix time is defined in terms of UTC, and so does not increase monotonically over a leap second, leap seconds do not play a role in the conversion of the timestamp to a date in UTC. So the conversion is actually a lot simpler than it's sometimes described.

author: rolycat
author_rank: Super Contributor
timestamp: August 10, 2015, 10:13:34 am
content: Quote from: tggzzz on August 10, 2015, 09:34:46 amThere were 445 days in the year 46 BC.

author: tggzzz
author_rank: Super Contributor
timestamp: August 10, 2015, 10:52:45 am
content: Quote from: rolycat on August 10, 2015, 10:13:34 amAnd 355 in 1485/1752, depending on where you lived. Hence the rallying cry "give us back our 10 days".But I'm more interested in what happens now, or at least in living memory.

author: ElectricGuy
author_rank: Regular Contributor
timestamp: August 10, 2015, 03:37:00 pm
content: Thank you all for the answers, even the not constructives;Actually i've been using epoch time for some time, but using libraries, and i want to understand the algorithm to convert epoch time, thats why i asked it on the forum.I already found a way to convert human time to epoch time.My algorithm probably it's not the most elegant, but it works. The algorithm is thisGiven certain date and time 2015-10-08 16:04:30All calculations to integer. This is important!1 - Subtract 2015 - 1970 to obtain the years bettewen 1970 and current year2 - Divide the number of years obtained in 1 step by 4 to obtain the number of leap years beteween thos two dates;3 - Multiple the result of step 1 by 365 and add the result obtained in step 2. This way i obtain the number o days beteween the twoo dates with +1 day do leap years included;4 - check if the current year is leapyear or not;5 - Now i have to add the number of days in the current year. So if i'm in the 8 month, i will add the 7 previous months ( 31 + 28 + 31 + 30 + 31 + 30 + 31)6 - now the final calcultion is easy,epoch time = (multiply step 3 by 86400) + (multiply step 5 by 86400) + ( (day-1) * 86400 ) + (hour * 3600) + (mintes * 60) + secondsFor the above example;YearOffset = 2015 - 1970 = 45 yearsLeapYears = 45/4 = 11.25 => 11 leapyearsNumberofDays = 45*365 + 11 = 16436daysCurrent year its not leap year, so february = 28Month = 8, so NumberOfDaysUntilPreviousMonth = 31 + 28 + 31 + 30 + 31 + 30 + 31 = 212 daysepochtime = (16436 * 86400) + (212*86400) + ((10-1)*86400) + (16*3600) + (4*60) + 30 = 1439222670 => validated with the thousunds of online converters.Now, i want to work out, how to convert epochtime to human date!!!Any help?

author: tggzzz
author_rank: Super Contributor
timestamp: August 10, 2015, 03:50:01 pm
content: Quote from: ElectricGuy on August 10, 2015, 03:37:00 pmThe "not constructive" answers are there for a reason. Omitting to consider/understand them has caused many non-trivial programs and systems to subtly fail. If your program is non-trivial or important, don't ignore the corner cases.

author: miguelvp
author_rank: Super Contributor
timestamp: August 10, 2015, 03:50:50 pm
content: I'll just throw this in here:https://en.wikipedia.org/wiki/Leap_secondBut I'm not sure if unix time will compensate for this at all.Usually what I do is use the libraries like gmtime to fill a tm structure or mktime to go the other way.But you have to take into account get_timezone and getdstbias because the conversion will adjust your result depending on system settings.As for the algorithm to go the other way, well just reverse yours using modulus and divisions to get the pieces needed.Edit: unix time does take leap seconds into account, FWIW

author: Richard Crowley
author_rank: Super Contributor
timestamp: August 10, 2015, 03:52:01 pm
content: Simple division by 4 works for your limited range of interest.But it is not an accurate method over a wider range of values.I don't see how the number of days in a week has anything to do with this calculation?

author: ElectricGuy
author_rank: Regular Contributor
timestamp: August 10, 2015, 03:52:34 pm
content: Quote from: tggzzz on August 10, 2015, 03:50:01 pmI don't, thats why i "Thank you all for the answers, even the not constructives"

author: miguelvp
author_rank: Super Contributor
timestamp: August 10, 2015, 03:54:38 pm
content: Quote from: ElectricGuy on August 10, 2015, 03:52:34 pmI will say your phrase construct and your answer to someone that helped you is "not constructive"

author: ElectricGuy
author_rank: Regular Contributor
timestamp: August 10, 2015, 03:54:47 pm
content: Quote from: Richard Crowley on August 10, 2015, 03:52:01 pmGive an example that it doosn't work assuming a date between 1970 and 2038 ( epochtime 32bits )days of week, i don't get it, can you explain me?

author: ElectricGuy
author_rank: Regular Contributor
timestamp: August 10, 2015, 03:58:51 pm
content: Quote from: miguelvp on August 10, 2015, 03:50:50 pmYes, unix time takes care of the leapseconds.Timezone its not a big deal, because, for instance, my time zone is GMT +1All i need to do its add 3600 to unixtime. thats the advantage of working with unixtime.I can grabe the for insntance from a GPS ( UTC ), then convert it to unixtime, then add 3600 and then convert it to human time, and i get the time for my timezone, without any issue.

author: Richard Crowley
author_rank: Super Contributor
timestamp: August 10, 2015, 04:00:01 pm
content: Quote from: ElectricGuy on August 10, 2015, 03:54:47 pmDid you not understand the clause "for your limited range of interest"?This only works because the year 2000 happens to be evenly divisible by 400.The complete rule is...QuoteQuoteOthers here were claiming that number of days in a week had values other than 7.The only example of that I can recall was for a period of time in France, and then they reverted back to 7.But in any case, it is a moot point as the concept of "week" plays no part in the calculation.

author: ElectricGuy
author_rank: Regular Contributor
timestamp: August 10, 2015, 04:05:08 pm
content: Quote from: Richard Crowley on August 10, 2015, 04:00:01 pmOk, i understand now Richars. Thanks. Actually i translated my algorithm to C# and my calculation or the leap year is like yours;Code:if((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0)){leapyear = 1;}else{leapyear = 0;}Now... i'm struglind how to get the way around grrrrrrrr

author: tggzzz
author_rank: Super Contributor
timestamp: August 10, 2015, 04:05:39 pm
content: Quote from: Richard Crowley on August 10, 2015, 03:52:01 pmIt is a calendar issue - quite ignorable if you are only considering one calendar used by a small subset of the world.But if you need to use the calendar in use in the larger countries of the world, then you might benefit from understanding some of the horrors. See, for merely one example,https://en.wikipedia.org/wiki/Chinese_ten-day_week#Day.2C_hour.2C_and_weekandhttps://en.wikipedia.org/wiki/Chinese_ten-day_week#Solar_year_and_solar_term

author: Richard Crowley
author_rank: Super Contributor
timestamp: August 10, 2015, 04:19:37 pm
content: Quote from: tggzzz on August 10, 2015, 04:05:39 pmYour citation says....Quote

author: miguelvp
author_rank: Super Contributor
timestamp: August 10, 2015, 04:25:33 pm
content: I had to, recently, implement something similar to ISO 8601https://en.wikipedia.org/wiki/ISO_week_dateAnd it had to work world wide regardless of time setting and all in sync.Not terribly difficult but there are too many subtleties that made the task non-trivial at all.And I couldn't use strftime or I would have done so.

author: Mechanical Menace
author_rank: Super Contributor
timestamp: August 10, 2015, 06:08:58 pm
content: Quote from: tggzzz on August 10, 2015, 04:05:39 pmThat one calender is the standard everywhere for all but religious and/or ceremonial uses. 100% of the world is not a small subset.

author: ElectricGuy
author_rank: Regular Contributor
timestamp: August 10, 2015, 07:03:27 pm
content: Now the Epoch to Human convertion;second = epochtime % 86400;epochtime /= 86400;hour = second / 3600;minute = second / 60 % 60;second = second % 60;int x = (epochtime * 4 + 102032) / 146097 + 15;int b = epochtime + 2442113 + x - (x / 4);int c = (b * 20 - 2442) / 7305;int d = b - 365 * c - c / 4;int e = d * 1000 / 30601;day = d - e * 30 - e * 601 / 1000;if (e < 14){year = c - 4716;month = e - 1;}else{year = c - 4715;month = e - 13;}With help of some codes and ideias on the web.

author: Jeroen3
author_rank: Super Contributor
timestamp: August 10, 2015, 07:11:22 pm
content: Be aware that anything you program now using unix timestamps might get in trouble around 2038.https://en.wikipedia.org/wiki/Year_2038_problem

author: tggzzz
author_rank: Super Contributor
timestamp: August 10, 2015, 07:14:20 pm
content: Quote from: Mechanical Menace on August 10, 2015, 06:08:58 pmReally? I dare you to go and shout that in some significant countries!

author: ElectricGuy
author_rank: Regular Contributor
timestamp: August 10, 2015, 07:18:40 pm
content: Quote from: Jeroen3 on August 10, 2015, 07:11:22 pmYes, i know. This post it was just to understand the convertion algorithm.But by the way, if they change the 32bit time to 64bit time, that will give us time probably beind the end of universe.https://youtu.be/QJQ691PTKsA

author: Richard Crowley
author_rank: Super Contributor
timestamp: August 10, 2015, 07:20:24 pm
content: Quote from: tggzzz on August 10, 2015, 07:14:20 pmSince the most populous country on the planet (China) uses the same 7-day calendar that you and I use, what exactly to you mean by "significant countries"?

author: hamster_nz
author_rank: Super Contributor
timestamp: August 11, 2015, 10:33:21 am
content: Quote from: miguelvp on August 10, 2015, 03:50:50 pmI'm not so sure - NTP has bugs likehttp://www.pcworld.com/article/2942992/leap-second-causes-internet-hiccup-particularly-in-brazil.htmlI know for sure that the Windows API is broken:https://msdn.microsoft.com/en-us/library/windows/desktop/ms724950(v=vs.85).aspxQuoteThis means that valid UTC leap seconds are not compatible with the Windows SYSTEMTIME structure definitions - shrug.

author: sleemanj
author_rank: Super Contributor
timestamp: August 11, 2015, 11:59:02 am
content: Quote from: Richard Crowley on August 10, 2015, 07:20:24 pmJapan for example, while technically they switched to the Gregorian calendar back in the late 1800's, in practice amongst the population and even local governments the traditional Japanese calendar is still used, of course the public can use gregorian dates, but for example if you're looking for stuff in Japan by year then you might need to look for the Japanese date.I ran into this myself when I had to buy some parts for my very very very JDM vehicle once, I had to convert 1996 into the Japanese date (8th year of Heisei for what it matters) in order to determine if the parts where for the correct year, because at least some of the ads (on Yahoo JP auctions) used Japanese dates.

author: Mechanical Menace
author_rank: Super Contributor
timestamp: August 11, 2015, 02:57:37 pm
content: Quote from: sleemanj on August 11, 2015, 11:59:02 amWhat year is it officially? Which date do manufacturers stamp on things? If you went to a shop instead of an auction site would you have had that problem? If I was going to make an appointment in Japan for next Thursday would I have to convert that? At midday on the 11th of August 2015 was it a Tuesday?

author: Mechanical Menace
author_rank: Super Contributor
timestamp: August 12, 2015, 09:00:27 am
content: Quote from: mojo-chan on August 12, 2015, 08:02:36 amNope, officially it's 2015, ceremonially it's 27.

author: tggzzz
author_rank: Super Contributor
timestamp: August 13, 2015, 09:14:34 am
content: Quote from: ElectricGuy on August 10, 2015, 01:16:17 amAs others have pointed out, this can be surprisingly subtle. One way to gutturally understand that is to realise that the definitive way of doing it is ever-changing. Typically there are about 10 changes per year: see the entries inftp://ftp.iana.org/tz/releases/and realise that *2014j* is the 10th update in 2014. That can cause problems for devices/systems that have long lifetimes, especially embedded devices.For merelyonecountry andoneaspect of their time, have a look athttp://www.npl.co.uk/educate-explore/what-is-time/archive-of-summer-time-dates-1916-2006You may findhttps://www.iana.org/time-zones/repository/tz-link.htmlhelpful, as well as a few coding traps mentioned inhttp://www.exit109.com/~ghealton/y2k/yrexamples.html#_BadGuysWhether or not any of that matters to you is application dependent, of course.

author: codeboy2k
author_rank: Super Contributor
timestamp: August 14, 2015, 01:26:32 am
content: Quote from: mojo-chan on August 13, 2015, 01:59:27 pmAgreed. For example, a sprinkler controller wants to turn on the sprinkler at 6 am everyday, it doesn't care about leap seconds, and maybe hard-codes the current DST changes for the market it's being sold into. Minor deviations over the expected lifetime of the controller is not a big deal here and the owner/manager can correct the time if it gets out of sync with the actual time.

author: Richard Crowley
author_rank: Super Contributor
timestamp: August 14, 2015, 01:51:34 am
content: Quote from: codeboy2k on August 14, 2015, 01:26:32 amIt doesn't even care about leap-YEARS. Not does it care about the numeric day of the month, or the month of the year, for that matter.But it DOES care about the day of the week which, fortunately, is perhaps the only reliable constant here.

author: tggzzz
author_rank: Super Contributor
timestamp: August 14, 2015, 08:05:48 am
content: Quote from: Richard Crowley on August 14, 2015, 01:51:34 am... and only requires a modulo 7 (or maybe 86400 or 19817481600) counter!