Skip to content

Safin313-stack/Project-C-Digital-Clock

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation



C Platform Compiler Editor MIT License


View Source
✦ Click above to view the full source code on GitHub ✦




🕐 Features

⏰ Real-time Display 🗓️ Date Display 🔄 12/24 Format 🖥️ Terminal UI ⚡ system time.h
Live clock updating every second in the terminal Shows current date alongside the time Toggle between 12-hour AM/PM and 24-hour format Clean formatted output directly in the console Powered by C standard library time functions

🖥️ Terminal Preview

╔══════════════════════════════════════════════╗
║                                              ║
║           DIGITAL CLOCK — C PROGRAM         ║
║                                              ║
║   ┌──────────────────────────────────────┐   ║
║   │                                      │   ║
║   │         🕐  03 : 42 : 17  PM         │   ║
║   │                                      │   ║
║   │       📅  Thursday, 19 Mar 2026      │   ║
║   │                                      │   ║
║   └──────────────────────────────────────┘   ║
║                                              ║
║   Format  : [ 12-hr ]  [ 24-hr ]             ║
║   Press   :  Ctrl+C  to exit                 ║
║                                              ║
╚══════════════════════════════════════════════╝

🧠 How It Works

Fetching System Time with time.h

#include <stdio.h>
#include <time.h>

int main() {
    time_t rawtime;
    struct tm *timeinfo;

    time(&rawtime);                   // get current time as seconds since epoch
    timeinfo = localtime(&rawtime);   // convert to local time struct

    printf("Time: %02d:%02d:%02d\n",
        timeinfo->tm_hour,
        timeinfo->tm_min,
        timeinfo->tm_sec);

    printf("Date: %02d/%02d/%04d\n",
        timeinfo->tm_mday,
        timeinfo->tm_mon + 1,
        timeinfo->tm_year + 1900);

    return 0;
}

12 / 24 Hour Format Toggle

int hour = timeinfo->tm_hour;
char *period = "AM";

if (hour >= 12) {
    period = "PM";
    if (hour > 12) hour -= 12;   // convert to 12-hr
} else if (hour == 0) {
    hour = 12;                   // midnight edge case
}

Real-time Refresh with sleep()

#include <windows.h>   // for Windows Sleep()

while (1) {
    system("cls");     // clear terminal screen
    // ... print clock ...
    Sleep(1000);       // wait 1 second then refresh
}

🛠️ Compile and Run

Prerequisites

Step 1 — Compile

gcc -o digital_clock "(PROJECT)_Digital_Clock.c"

Step 2 — Run

./digital_clock

Or run directly in VS Code terminal

# Open the folder in VS Code, then in the terminal:
gcc -o digital_clock "(PROJECT)_Digital_Clock.c" && ./digital_clock

⚠️ This project uses windows.h and system("cls") — designed for Windows only. On Linux/macOS replace Sleep(1000) with sleep(1) and system("cls") with system("clear").


📁 Project Structure

Project-C-Digital-Clock/
│
├── 📄 (PROJECT)_Digital_Clock.c    ← Full source code · single file
└── 📄 README.md                    ← Project documentation

🛠️ Tech Stack

┌──────────────────────────────────────────────────┐
│              C Console Application               │
├─────────────────┬────────────────────────────────┤
│  Language       │  C (C99 standard)              │
│  Compiler       │  MinGW GCC (Windows)           │
│  Editor         │  VS Code                       │
│  Libraries      │  stdio.h · time.h · windows.h  │
│  Platform       │  Windows                       │
└─────────────────┴────────────────────────────────┘

📚 Key Concepts Used

time()         → returns current time as seconds since Jan 1, 1970
localtime()    → converts time_t to local tm struct
tm_hour        → hour field (0-23) from tm struct
tm_min         → minute field (0-59) from tm struct
tm_sec         → second field (0-59) from tm struct
tm_mday        → day of month from tm struct
tm_mon         → month (0-11, so +1 for display)
tm_year        → years since 1900 (so +1900 for display)
Sleep(1000)    → pauses execution for 1000ms (Windows)
system("cls")  → clears the terminal screen (Windows)

👤 Developer


Saharia Hassan Safin C Developer · Front-end Developer


GitHub   Source Code


"Time flies — so I built a clock to watch it" 🕐



MIT License · © 2025 Saharia Hassan Safin · ⭐ Star this repo if it helped you!

About

A real-time digital clock in C — terminal display with 12/24 hour format and date

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages