Time.zone() and Time.local()

Hi Everyone,
My photon is not printing the correct time here in Australia.
We are in the Greenwich Mean Time + 10 zone.

I tried the following in my Setup() function:

None of these worked.

Here are the important parts of my code:

void setup() {
void loop() {
   Serial.println(Time.format(currLocTime, TIME_FORMAT_ISO8601_FULL));

It outputs something like this:
But I wanted:

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.

Thanks for any input :grinning:

1 Like

Z is Zulu, aka UTC, aka GMT. (okay, they aren’t quite the same, but close enough)

So that wasn’t PM. Otherwise I don’t know what to tell you- lurking. It should work, I think.

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

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)


Bravo Zulu thanks

Ah, that makes sense. That’s exactly what seemed to be happening, the offset being added twice.

Thanks very much Scruffr! Very illuminating :flashlight:

1 Like

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.

Serial.println(Time.format(Time.now(), "%Y-%m-%d %H:%M:%S"));

Thanks, but how does that code get the actual time? I only see a bunch of format characters.

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.

1 Like

Thanks rickkas7.

Ah so if I supply only the one argument, it defaults to assuming I want the current time. Thanks @ScruffR

1 Like

When I do:

void loop(){

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).

plus there is a method to block until the time is valid… coincidentally called isValid().


Works :slight_smile: Thanks.

1 Like

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?

Hi @archr,

Checkout https://docs.particle.io/tutorials/integrations/google-maps/ or https://www.hackster.io/middleca/cellular-geolocation-using-your-cell-tower-and-google-b83534 for geolocation examples.


1 Like

I did try the https://docs.particle.io/tutorials/integrations/google-maps/ tutorial. I get a error status 404.

Hi @archr,

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.