Czy wie ktoś jak wyświetlić bieżącą datę w języku C (która jest np w systemie naszym).
Mógłby napisać prosty program wyświetlający bieżącą datę? Poprosze
Wyświetlanie bieżącej daty w programie
- Rejestracja: dni
- Ostatnio: dni
- Postów: 45
0
- Rejestracja: dni
- Ostatnio: dni
- Postów: 37
1
Najprościej jak się dało :) Ale żebyś pomyślał, zrobiłem to dla godziny
#include <iostream>
#include <string>
#include <ctime>
auto zrob_string(int n) -> string
{
return (n < 10 ? "0" + to_string(n) : to_string(n));
}
auto zrob_date(struct tm dattm) -> string
{
return zrob_string(dattm.tm_hour) +
":" +
zrob_string(dattm.tm_min) +
":" +
zrob_string(dattm.tm_sec);
}
auto main() -> int
{
time_t tim = time(&tim);
struct tm dattm = *localtime(&tim);
printf("%s", zrob_date(dattm).c_str());
}
- Rejestracja: dni
- Ostatnio: dni
- Lokalizacja: Kraków
- Postów: 43
2
Przychylam się do pomysłu aby zrobić to dla czasu. To coś w rodzaju wędki nie ryby. Nie rozumiem tylko dlaczego to jest C++ skoro w tagach jest C i C# (czego w sumie też nie rozumiem). Poniżej moja propozycja, choć może wyważam drzwi armatą...
MSVC↴
#include <stdio.h>
#include <time.h>
int main(void) {
struct timespec ts;
timespec_get(&ts, TIME_UTC);
struct tm tm;
localtime_s(&tm, &ts.tv_sec);
printf("%02d:%02d:%02d\n", tm.tm_hour, tm.tm_min, tm.tm_sec);
}
POSIX↴
#include <stdio.h>
#include <time.h>
int main(void) {
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
struct tm tm;
localtime_r(&ts.tv_sec, &tm);
printf("%02d:%02d:%02d\n", tm.tm_hour, tm.tm_min, tm.tm_sec);
}
https://en.cppreference.com/w/c/chrono/timespec.html
https://en.cppreference.com/w/c/chrono/timespec_get
https://en.cppreference.com/w/c/chrono/tm.html
https://en.cppreference.com/w/c/chrono/localtime
- Rejestracja: dni
- Ostatnio: dni
0
C23:
https://en.cppreference.com/w/c/chrono/localtime.html
https://en.cppreference.com/w/c/chrono/strftime.html
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
int main() {
char buff[256];
time_t t = time(NULL);
struct tm tm;
if (localtime_r(&t, &tm) == NULL) {
perror("local time");
return EXIT_FAILURE;
}
if (strftime(buff, sizeof buff, "%A %c", &tm)) {
puts(buff);
} else {
puts("strftime failed");
}
}