I have a program that requires UTC time, in order to make correct calculations, however on my LCD I would like to be able to display "human" time, i.e. the normal Local timezone (and even auto DST adjust if possible, but not a big deal). I Havent been able to identify a straightforward way to have a secondary timekeeping string in the same sketch. can this be done?
from the playground - Arduino Playground - Time
time_t t = now(); // store the current time in time variable t
hour(t); // returns the hour for the given time t
minute(t); // returns the minute for the given time t
second(t); // returns the second for the given time t
day(t); // the day for the given time t
weekday(t); // day of the week for the given time t
month(t); // the month for the given time t
year(t); // the year for the given time t
thanks for the reply robtillaart. This is the code I'm using for the time set and I do understand how that works, but what I am trying to figure is, can I have the Arduino tracking UTC time (as I am now), but do an offset so that the variable I use to display time on the LCD is actually local time? UTC time is used for all my calculations inside Arduino, and the only use of local time will be for display. Can I just do something like a localHour = ((hour()-8); then output localHour to my LCD for the shift to Pacific time?
Can I just do something like a localHour = ((hour()-8); then output localHour to my LCD for the shift to Pacific time?
As long as you correct for localHour going negative, yes. -3:30 is going to look a little strange, early in the morning.
Why don't you just adjust the time_t variable for the timezone. Then you can use all the regular methods, but pass in the local time.
time_t utc, local;
int localHour;
utc = now();
local = utc - 8 *60 * 60; // Local time 8 hours behind UTC.
localHour = hour(local);
Iain
Thanks iain this looks like what I'm after. I just came out of surgery a couple hours ago, so when I can think clearly enough to do some coding ill try this out
EDIT: Hey guys, wow this is extremely, old! Life got busy, but I'm back to my Arduino. The above code was exactly what I needed to correct to local time. How can I do the same kind of offset for the date? For example, right now (11:20pm Pacific time), it shows the correct time, however since I am only offsetting the hour, it is still calculating the date based on UTC, and therefore the date shows tomorrow (8/10/2011... instead of 8/9/2011).
EDIT EDIT: Okay, some tinkering, I figured it out. Below is my whole function in case this can help someone. Basically the deal is, I needed to display "Human" time, while the Arduino still kept UTC time for its' actual functions:
The only other thing that would really kick ass is if someone could tell me how I could have it automatically adjust for daylight savings, instead of me just manually changing the offset between 7 and 8 twice a year :)
void LCDprintTime() {
time_t utc, local;
int localHour;
int localDay;
int localMonth;
int localYear;
utc = now();
local = utc - 7 *60 * 60; // Local time 8 hours behind UTC (7 during summer)
localHour = hour(local);
localDay = day(local);
localMonth = month(local);
localYear = year(local);
lcd.setCursor(0,2);
if(localHour <10)
lcd.print("0");
if(localHour >12)
lcd.print(localHour-12);
else
lcd.print(localHour);
lcd.print(":");
if(minute() <10)
lcd.print("0");
lcd.print(minute());
lcd.print(":");
if(second() <10)
lcd.print("0");
lcd.print(second());
if(localHour <12)
lcd.print("am ");
if(localHour >=12)
lcd.print("pm ");
lcd.print(localMonth);
lcd.print("/");
lcd.print(localDay);
lcd.print("/");
lcd.print(localYear);
lcd.print(" ");
}