Skip to content

Commit

Permalink
DB: Simplify date parsing a bit and remove RawGetDateTime
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed Aug 13, 2022
1 parent 01fc519 commit 009b317
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 30 deletions.
5 changes: 3 additions & 2 deletions MCGalaxy/Commands/Information/CmdAbout.cs
Expand Up @@ -85,8 +85,9 @@ public sealed class CmdAbout : Command2 {
BlockDBEntry entry = default(BlockDBEntry);
entry.OldRaw = Block.Invalid;

foreach (string[] row in entries) {
DateTime time = row[1].ParseDBDate();
foreach (string[] row in entries)
{
DateTime time = row[1].ParseDBDate().ToUniversalTime();
TimeSpan delta = time - BlockDB.Epoch;
entry.TimeDelta = (int)delta.TotalSeconds;
entry.Flags = BlockDBFlags.ManualPlace;
Expand Down
1 change: 0 additions & 1 deletion MCGalaxy/Database/Backends/Interfaces.cs
Expand Up @@ -77,7 +77,6 @@ public abstract class ISqlRecord
public abstract bool IsDBNull(int i);

public abstract object GetValue(int i);
public abstract string RawGetDateTime(int col);
public abstract string GetStringValue(int col);
public abstract string DumpValue(int col);

Expand Down
2 changes: 1 addition & 1 deletion MCGalaxy/Database/Backends/MySQL.cs
Expand Up @@ -238,7 +238,7 @@ sealed class MySQLReader : ISqlReader
public override object GetValue(int i) { return rdr.GetValue(i); }


public override string RawGetDateTime(int col) {
string RawGetDateTime(int col) {
DateTime date = GetDateTime(col);
return date.ToString(Database.DateFormat);
}
Expand Down
18 changes: 4 additions & 14 deletions MCGalaxy/Database/Backends/SQLiteBackend.cs
Expand Up @@ -356,17 +356,11 @@ public sealed class SQLiteCommand : ISqlCommand
static class SQLiteConvert
{
static string[] _datetimeFormats = new string[] {
"yyyy-MM-dd HH:mm:ss.FFFFFFFK", /* NOTE: UTC default (0). */
"yyyy-MM-dd HH:mm:ssK",
"yyyy-MM-dd HH:mmK",

"yyyy-MM-dd HH:mm:ss.FFFFFFF", /* NOTE: Non-UTC default (3). */
"yyyy-MM-dd HH:mm:ss",
"yyyy-MM-dd HH:mm",
DATEFORMAT_UTC, DATEFORMAT_LOCAL
};

static readonly string _datetimeFormatUtc = _datetimeFormats[0];
static readonly string _datetimeFormatLocal = _datetimeFormats[3];
const string DATEFORMAT_UTC = "yyyy-MM-dd HH:mm:ssK";
const string DATEFORMAT_LOCAL = "yyyy-MM-dd HH:mm:ss";
static Encoding utf8 = new UTF8Encoding();

public static byte[] ToUTF8(string text) {
Expand Down Expand Up @@ -400,7 +394,7 @@ static class SQLiteConvert
}

public static string ToString(DateTime value) {
string format = (value.Kind == DateTimeKind.Utc) ? _datetimeFormatUtc : _datetimeFormatLocal;
string format = (value.Kind == DateTimeKind.Utc) ? DATEFORMAT_UTC : DATEFORMAT_LOCAL;
return value.ToString(format, CultureInfo.InvariantCulture);
}

Expand Down Expand Up @@ -540,10 +534,6 @@ public sealed class SQLiteDataReader : ISqlReader
return stmt.GetValue(i, affinity);
}

public override string RawGetDateTime(int col) {
return GetString(col); // GetDateTime is extremely slow so avoid it
}

public override string GetStringValue(int col) {
return GetString(col);
}
Expand Down
14 changes: 2 additions & 12 deletions MCGalaxy/Database/BlockDB/BlockDBTableDumper.cs
Expand Up @@ -158,18 +158,8 @@ public sealed class BlockDBTableDumper {
}

void UpdateTimestamp(ISqlRecord record) {
// date is in format yyyy-MM-dd hh:mm:ss
string date = record.RawGetDateTime(1);
int year = (date[0] - '0') * 1000 + (date[1] - '0') * 100 + (date[2] - '0') * 10 + (date[3] - '0');
int month = (date[5] - '0') * 10 + (date[6] - '0');
int day = (date[8] - '0') * 10 + (date[9] - '0');
int hour = (date[11] - '0') * 10 + (date[12] - '0');
int min = (date[14] - '0') * 10 + (date[15] - '0');
int sec = (date[17] - '0') * 10 + (date[18] - '0');

DateTime time = new DateTime(year, month, day, hour, min, sec);
DateTime utc = time.ToUniversalTime();
entry.TimeDelta = (int)utc.Subtract(BlockDB.Epoch).TotalSeconds;
DateTime time = record.GetDateTime(1).ToUniversalTime();
entry.TimeDelta = (int)time.Subtract(BlockDB.Epoch).TotalSeconds;
}
}
}

0 comments on commit 009b317

Please sign in to comment.