Berechnen der zeit anhand eines zeit und datum strings
-
hallo
wie kann ich eine zeit in sekunden ausrechnen wenn ich folgendes als information habe?
das momentane datum + die momentane zeit. nun die differenz in sekunden zu einem neuen datum sowie neue zeit, oder gleiches datum aber neue zeit? z.B.
von
15.09.2005 16:33:02
nach
18.10.2005 08:10:22oder von
15.09.2005 16:33:02
nach
15.09.2005 23:43:40wie kann ich nun das ganze in sekunden ausrechnen? das datum sowie die zeit (von/nach) steht mir jeweils als string vor. genau wie eben beschrieben.
-
nach einem kaffee und einem zigaretchen hab ich mir eine lösung überlegt.
diese betrifft wenn das datum das gleiche ist. also nur die differenz der stunden in sekunden ausrechnen. anbei meine lösung:int seconds; int a_h = StrToInt(Edit1->Text); int a_m = StrToInt(Edit2->Text); int a_s = StrToInt(Edit3->Text); int t_h = StrToInt(Edit4->Text); int t_m = StrToInt(Edit5->Text); int t_s = StrToInt(Edit6->Text); a_h = t_h - a_h; a_m = t_m - a_m; a_s = t_s - a_s; a_h *= 3600; a_m *= 60; seconds = a_h + a_m + a_s;
was denkt ihr darüber? ok oder kann man besser machen?
was die berechnung mit zudem anderem datum betrifft, da bin ich gerade dran. sollte sich jedoch mit der lösung spiegeln da ich lediglich och ausrechnen muss wieviele date es differenziert und dann die den multiplikator operator wieder spielen lassen.
-
Sollte funktionieren. Ab jetzt wird's ein wenig schwieriger.
Habe für ein Programm das ich kürzlich gemacht habe in die andere Richtung rechnen müssen. Gegeben war die Anzahl der Tage seit 1.1.1980 und ich musste das Datum berechnen. Vielleicht hilft dir der code.bool is_leap_year( const int year ) { if ((year % 400) == 0) return true; else if ((year % 100) == 0) return false; else if ((year % 4) == 0) return true; return false; } int days_of_year( const int year ) { return ( is_leap_year( year ) ? 366 : 365 ); } short days_of_month(const int month, const int year) { int arrDaysinMonth[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; if (month == 2) { // february: if (is_leap_year(year)) return 29; else return 28; } if ((month >= 1) && (month <= 12)) return arrDaysinMonth[month]; else { return 0; // invalid month } } string get_date_of_day( unsigned long day ) const { ostringstream str; int year = 1980; while (1) { int days_this_year = days_of_year( year ); if ( day > days_this_year ) { day -= days_this_year; year++; } else break; } int month = 1; while (1 ) { int days_this_month = days_of_month( month, year ); if ( day > days_this_month ) { day -= days_this_month; month++; } else break; } str << day << "." << month << "." << year; return str.str(); }
Kurt
-
ui :p thanks werd ich mir mal anschauen. habe jedoch gerade ein anderes problem. möchte überprüfen ob ein datum kleiner oder grösser ist als ein anderes datum. wenn ich das datum zu einem typ int konvertieren würde, würde das nicht richtig gehen, da die nullen vor einer zahl im string also z.B. 06.09.2005 als int dann so aussehen würden 692005.
um das ganze aber korrekt vergleichen zu können, ob ein datum grösser oder kleiner ist als ein anderes, müsste der int ja so aussehen 06092005 was ja aber nicht geht. wie löse ich sowas am einfachsten? mit einem float geht das glaube ich auch nicht oder?
-
Du musst nur das Format des Strings umdrehen
zb 20050321 dann kannst du die strings ohne umzurechnen direkt vergleichen.
Kurt
-
das ist eine gute idee. gibt es eine funktion die das umkehren erledigt doer wie genau kann ich die positionen spiegeln?
-
Kommt ganz darauf an wie das datum vorliegt.
Kurt
-
hallo
habs nun mit substring gelöst. an der berechung der sekunden wenn das datum nicht gleich das aktuelle ist hänge ich jedoch immer noch. habe mir deinen code mal angeschaut jedoch kann ich daraus keinen lösungsansatz machen.
das mit der berechnung der sekunden wenn das datum gleich dem aktuellen ist, klappt ja, wie bereits im zweiten posting erwähnt. jedoch scheitere ich an der lösung wenn das datum abweicht. also z.B. 1 tag mehr oder 2 oder 3 etc...
hatte es mir einfacher vorgestellt. z.B. das ich eben den oder die tag(e) dazu rechne und das in sekunden. das würde ja aber garnicht gehen. beispiel:es ist der 23.09.2005 und die zeit 23:50:00
gegeben ist der 24.09.2005 und die zeit 00:50:00das kann ich ja nun nicht als ganzer tag berechnen, obwohl 24. von der zahl her ein tag mehr wäre. jedoch wenn man sich das beispiel anschaut, handelt es sich lediglich um eine stunde mehr. also genau so wie wenn das datum gleich aktuellem wäre: 23.09.2005 22:50:00 ... 23.09.2005 23:50:00 gleicher effekt.
wie also erechne ich das ganze am effizientesten wenn das datum grösser ist als das aktuellste, jedoch so, das die errechneten sekunden auch stimmen.
-
Der einzige vernünftige Rat den ich dir geben kann ist dass du dir die normalen std C/C++ time funktionen ansiehst.http://www.cppreference.com/stddate/
Dort findest du ziemlich alles was du brauchst. Wenn du aber trotzdem alles selbst machen willst dann könnten dir meine Funktionen etwas weiterhelfen.
Bei weiteren Fragen werde ich und viele andere hier dir gerne weiterhelfen.
Kurt
-
ich kriegs einfach nicht hin. das kann doch nicht so schwer sein oder doch? scheinbar bin ich zu übermüdet. die lösung liegt mir auf der zunge und doch irgendwie nicht. die logik ist ja eigentlich simpel. im schlimmsten fall könnte ich einfach die stunden inkrementieren und jedesmal abfragen ob das höhere datum nun erreicht ist, wenn ja, dann errechne ich die inkrementieren stunden + die restlichen minuten und sekunden nach total sekunden. wäre ne möglichkeit oder? dann müsste ich jedoch von den sekunden her anfangen zu inkrementieren da das abfragen ob das datum erreicht wurde ja nicht aussreicht. könnte ja sein das zwar das datum höher ist, die zeit aber unter der aktuellen zeit + aktuellem datum. da muss man also auch noch aufpassen. oder geht es einfacher und ich kenne nur die lösung (bezüglich C++ wissensstand) noch nicht?
-
Wenn du es also wirklich selbst machen willst dann würde ich auf jeden Fall das Datum + Uhrzeit in eine Anzahl von Sekunden seit einem bestimmten Tag ( 1.1.1900 vielleicht ) umrechnen. Diese Zahlen kannst du dann einfach vergleichen, addieren und subtrahieren. Dann brauchst du noch Funktionen um eine bestimmte Anzahl von Sekunden wieder in ein Datum und Uhrzeit umzurechnen.
Kurt
-
ich versuche es gerade inetwa so, leider weiss ich nicht weiter.
int act_day = 25; //aktueller tag des datum int act_month = 9; //aktueller monat des datum int act_year = 2005; //aktuelles jahr des datum int act_hour = 22; //aktuelle stunde der zeit int act_minute = 50; //aktuelle minute der zeit int act_second = 5; //aktuelle sekunde der zeit int to_day = 26; //erechne bis tag int to_month = 9; //erechne bis monat int to_year = 2005; //erechne bis jahr int to_hour = 1; //erechne bis stunde int to_minute = 50; //erechne bis minute int to_second = 5; //erechne bis sekunde int total_days = 0; //total inkrementiert der differenz int total_hours = 0; //total inkrementiert der differenz int total_minutes = 0; //total inkrementiert der differenz int total_seconds = 0; //total inkrementiert der differenz //am ende in sekunden ausrechnen: total_days, total_hours, total_minutes //am ende addieren: total_days(sekunden), total_hours(sekunden), total_minutes(sekunden), total_seconds //<<<--- HIER NUN SCHLEIFENARBEIT DIE ALLES WAS ZU INKREMENTIEREN IST, INKREMENTIERT. NUR SOVIEL ALS NÖTIG, DAMIT ES STIMMT. --->>> while(act_day < to_day && act_month < to_month && act_year < to_year) //überprüfen ob act_day, act_month, act_year nun gleich sind wie to_ { //...ansonsten die daten weiter inkrementieren..... //<<<--- HIER WEISS ICH NICHT WEITER --->>> }
aber ist mein lösungsansatz einigermassen verständlich?
kann mir einer weiterhelfen? ich muss nun eben durch eine verschachtelung in der while-schleife das ganze effizient inkrementieren, abfragen, etc. hmmm.
-
soweit bin ich nun:
int act_day = 25; int act_month = 9; int act_year = 2005; int act_hour = 22; int act_minute = 50; int act_second = 5; int to_day = 26; int to_month = 9; int to_year = 2005; int to_hour = 1; int to_minute = 50; int to_second = 5; int total_diffdays = 0; int total_diffhours = 0; int total_diffminutes = 0; int total_diffseconds = 0; int seconds = 0; while(true) { if(act_day < to_day && act_month < to_month && act_year < to_year) { if(act_second < 59) { act_second++; total_diffseconds++; } else { act_second = 0; act_minute++; total_diffminutes++; if(act_minute == 59) { act_minute = 0; act_hour++; total_diffhours++; } if(act_hour == 23) { act_day++; total_diffdays++; } } } else { total_diffdays *= 24 * 60 * 60; total_diffhours *= 60 * 60; total_diffminutes *= 60; seconds = total_diffdays + total_diffhours + total_diffminutes + total_diffseconds; break; } }
ist jedoch noch fehlerhaft da ich 1. wissen muss ob ein monat 28, 30 oder 31 tage hat. dann muss ich noch was überprüfen. Hilfe!
-
Sorry, keine direkte Antwort auf Deinen Code.
Vielleicht aber trotzdem für Dich von Interesse.
Zum Umrechnen aus der "normalen" Darstellung Tag Monat Jahr in eine linare Darstellung (Anzahl Tage seit einem Datum) hilft das Stichwort: Julianisches Datum. Erklärt wird das ganz ordentlich (mit Formeln) bei Wikipedia.
Routinen findet man dafür auch schon fertig, z.B. in der gnulib
http://savannah.gnu.org/projects/gnulib
Module getDate.cDie Differenz in Tagen ist dann einfach die Differenz der beiden julianischen Datumswerte. Die Zeit bildet man einfach als Bruchteil von 24 Stunden ab (z.B. 0.5 ist 12h Mittag).
Um die Zahl der Sekunden zu berechnen, multiplizierst Du die Differenz mit 24 * 60 * 60 (Anzahl Stunden pro Tages * Anzahl Minuten pro Stunde * Anzahl Sekunden pro Minute) und hast dann die Differenz in Sekunden.Beispiel (auf Sekunden gerundet):
16.03.1983 22:48.00 = 2445410.45
10.01.1984 17:31.11 = 2445710.23
Differenz: 299,78 = 299 Tage und 0,78 von 24 Stunden = 18:43:12
In Sekunden 299,78 * 24 * 60 * 60 = 18648714 Sekunden
-
Ich würde Datum und Uhrzeit so nach Sekunden seit 1.1.1900 umrechnen
unsigned long time_to_sec( int hour, int minute, int sec ) { return sec + 60*minute + 60*60*hour; } unsigned long sec_of_month( int month, int year ) { return days_of_month(month, year) * 24 * 60* 60; } unsigned long sec_of_year( int year ) { return days_of_year( year ) * 24 * 60* 60; } unsigned long time_date_to_sec ( int year, int month, int day, int hour, int minute, int sec ) { unsigned long ret = time_to_sec( hour, minute, sec ); for( int y = 1900; y < year; ++y ) // ganze jahre dieser Tag ret += sec_of_year( y ); for( int m = 1; m < month; ++m ) // ganze monate ret += sec_of_month( m, year ); ret += ( day-1 )* 24 * 60* 60; // restliche Tage im Monat return ret; }
Vorsicht ungetestet.
Die fehlenden funktionen sind in meinem ersten posting.
Kurt
-
danke werde ich mir gleich mal anschauen und auch danke an dir "niemand" schau ich mir auch gleich an. habe meine lösung noch erweitert. könnt ihr euch die bitte mal anschauen? sollte bis auf den konstanten monat (kommentiert) eigentlich beinahe stimmen oder was vergesse ich noch zu berücksichtigen? was ist falsch?
[cpp]
int act_day = 25;
int act_month = 9;
int act_year = 2005;int act_hour = 23;
int act_minute = 50;
int act_second = 5;int to_day = 26;
int to_month = 9;
int to_year = 2005;int to_hour = 2;
int to_minute = 50;
int to_second = 5;int total_diffdays = 0;
int total_diffhours = 0;
int total_diffminutes = 0;
int total_diffseconds = 0;int seconds = 0;
while(true)
{
if(act_day < to_day && act_month <= to_month && act_year <= to_year)
{
if(act_second < 59)
{
act_second++;
if(total_diffseconds == 59)
{
total_diffminutes++;
total_diffseconds = 0;
}
else
{
total_diffseconds++;
}
}
else
{
act_second = 0;
act_minute++;
if(total_diffminutes == 59)
{
total_diffhours++;
total_diffminutes = 0;
}
else
{
total_diffminutes++;
}
if(act_minute == 59)
{
act_minute = 0;
act_hour++;
if(total_diffhours == 23)
{
total_diffdays++;
total_diffhours = 0;
}
else
{
total_diffhours++;
}
}
if(act_hour == 23)
{
if(act_day == 31) //momentan konstant muss aber stimmen!!! (ob 29, 30, 31)
{
if(act_month == 12)
{
act_day = 1;
act_month = 1;
act_year++;
}
else
{
act_month++;
act_day = 1;
}
}
else
{
act_day++;
}
}
}
}
else
{
total_diffdays *= 24 * 60 * 60;
total_diffhours *= 60 * 60;
total_diffminutes *= 60;
seconds = total_diffdays + total_diffhours + total_diffminutes + total_diffseconds;
break;
}
}
[cpp]
-
sorry!
int act_day = 25; int act_month = 9; int act_year = 2005; int act_hour = 23; int act_minute = 50; int act_second = 5; int to_day = 26; int to_month = 9; int to_year = 2005; int to_hour = 2; int to_minute = 50; int to_second = 5; int total_diffdays = 0; int total_diffhours = 0; int total_diffminutes = 0; int total_diffseconds = 0; int seconds = 0; while(true) { if(act_day < to_day && act_month <= to_month && act_year <= to_year) { if(act_second < 59) { act_second++; if(total_diffseconds == 59) { total_diffminutes++; total_diffseconds = 0; } else { total_diffseconds++; } } else { act_second = 0; act_minute++; if(total_diffminutes == 59) { total_diffhours++; total_diffminutes = 0; } else { total_diffminutes++; } if(act_minute == 59) { act_minute = 0; act_hour++; if(total_diffhours == 23) { total_diffdays++; total_diffhours = 0; } else { total_diffhours++; } } if(act_hour == 23) { if(act_day == 31) //momentan konstant muss aber stimmen!!! (ob 29, 30, 31) { if(act_month == 12) { act_day = 1; act_month = 1; act_year++; } else { act_month++; act_day = 1; } } else { act_day++; } } } } else { total_diffdays *= 24 * 60 * 60; total_diffhours *= 60 * 60; total_diffminutes *= 60; seconds = total_diffdays + total_diffhours + total_diffminutes + total_diffseconds; break; } }
-
hm komisch das ergebnisn ist 114
im code ist die differenz 3 stunden. müsste also ergebniss: 10800 sein.
-
hier der entgültige code, mit deinen beiden funktionen um den tag des monats zu holen.
//--------------------------------------------------------------------------- bool is_leap_year(const int year) { if((year % 400) == 0) { return true; } else { if((year % 100) == 0) { return false; } else { if((year % 4) == 0) { return true; } } } return false; } //--------------------------------------------------------------------------- short days_of_month(const int month, const int year) { int arrDaysinMonth[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if(month == 2) { if(is_leap_year(year)) { return 29; } else { return 28; } } if((month >= 1) && (month <= 12)) { return arrDaysinMonth[month]; } else { return 0; } } //--------------------------------------------------------------------------- int CalculateSeconds() { int act_day = 25; int act_month = 9; int act_year = 2005; int act_hour = 23; int act_minute = 50; int act_second = 5; int to_day = 26; int to_month = 9; int to_year = 2005; int to_hour = 2; int to_minute = 50; int to_second = 5; int total_diffdays = 0; int total_diffhours = 0; int total_diffminutes = 0; int total_diffseconds = 0; int seconds = 0; while(true) { if(act_day < to_day && act_month <= to_month && act_year <= to_year) { if(act_second < 59) { act_second++; if(total_diffseconds == 59) { total_diffminutes++; total_diffseconds = 0; } else { total_diffseconds++; } } else { act_second = 0; act_minute++; if(total_diffminutes == 59) { total_diffhours++; total_diffminutes = 0; } else { total_diffminutes++; } if(act_minute == 59) { act_minute = 0; act_hour++; if(total_diffhours == 23) { total_diffdays++; total_diffhours = 0; } else { total_diffhours++; } } if(act_hour == 23) { if(act_day == days_of_month(act_month, act_year)) { if(act_month == 12) { act_day = 1; act_month = 1; act_year++; } else { act_month++; act_day = 1; } } else { act_day++; } } } } else { total_diffdays *= 24 * 60 * 60; total_diffhours *= 60 * 60; total_diffminutes *= 60; seconds = total_diffdays + total_diffhours + total_diffminutes + total_diffseconds; return seconds; } } } //---------------------------------------------------------------------------
aber eben, komischerweise erhalte ich 114 als ergebniss.
-
also jetzt bin ich meiner meinung nach auf dem richtigen weg. die schleife beendet nun genau dann wann sie soll. nähmlich wenn datum und die zeit zur berechnung erreicht wurde. jetzt stimmt nur der wert noch nicht. also alle total_ inkrementationen sind wohl noch teils an falscher stelle. jetzt muss ich jedoch schlafen
wenn jemand lust hat mir den code zu korrigieren, hier ist er:
int CalculateSeconds() { int act_day = 25; int act_month = 9; int act_year = 2005; int act_hour = 23; int act_minute = 50; int act_second = 5; int to_day = 26; int to_month = 9; int to_year = 2005; int to_hour = 2; int to_minute = 50; int to_second = 5; int total_diffdays = 0; int total_diffhours = 0; int total_diffminutes = 0; int total_diffseconds = 0; int seconds = 0; while(true) { if(act_day == to_day && act_month == to_month && act_year == to_year && act_hour == to_hour && act_minute == to_minute && act_second == to_second) { total_diffdays *= 24 * 60 * 60; total_diffhours *= 60 * 60; total_diffminutes *= 60; seconds = total_diffdays + total_diffhours + total_diffminutes + total_diffseconds; return seconds; } else { if(act_second < 59) { act_second++; if(total_diffseconds < 59) { total_diffseconds++; } else { total_diffseconds = 0; total_diffminutes++; } } else { act_second = 0; if(act_minute == 59) { act_minute = 0; if(act_hour == 23) { act_hour = 0; if(act_day == days_of_month(act_month, act_year)) { if(act_month == 12) { act_day = 1; act_month = 1; act_year++; act_hour = 0; act_minute = 0; act_second = 0; } else { act_month++; act_day = 1; act_hour = 0; act_minute = 0; act_second = 0; } } else { act_day++; } } else { act_hour++; if(total_diffhours < 23) { total_diffhours++; } else { total_diffseconds = 0; total_diffminutes = 0; total_diffhours = 0; total_diffdays++; } } } else { act_minute++; if(total_diffminutes < 59) { total_diffminutes++; } else { total_diffseconds = 0; total_diffminutes = 0; total_diffhours++; } } } } } }