It outputs something like this:
2016-05-21T00:14:56+10:00
But I wanted:
2016-05-21T16:14:56
I am trying to put this string into a MySQL database DateTime. Another problem is that it saves in the MySQL DateTIme as 00:00:000:00etc.
With Time.zone(10);, I got 10 hours AHEAD of local time using Time.local()
So is Time.zone necessary?
When I took it out I got the wrong time too.
All to odd for me to work out what is going on I’m afraid.
Anyone else come up against this? -Apart from the guy who fixed it by flashing to the basic LED program… doesn’t seem like a proper fix to me.
Also, at one point it said 4:30Z - is the Z some sort of symbol for PM?
because that would have been the correct time.
Lastly, I have the latest firmware from the CLI running on the Photon as of 2 days ago.
Time.format() expects you to pass Time.now() (or a UTC timestamp) and not your local time, since it internally uses the set Time.zone() and if you provide Time.local() you’ll end up having that offset added twice.
So you’d either use Serial.println(Time.format(Time.now(), TIME_FORMAT_ISO8601_FULL)); or even better leave the timestamp away completely, since the current time will be taken by default.
// this will just do the job
Serial.println(Time.format(TIME_FORMAT_ISO8601_FULL));
Z stands for Zone or Zero or Zulu and is the UTC convention to indicate NO time zone offsets (equals to YYYY-MM-DDThh:mm+00:00)
What ScruffR said. Also, the standard format for DATETIME in MySQL is %Y-%m-%d %H:%M:%S, which looks like “2016-05-20 11:23:30”. Depending on how you’re getting the data into your database, that may or may not be an easier format than TIME_FORMAT_ISO8601_FULL. It’s UTC time, so use Time.now(), not Time.local(), as well.
Sorry, I forgot the Time.now() parameter. I corrected it above. The Time.format method converts the time in the first parameter using the format specified in the second parameter. The format specifier works the same as the Unix-like function strftime.
@Lindsay, that Time.now() is not required since there is an overload that just takes one string as format parameter and uses the current time to convert.
Hence what I said above
Consequently @rickkas7's original code was correct after all
Serial.println(Time.format("%Y-%m-%d %H:%M:%S"));
You are working with a Time object whos intrinsic property is the current time, so whatever method you use will have access to this time value.
I get 1999-11-30T17:00:30-07:00 until the Electron connects to the internet and then I start getting valid readings. I have multithreading enabled and my zone is set to Time.zone(-7) in setup().
I do get an accurate result once connected to internet. But how can I get the right time at particle startup?!
Once the RTC gets a correct reading it will maintain the correct time as long the battery stays connected (or you keep VBAT powered by any other means).
Is there a way for the Electron to automatically detect the timezone and print out a valid timestamp (without using Time.zone)?
I read this thread, but couldn’t make much sense of it.
Also, is there a way to get geolocation (maybe from network triangulation) of an Electron?
You can send that same Browser Location request to any number of geolocation services if the Google integration isn’t working. I’ve just learned that the cloud team has deployed a fix to make the Google Maps Integration more reliable, so I’d try it again now.
If you’re using a 3G electron, you can also use the cell locate feature on the ublox module.