Converting Utc Hour To Local Time Hour

I am currently building a gps clock with 7 segment displays and I have the utc hour from the gps stored in the variable "hr" how would I convert this to local time? (I live in +6 Central Time Zone but I would like to be able to set it to any time zone). Every thing i have found requires a library to handle the time clock when i just want to do that with the gps.. Any help is appreciated! (I can post the code if you like but I've given pretty much all the info you need here.)

Thanks,

Christopher aka cmk20

What happens when you just add 6 to the hour? Also modulus it with 24 to get past the zero hour.

cmk20:
I can post the code if you like but I've given pretty much all the info you need here.

It would be useful to have a date and time variable declaration example and some sample data.

If it is just about time (and not date) you just can add 6 hours and do the hours%24 to get the local time hour.

Besides of that: Many countries use "Daylight Saving Time" (summertime) during the summer months. If you want to have valid "local time" considering summertime setting, you also would have to tell about the summertime rule in your country (if there is one), for developing a function.

cmk20:
(I live in +6 Central Time Zone but I would like to be able to set it to any time zone).

Is your "Central Time Zone" in the United States, or in some other country?

The Central Time Zone in the United States is 6 hours behind UTC.

Please post your code. It would be useful to see what you have.

Thanks for your replies, I manged to fix it myself... (I missed most of the replies as my emil notifications werent configured... also yes I realize central time is -6)
Heres the code:

    mn = mn + timezonemn;
    if (mn > 59) {
      mn = mn - 60;
      hr = hr + 1;
    }
    else {
      if (mn < 0) {
        mn = mn + 60;
        hr = hr - 1;
      }
    }
    hr = hr + timezonehr;
    if (hr > 23) {
      hr = hr - 24;
    }
    else {
      if (hr < 0) {
        hr = hr + 24;
      }
    }

I have to variables timezonehr and timezonemn for both minute and hour offsets, i add them to the hr or mn variables and then check if they went over... pretty simple

cmk20:
I realize central time is -6)

OK, so if your local time calculation is for the Canadian province of Saskatchewan, you are done. This province in Canada is at Central Standard Time all through the year.

In other areas CST might switch to Central DayLight Time CDT during the summer months (1 hour forward), and in case you want to calculate CDT correctly, you would have to do additional date calculations.

This province in Canada is at Central Standard Time all through the year.

Sensible people!

There is hardly any nationwide practice more wasteful and idiotic than setting/resetting clocks to Daylight Savings Time. At the institution where I work, they actually have two complete sets of clocks, so that twice a year the janitorial staff can replace all the clocks, rather than fiddling with each one.

cmk20:
Thanks for your replies, I manged to fix it myself... (I missed most of the replies as my emil notifications werent configured... also yes I realize central time is -6)
Heres the code:

    mn = mn + timezonemn;

if (mn > 59) {
      mn = mn - 60;
      hr = hr + 1;
    }
    else {
      if (mn < 0) {
        mn = mn + 60;
        hr = hr - 1;
      }
    }
    hr = hr + timezonehr;
    if (hr > 23) {
      hr = hr - 24;
    }
    else {
      if (hr < 0) {
        hr = hr + 24;
      }
    }




I have to variables timezonehr and timezonemn for both minute and hour offsets, i add them to the hr or mn variables and then check if they went over... pretty simple

What type are hr and mn? Are you using bytes or ints for them?

odometer:
What type are hr and mn? Are you using bytes or ints for them?

There int's heres the final code

#include <TinyGPS++.h>
TinyGPSPlus gps;
int d1a = 12;
int d1b = 11;
int d1c = 10;
int d1d = 9;
int d2a = 7;
int d2b = 6;
int d2c = 5;
int d2d = 4;
int d3a = 22;
int d3b = 23;
int d3c = 24;
int d3d = 25;
int d4a = 26;
int d4b = 27;
int d4c = 28;
int d4d = 29;
int ampmled = 30;
int hr = 0;
int mn = 0;
int timezonehr = -6; //Timezone hour offset
int timezonemn = 0;  //Timezone minute offset
int numbers[][4] =   //Display codes
{
  {0,0,0,0}, // 0
  {0,0,0,1}, // 1
  {1,0,0,0}, // 2
  {1,0,0,1}, // 3
  {0,1,0,0}, // 4
  {0,1,0,1}, // 5
  {1,1,0,0}, // 6
  {1,1,0,1}, // 7
  {0,0,1,0}, // 8
  {0,0,1,1}, // 9
  {1,1,1,1}  // Clear
};
int num[4];
void disp(int dispnum,int numtodisplay){
    for (int x = 0; x < 4; x++){
      num[x] = numbers[numtodisplay][x];
    }
    if (dispnum == 1){
     if (num[0] == 1){
       digitalWrite(d1a, HIGH);
     }
     else{
       digitalWrite(d1a, LOW);
     }
     if (num[1] == 1){
       digitalWrite(d1b, HIGH);
     }
     else{
       digitalWrite(d1b, LOW);
     }
     if (num[2] == 1){
       digitalWrite(d1c, HIGH);
     }
     else{
       digitalWrite(d1c, LOW);
     }
     if (num[3] == 1){
       digitalWrite(d1d, HIGH);
     }
     else{
       digitalWrite(d1d, LOW);
     }
    }
    if (dispnum == 2){
     if (num[0] == 1){
       digitalWrite(d2a, HIGH);
     }
     else{
       digitalWrite(d2a, LOW);
     }
     if (num[1] == 1){
       digitalWrite(d2b, HIGH);
     }
     else{
       digitalWrite(d2b, LOW);
     }
     if (num[2] == 1){
       digitalWrite(d2c, HIGH);
     }
     else{
       digitalWrite(d2c, LOW);
     }
     if (num[3] == 1){
       digitalWrite(d2d, HIGH);
     }
     else{
       digitalWrite(d2d, LOW);
     }
    }
    if (dispnum == 3){
     if (num[0] == 1){
       digitalWrite(d3a, HIGH);
     }
     else{
       digitalWrite(d3a, LOW);
     }
     if (num[1] == 1){
       digitalWrite(d3b, HIGH);
     }
     else{
       digitalWrite(d3b, LOW);
     }
     if (num[2] == 1){
       digitalWrite(d3c, HIGH);
     }
     else{
       digitalWrite(d3c, LOW);
     }
     if (num[3] == 1){
       digitalWrite(d3d, HIGH);
     }
     else{
       digitalWrite(d3d, LOW);
     }
    }
    if (dispnum == 4){
     if (num[0] == 1){
       digitalWrite(d4a, HIGH);
     }
     else{
       digitalWrite(d4a, LOW);
     }
     if (num[1] == 1){
       digitalWrite(d4b, HIGH);
     }
     else{
       digitalWrite(d4b, LOW);
     }
     if (num[2] == 1){
       digitalWrite(d4c, HIGH);
     }
     else{
       digitalWrite(d4c, LOW);
     }
     if (num[3] == 1){
       digitalWrite(d4d, HIGH);
     }
     else{
       digitalWrite(d4d, LOW);
     }
    }
}
void setup() {
  // put your setup code here, to run once:
  pinMode(d1a, OUTPUT);
  pinMode(d1b, OUTPUT);
  pinMode(d1c, OUTPUT);
  pinMode(d1d, OUTPUT);
  pinMode(d2a, OUTPUT);
  pinMode(d2b, OUTPUT);
  pinMode(d2c, OUTPUT);
  pinMode(d2d, OUTPUT);
  pinMode(d3a, OUTPUT);
  pinMode(d3b, OUTPUT);
  pinMode(d3c, OUTPUT);
  pinMode(d3d, OUTPUT);
  pinMode(d4a, OUTPUT);
  pinMode(d4b, OUTPUT);
  pinMode(d4c, OUTPUT);
  pinMode(d4d, OUTPUT);
  pinMode(ampmled, OUTPUT);
  Serial.begin(9600);
  Serial3.begin(9600);
}
uint32_t timer = millis();
void loop() {
  // put your main code here, to run repeatedly:
  while (Serial3.available()) {
      gps.encode(Serial3.read());
  }
  if (timer > millis())  timer = millis();
  if (millis() - timer > 500) { 
    timer = millis();
    hr = gps.time.hour();
    mn = gps.time.minute();
    //hr = 0; //DEBUG
    //mn = 0; //DEBUG
    mn = mn + timezonemn;
    if (mn > 59) {
      mn = mn - 60;
      hr = hr + 1;
    }
    else {
      if (mn < 0) {
        mn = mn + 60;
        hr = hr - 1;
      }
    }
    hr = hr + timezonehr;
    if (hr > 23) {
      hr = hr - 24;
    }
    else {
      if (hr < 0) {
        hr = hr + 24;
      }
    }
    if (hr > 12) {
      hr = hr - 12;
      digitalWrite(ampmled, LOW);
    }
    else {
      digitalWrite(ampmled, HIGH);
    }
    if (hr == 0) {
      hr = 12;
    }
    disp(1, int(hr / 10));
    disp(2, int(hr % 10));
    disp(3, int(mn / 10));
    disp(4, int(mn % 10));
  }
}

I am using 7447 7 segment drivers to make the code easier. Here's a picture:

Is there any current limiting between the 7447A(40mA sink current) or 74LS47(24mA sink current) and the segments of the displays? Good chance you'll burn them out if not.

CrossRoads:
Is there any current limiting between the 7447A(40mA sink current) or 74LS47(24mA sink current) and the segments of the displays? Good chance you'll burn them out if not.

Yes there are resisters on the cathode of each of the displays. I have run the clock for 24 hrs without any problems.

I think you meant Anode. 1 per common anode is better than nothing. If you see brightness variation dependent on the amount of segments that are on, the 1 resistor is the reason.

CrossRoads:
I think you meant Anode. 1 per common anode is better than nothing. If you see brightness variation dependent on the amount of segments that are on, the 1 resistor is the reason.

Hmm.... OK that's the reason they dim, its not too bad its only noticeable in a very dark room.

I Am Built A Gps Clock About A Year Ago, The Clock Works However I Am Tired Of Changing The Code Every Dst Change. I Have Looked On The Internet For Answers However I Cant Find Anything That Would Work With My Code :frowning:

Here's A Picture Of The Clock:

Here Is The Code:

#include <TinyGPS++.h>
TinyGPSPlus gps;
const int timezonehr = -5; //Timezone hour offset
const int timezonemn = 0; //Timezone minute offset
const int d1a = 12; //Lcd Controller Pin Definitions
const int d1b = 11;
const int d1c = 10;
const int d1d = 9;
const int d2a = 7;
const int d2b = 6;
const int d2c = 5;
const int d2d = 4; 
const int d3a = 22;
const int d3b = 23;
const int d3c = 24;
const int d3d = 25;
const int d4a = 26;
const int d4b = 27;
const int d4c = 28;
const int d4d = 29;
const int ampmled = 30; //Am/Pm Led Pin
const int numbers[][4] = //Display Codes
{
  {0,0,0,0}, // 0
  {0,0,0,1}, // 1
  {1,0,0,0}, // 2
  {1,0,0,1}, // 3
  {0,1,0,0}, // 4
  {0,1,0,1}, // 5
  {1,1,0,0}, // 6
  {1,1,0,1}, // 7
  {0,0,1,0}, // 8
  {0,0,1,1}, // 9
  {1,1,1,1}  // Clear
};
int num[4];
int hr = 0;
int mn = 0;
int oldmn = 0;
void disp(int dispnum,int numtodisplay){ //Function That Displays A Number On A Seleced Display
    for (int x = 0; x < 4; x++){ //Loop Through The Number Codes And Store The Correct One In The List Num
      num[x] = numbers[numtodisplay][x];
    }
    if (dispnum == 1){ //Use A Large If Tree To Display The Number Code Stored In Num
     if (num[0] == 1){
       digitalWrite(d1a, HIGH);
     }
     else{
       digitalWrite(d1a, LOW);
     }
     if (num[1] == 1){
       digitalWrite(d1b, HIGH);
     }
     else{
       digitalWrite(d1b, LOW);
     }
     if (num[2] == 1){
       digitalWrite(d1c, HIGH);
     }
     else{
       digitalWrite(d1c, LOW);
     }
     if (num[3] == 1){
       digitalWrite(d1d, HIGH);
     }
     else{
       digitalWrite(d1d, LOW);
     }
    }
    if (dispnum == 2){
     if (num[0] == 1){
       digitalWrite(d2a, HIGH);
     }
     else{
       digitalWrite(d2a, LOW);
     }
     if (num[1] == 1){
       digitalWrite(d2b, HIGH);
     }
     else{
       digitalWrite(d2b, LOW);
     }
     if (num[2] == 1){
       digitalWrite(d2c, HIGH);
     }
     else{
       digitalWrite(d2c, LOW);
     }
     if (num[3] == 1){
       digitalWrite(d2d, HIGH);
     }
     else{
       digitalWrite(d2d, LOW);
     }
    }
    if (dispnum == 3){
     if (num[0] == 1){
       digitalWrite(d3a, HIGH);
     }
     else{
       digitalWrite(d3a, LOW);
     }
     if (num[1] == 1){
       digitalWrite(d3b, HIGH);
     }
     else{
       digitalWrite(d3b, LOW);
     }
     if (num[2] == 1){
       digitalWrite(d3c, HIGH);
     }
     else{
       digitalWrite(d3c, LOW);
     }
     if (num[3] == 1){
       digitalWrite(d3d, HIGH);
     }
     else{
       digitalWrite(d3d, LOW);
     }
    }
    if (dispnum == 4){
     if (num[0] == 1){
       digitalWrite(d4a, HIGH);
     }
     else{
       digitalWrite(d4a, LOW);
     }
     if (num[1] == 1){
       digitalWrite(d4b, HIGH);
     }
     else{
       digitalWrite(d4b, LOW);
     }
     if (num[2] == 1){
       digitalWrite(d4c, HIGH);
     }
     else{
       digitalWrite(d4c, LOW);
     }
     if (num[3] == 1){
       digitalWrite(d4d, HIGH);
     }
     else{
       digitalWrite(d4d, LOW);
     }
    }
}
void setup() {
  pinMode(d1a, OUTPUT); //Set The Display Pins As Outputs
  pinMode(d1b, OUTPUT);
  pinMode(d1c, OUTPUT);
  pinMode(d1d, OUTPUT);
  pinMode(d2a, OUTPUT);
  pinMode(d2b, OUTPUT);
  pinMode(d2c, OUTPUT);
  pinMode(d2d, OUTPUT);
  pinMode(d3a, OUTPUT);
  pinMode(d3b, OUTPUT);
  pinMode(d3c, OUTPUT);
  pinMode(d3d, OUTPUT);
  pinMode(d4a, OUTPUT);
  pinMode(d4b, OUTPUT);
  pinMode(d4c, OUTPUT);
  pinMode(d4d, OUTPUT);
  pinMode(ampmled, OUTPUT);
  //Serial.begin(9600); //DEBUG
  Serial3.begin(9600); //Connect To The Gps Via Serial3
}
void loop() {
  while (Serial3.available()) { //If Data Is Available Form The Gps
      gps.encode(Serial3.read()); //Pull Data From The Gps, Then Send It To TinyGps++
  }
  if (gps.time.minute() != oldmn) { //If The Minute Changes Update The Display
    oldmn = gps.time.hour(); //Store The Current Minute In oldmin To Be Compared To Later
    hr = gps.time.hour(); //Pull The Time From TinyGps++
    mn = gps.time.minute();
    //hr = 0; //DEBUG
    //mn = 0; //DEBUG
    //The Rest Of The Code Changes The Time Zone, Converts To 12 Hour Am/Pm Time Then Updates The Am/Pm Led And The Displays
    mn = mn + timezonemn; //Add The Minute Time Zone Offset
    if (mn > 59) { //If The Minute Is Over 59 (From The Time Zone Conversion)
      mn = mn - 60; //Subtract 60 From It
      hr = hr + 1; //Then Add 1 To The Hour
    }
    else {
      if (mn < 0) { //If Minute Is Less Than 0, Do The Inverse Of Above
        mn = mn + 60;
        hr = hr - 1;
      }
    }
    hr = hr + timezonehr; //Add The Hour Time Zone Offset
    if (hr > 23) { //Do The Same Thing We Did Above
      hr = hr - 24;
    }
    else {
      if (hr < 0) {
        hr = hr + 24;
      }
    }
    if (hr > 11) { //If The Hour Is Above 11
      hr = hr - 12; //Subtract 12 (Thus Converting To 12 Hour Time)
      digitalWrite(ampmled, LOW); //Ajust The Am/Pm Led Accordingly
    }
    else {
      digitalWrite(ampmled, HIGH);
    }
    if (hr == 0) { //If The Hour Is 0
      hr = 12; //Make It 12 (This Makes Sure When Its 12:XX The Clock Says 12 Not 0)
    }
    if (int(hr / 10) == 0) { //If The Hour Is 1 Digit Long
      disp(1, 10); //Dont Display Anything On Disp 1 (Ex: If Its 2:30 The Clock Will Display 2:30 Not 02:30)
    }
    else {
      disp(1, int(hr / 10)); //Otherwise Display A 1
    }
    disp(2, int(hr % 10)); //Wirte The Other Numbers To There Respective Displays
    disp(3, int(mn / 10));
    disp(4, int(mn % 10));
  }
}

I have a couple examples of Dalight Saving Time sketches on this post.
http://forum.arduino.cc/index.php?topic=329079.msg2272525#msg2272525
One is for Italy and one for the US. Maybe it will be of some help.

cmk20:
I Have Looked On The Internet For Answers However I Cant Find Anything That Would Work With My Code :frowning:

No, not every piece of code you can dream of is available for download on the Internet.
Sometimes you have to write some lines of code by yourself.

Why not write your own 'localtime' function which works with your code?

Need help with that?

In case yes, you better tell about the current DST rule in your country.
Timezone UTC-5 might be somewhere in the USA or Canada or Brazil or Panama.

P.S.: OK, after scrolling back a dozen or so postings in this thread I found the time zone and DST information

  • standard timezone is UTC-6
  • and during DST the timezone equals UTC-5

Also see this library:

SurferTim:
I have a couple examples of Dalight Saving Time sketches on this post.
DST AUTOMATIC- client ntp - #5 by SurferTim - Networking, Protocols, and Devices - Arduino Forum
One is for Italy and one for the US. Maybe it will be of some help.

Thanks That's Exactly What I Needed :slight_smile:

I Think Your Shift Key Is Coupled To Your Space Key.

I have a full operable DST sketch for the U.S., but it uses the ethernet shield and NTP rather than a GPS unit if you are interested.