3

I am playing around with NodeMCU on an ESP8266. I have a Date String and a Time String from a Web Request like this:

15.07.16 (German format DD.MM.YY)

19:50 (24 hours format)

These DateTimes lay usually a little bit in the future. I want to get the number of minutes from the current time to the time from my strings above.

I guess I have to create a time object from the strings and then compare it to the current time. But how can I do that with Lua?

Unfortunately there is no os Library on NodeMCU (or I might have missed how to enable it).

Calculating the difference manually would be a huge pain which I would like to avoid. Does anyone know a way to compute that with available or external libraries?

Thanks for any support!

dda
  • 6,030
  • 2
  • 25
  • 34
amiroo
  • 91
  • 10
  • www.google.com enter "lua time". Open the first result... if you want to use Lua I highly recommend to at least read the language reference which covers both topics. It will answer many upcoming questions as well. – Piglet Jul 15 '16 at 19:58
  • Thank you for your quick answer. I already did that of course. But on my ESP8266 there is no **os**, there also no **os.time** – amiroo Jul 15 '16 at 20:07
  • Can this be closed? – Marcel Stör Aug 20 '16 at 20:41

2 Answers2

4

There's a pending PR for rtctime that does the exact opposite, Unix epoch to UTC calendar.

If you convert your strings to a Unix epoch X you could do

-- delta in minutes
local delta = (X - rtctime.get()) / 60

You can either calculate X yourself, which is far from trivial due to leap years & seconds and other date/time oddities, or your can send a request to http://www.convert-unix-time.com/api?date=15.07.2016%2019:50&timezone=Vienna&format=german and extract the timestamp from it.

Marcel Stör
  • 22,695
  • 19
  • 92
  • 198
  • Thank you Marcel! This looks promising. Simple and straight-forward. I just noticed, that my firmware is not compiled with rtctime support. So I will try that tomorrow and let you know. – amiroo Jul 15 '16 at 22:19
  • Think about floating point support in the firmware for the range of `X`. – Marcel Stör Jul 15 '16 at 22:26
  • I just made a custom build with rtctime support. Do you have an idea, why rtctime.get() returns 0,0 for me? – amiroo Jul 15 '16 at 22:34
  • I found this.... "When the ESP8266 starts up, the Realtime Clock (RTC) is not initialized. If your IoT project has the need to provide timestamps in its generated data, then your code will need to fetch the current datetime from a server after the network is available." So RTC is useless for me. But what I can do i sending a request for the x and one for the currentTime (which I get as string in some http headers earlier) and then simply subtract the timestamp. This will take some more http requests, but I can't see a better way to do this. – amiroo Jul 15 '16 at 22:47
  • 1
    Go directly to the source and read the intro to the rtctime module to learn about its modus operandi. It explains how to use it in combination with the SNTP module to sync time. As an alternative you could send an HTTP HEAD request to a trusted server and parse the timestamp from the response header. Jason has a nice blog post about that: http://blog.falafel.com/set-current-datetime-nodemcu/ – Marcel Stör Jul 16 '16 at 06:16
  • Oh, didn't realize your quoted from Jason's blog, sorry. – Marcel Stör Jul 17 '16 at 21:28
  • I can't edit my old comment but that link is now stale as falafel.com appears to have closed shop. Wayback machine to the rescue: https://web.archive.org/web/20160601003733/http://blog.falafel.com/set-current-datetime-nodemcu/ – Marcel Stör Sep 15 '19 at 20:44
1

First you get the numbers from the strings using Lua's string library:

https://www.lua.org/pil/20.html

https://www.lua.org/manual/5.3/manual.html#6.4

Then you do the time calculations using Lua's os library:

https://www.lua.org/pil/22.1.html

https://www.lua.org/manual/5.3/manual.html#6.9

I won't give you more information as you did not show any own effort to solve the problem.

Addon:

As you don't have the os library (didn't know that) you can simply calculate that stuff yourself.

Get the month, year hour and minute number from the strings using string.sub or string patterns.

Then simply calculate the time difference. You know how many days each month has. You know how many minutes per hour and how many hours per day. Determine if the year is a leap year (if you don't know how: https://support.microsoft.com/en-us/kb/214019)

Piglet
  • 27,501
  • 3
  • 20
  • 43
  • Sorry, I should have said, that I use nodeMCU. There is no OS Library on the firmware. – amiroo Jul 15 '16 at 20:08
  • 1
    btw no need to downvote me, just because you believe I did not show any effort to solve it on my own. I provided all needed information except that I am using nodeMCU. Your answer was arrogant and not helpful at all for someone on an ESP. – amiroo Jul 15 '16 at 20:22
  • your question does not meet the recommendations for a good question on SO. so I downvote it. simple as that. Read [ask] – Piglet Jul 15 '16 at 20:42
  • 1
    @Piglet I've no desire to engage in a flame war about good/bad style on SO. Therefore, this is the only comment I'll leave about that. Except for the missing `NodeMCU` keyword the question is totally ok IMO. I wish people new to SO were welcomed with a little more sympathy and friendly guidance. – Marcel Stör Jul 15 '16 at 21:33
  • @Piglet Thank you anyways. I am looking for a way to compute that. No way I would do that manually. – amiroo Jul 15 '16 at 22:11
  • @MarcelStör In my opinion the votes exist to encourage people to improve their questions and in the end show others what is good and bad. I downvoted him because he just asked for a ready solution without showing any efforts himself. Now that he included some extra info on the os library I know he tried something himself so I upvoted his question. But you are right I might have given a few more hints on how he could improve it rather than just giving him [ask] – Piglet Jul 16 '16 at 13:42