Dear Colleagues! Production need asks to write the current time minus 10 minutes and I do not really work. Let me remind you that they get time like this :

#include "stdafx.h" #include <stdio.h> #include <iostream> #include <windows.h> #include <cstddef> // size_t #include <cstring> // strlen, strcpy using namespace std; int main() { setlocale(LC_ALL, "Russian"); SYSTEMTIME st, lt; GetSystemTime(&st); GetLocalTime(&lt); double dm = st.wMinute - 5; //Π·Π° минусом 5 ΠΌΠΈΠ½ΡƒΡ‚ double dm1 = lt.wMinute - 5; //Π·Π° минусом 5 ΠΌΠΈΠ½ΡƒΡ‚ //ΠŸΡ€ΠΎΡΡ‚ΠΎ Π²Ρ‹Π²ΠΎΠ΄ Π²Ρ€Π΅ΠΌΠ΅Π½ΠΈ printf("The system time is: %02d:%02d\n", st.wHour, st.wMinute); printf(" The local time is: %02d:%02d\n", lt.wHour, lt.wMinute); //НовоС врСмя для ΠΌΠΎΠ΅Π³ΠΎ ΠΏΡ€ΠΎΠ΅ΠΊΡ‚Π°. printf("The system time is: %02d:%02d\n", st.wHour, dm); printf(" The local time is: %02d:%02d\n", lt.wHour, dm1); system("PAUSE"); return 0; } 

Output:

 The system time is: 09:24 The local time is: 12:24 The NEW system time is: 09:00 The NEW local time is: 12:00 

It is quite obvious that the third and fourth line cannot satisfy us. How to correctly fix this mess?

  • What do you do between calls to printf ? Or do you want to say that the value of time changes itself ? - Harry
  • @Harry sorry. I skew copied the main line. Corrected. - Andrew Kachalin
  • Are you exactly corrected? Those. do you output double with the %02d ? - Harry
  • @Harry yes everything is like here - Andrew Kachalin
  • one
    No, not all. Once again and slowly: you output double with the %02d . By the link you specify, the integer value is displayed. Start by correcting the output lines. This is not your only problem, you will encounter another when you are thinking of reducing by 5 minutes, say, 2 minutes ... but first, correct the code. Why do you need double here ? - Harry

2 answers 2

I feel that the hints in the comments do not help ...
Start with the lines

 double dm = st.wMinute - 5; //Π·Π° минусом 5 ΠΌΠΈΠ½ΡƒΡ‚ double dm1 = lt.wMinute - 5; //Π·Π° минусом 5 ΠΌΠΈΠ½ΡƒΡ‚ 

Replace them with strings.

 int dm = st.wMinute - 5; //Π·Π° минусом 5 ΠΌΠΈΠ½ΡƒΡ‚ int dm1 = lt.wMinute - 5; //Π·Π° минусом 5 ΠΌΠΈΠ½ΡƒΡ‚ 

Further there will be other problems - when this difference turns out to be negative ... but this is a slightly different question.

  • I have a% d deputy associated with a doble. So it goes. Thank. - Andrew Kachalin

You did not specify the OS for which you are writing the task. Judging by the challenges - Windows? On Linux, everything is very simple:

 #include <time.h> #include <stdio.h> int main(int argc, char *argv[]) { time_t t_wrk, t_10; struct tm *ptm; t_wrk = time(NULL); // Π’Π΅ΠΊΡƒΡ‰Π΅Π΅ врСмя Π² Π‘Π•ΠšΠ£ΠΠ”ΠΠ₯ t_10 = t_wrk - 600; // 10 ΠΌΠΈΠ½ΡƒΡ‚ Π½Π°Π·Π°Π΄ // ΠŸΠ΅Ρ‡Π°Ρ‚Π°Π΅ΠΌ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚ ptm = localtime(&t_wrk); printf("Π‘Ρ‹Π»ΠΎ: %d ΠΌΠΈΠ½ΡƒΡ‚\n", ptm->tm_min); ptm = localtime(&t_10); printf("Π‘Ρ‚Π°Π»ΠΎ: %d ΠΌΠΈΠ½ΡƒΡ‚\n", ptm->tm_min); } 

I suspect that in Windows, these basic functions of working with time have long been realized. Checked at the border crossing hours. The border of the day did not check - everything is too obvious!