Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Particle Time Functions Demo
unsigned long uptime;
time_t t;
void setup() {
Serial.begin();
delay(5000);
Serial.println("==============================");
Serial.println(" Particle Time Demo");
Serial.println("==============================");
// Use millis() to see how long your device has been turned on
// You could also use micros() to get the same infomration in
// microseconds
uptime = millis();
Serial.printlnf("Device has been up for %u milliseconds.", uptime);
// In AUTOMATIC mode the time will always be synced by this point
// since the device will have already contacted the Particle Cloud
// In non-AUTOMATIC mode though you can check if the time is synched
if(Time.isValid())
{
Serial.println("The time is valid.");
}
// Easy access functions for the parts of the current time
Serial.printlnf("\nThe current hour of the day (0-23) is %d", Time.hour());
// The 12 hour version
Serial.printlnf("\nThe current hour of the day (1-12) is %d", Time.hourFormat12());
Serial.printlnf("Year: %d, Month(1-12): %d, Day(1-31): %d, Hour(0-23): %d, Minute(0-59): %d, Second(0-59): %d", Time.year(), Time.month(), Time.day(), Time.hour(), Time.minute(), Time.second());
Serial.printlnf("\nBut wait, that's all in UTC time. Right now it's %u", Time.now());
// Set the local time zone offset
Time.zone(-7); // -7 is for Pacific Daylight Time
Serial.printlnf("\nMy local time is: %u", Time.local());
Serial.println("Ugh, I still can't understand that giant number. Let's try formatting.");
// UTC time.
Serial.println("\nCurrent UTC time is:");
Serial.println(Time.timeStr(Time.now()));
Serial.println("\nCurrent local time is:");
Serial.println(Time.timeStr(Time.local()));
Serial.println("Or we could use the format() function.");
t = Time.local();
// Time.format() returns a String object and not a c style string
Serial.println("\nCurrent local time with TIME_FORMAT_DEFAULT is:");
Serial.println(Time.format(t, TIME_FORMAT_DEFAULT));
Serial.println("\nCurrent local time with TIME_FORMAT_ISO8601_FULL is:");
Serial.println(Time.format(t, TIME_FORMAT_ISO8601_FULL));
// Or we can use a custom format. For a full reference for formatting options see
// http://en.cppreference.com/w/c/chrono/strftime
Serial.println("\nCurrent local time with custom format is:");
Serial.println(Time.format(t, "%A %B %d%n%T"));
uptime = millis();
Serial.printlnf("\nNow the device has been up for %u milliseconds.", uptime);
}
void loop() {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.