Skip to content

Commit

Permalink
Replace bugged time(NULL) with WinAPI
Browse files Browse the repository at this point in the history
It looks like time(NULL) returns wrong time since last DST switch - tested in Poland.
  • Loading branch information
tomek-o committed Nov 1, 2023
1 parent 675b08c commit 394f1c0
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
33 changes: 30 additions & 3 deletions totp_generator/FormMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,30 @@
#pragma resource "*.dfm"
TfrmMain *frmMain;
//---------------------------------------------------------------------------

namespace
{

int64_t GetUnixTime(void)
{
FILETIME ft;
LARGE_INTEGER li;

/* Get the amount of 100 nano seconds intervals elapsed since January 1, 1601 (UTC) and copy it
* to a LARGE_INTEGER structure. */
GetSystemTimeAsFileTime(&ft);
li.LowPart = ft.dwLowDateTime;
li.HighPart = ft.dwHighDateTime;

int64_t ret = li.QuadPart;
ret -= 116444736000000000LL; /* Convert from file time to UNIX epoch time. */
ret /= 10000000LL;

return ret;
}

}

__fastcall TfrmMain::TfrmMain(TComponent* Owner)
: TForm(Owner),
outdatedTime(0)
Expand Down Expand Up @@ -102,7 +126,7 @@ void __fastcall TfrmMain::FormShow(TObject *Sender)
CLog::Instance()->SetLevel(E_LOG_TRACE);
CLog::Instance()->callbackLog = frmLog->OnLog;
}
LOG("Application started");
LOG("Application started\n");
}
//---------------------------------------------------------------------------

Expand Down Expand Up @@ -166,7 +190,10 @@ void __fastcall TfrmMain::btnGenerateClick(TObject *Sender)
uint64_t x = 30; /* step in seconds */
uint64_t t; /* number of steps */

time_t now = time(NULL);
int64_t now = GetUnixTime();

LOG("Generating, current time = %lld\n", now);

t = now / x;
outdatedTime = (t + 1) * x;

Expand Down Expand Up @@ -208,7 +235,7 @@ void __fastcall TfrmMain::btnCopyToClipboardClick(TObject *Sender)

void __fastcall TfrmMain::tmrRefreshOtpTimer(TObject *Sender)
{
time_t now = time(NULL);
int64_t now = GetUnixTime();

if (outdatedTime != 0)
{
Expand Down
7 changes: 3 additions & 4 deletions totp_generator/FormMain.dfm
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ object frmMain: TfrmMain
BorderIcons = [biSystemMenu, biMinimize]
BorderStyle = bsSingle
Caption = 'TOTP generator'
ClientHeight = 161
ClientHeight = 180
ClientWidth = 295
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Expand Down Expand Up @@ -36,11 +36,11 @@ object frmMain: TfrmMain
end
object StatusBar: TStatusBar
Left = 0
Top = 142
Top = 161
Width = 295
Height = 19
Panels = <>
ExplicitTop = 143
ExplicitTop = 142
end
object btnGenerate: TButton
Left = 8
Expand Down Expand Up @@ -94,7 +94,6 @@ object frmMain: TfrmMain
end
object miView: TMenuItem
Caption = 'View'
Visible = False
object miViewLog: TMenuItem
Action = actShowLog
end
Expand Down
6 changes: 3 additions & 3 deletions totp_generator/totp_generator.bdsproj
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@
<VersionInfo Name="AutoIncBuild">False</VersionInfo>
<VersionInfo Name="MajorVer">0</VersionInfo>
<VersionInfo Name="MinorVer">0</VersionInfo>
<VersionInfo Name="Release">1</VersionInfo>
<VersionInfo Name="Release">2</VersionInfo>
<VersionInfo Name="Build">0</VersionInfo>
<VersionInfo Name="Debug">False</VersionInfo>
<VersionInfo Name="PreRelease">False</VersionInfo>
Expand All @@ -273,13 +273,13 @@
<VersionInfoKeys>
<VersionInfoKeys Name="CompanyName">Tomasz Ostrowski</VersionInfoKeys>
<VersionInfoKeys Name="FileDescription"></VersionInfoKeys>
<VersionInfoKeys Name="FileVersion">0.0.1.0</VersionInfoKeys>
<VersionInfoKeys Name="FileVersion">0.0.2.0</VersionInfoKeys>
<VersionInfoKeys Name="InternalName"></VersionInfoKeys>
<VersionInfoKeys Name="LegalCopyright"></VersionInfoKeys>
<VersionInfoKeys Name="LegalTrademarks"></VersionInfoKeys>
<VersionInfoKeys Name="OriginalFilename"></VersionInfoKeys>
<VersionInfoKeys Name="ProductName"></VersionInfoKeys>
<VersionInfoKeys Name="ProductVersion">0.0.1.0</VersionInfoKeys>
<VersionInfoKeys Name="ProductVersion">0.0.2.0</VersionInfoKeys>
<VersionInfoKeys Name="Comments"></VersionInfoKeys>
</VersionInfoKeys>
<Debugging>
Expand Down
Binary file modified totp_generator/totp_generator.res
Binary file not shown.

0 comments on commit 394f1c0

Please sign in to comment.