Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7428031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T08:40:48+00:00 2026-05-29T08:40:48+00:00

Basically I am writing a simple bit of code to increment every one second

  • 0

Basically I am writing a simple bit of code to increment every one second and display the time and date, which is frustrating me, and I’m sure I’m overlooking something obvious.

So I will display the time as MJD (Modified Julian Date) HH:MM:SS but also allow for a hardcoded MJD to represent a leap second event, and then correctly display the leap second.

It kind of works, however it skips out 23:59:59.

I eventually plan on porting to plain old AVR so I don’t really want to mess around with arduino libraries.

int second = 0, minute = 0, hour = 0;
long mjd = 55000;
long leap = 55964;

void setup()
{
    Serial.begin(9600);
    Serial.print("MDJ ");
    Serial.print(mjd);
    Serial.print(" ");
    Serial.print(hour);
    Serial.print(":");
    Serial.print(minute);
    Serial.print(":");
    Serial.println(second);

}

void loop()
{
    second++;
    if (mjd == leap && hour == 23 && minute == 59 && second == 59) {
        second++;
    } else {
        if (second > 59) {
            minute++;
            second = 0; // reset seconds to zero
        }
    }
    if (minute > 59) {
        hour++;
        minute = 0;
    }

    if (hour > 23) {
        mjd++;
        hour = 0;
    }

    Serial.print("MDJ ");
    Serial.print(mjd);
    Serial.print(" ");
    if (hour < 10)
        Serial.print("0");
    Serial.print(hour);
    Serial.print(":");
    if (minute < 10)
        Serial.print("0");
    Serial.print(minute);
    Serial.print(":");
    if (second < 10)
        Serial.print("0");
    Serial.println(second);
    delay(1000);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T08:40:48+00:00Added an answer on May 29, 2026 at 8:40 am

    I think you might need only a very small change:

    if (mjd == leap && hour == 23 && minute == 59 && second == 60) {
        // allow 23:59:60
    } else {
        ...
    

    The problem appears to be that you increment to the next second, and then if it’s 23:59:59 and a leap second is indicated, you increment again to 60 (skipping 59).

    What you probably want to do is roll over the second unless it is a leap second.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a bit confused. I'm writing a very simple file system that basically: -reads
Good afternoon, I am writing a simple lexer which is basically a modified version
I am writing a simple OpenGL program with Qt Creator which basically creates a
I'm writing a script in python which basically queries WMI and updates the information
So I was writing some code today that basically looks like this: string returnString
I have a logger system which basically is a fancy way of writing my
I'm writing a script which is to be executed hourly. It basically works as:
I'm writing a simple linear linked list implementation in PHP. This is basically just
I'm writing a simple Google Web Toolkit service which acts as a proxy, which
I am writing a component for a web app which will display some html,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.