Skip to content

Commit 5781a8a

Browse files
committed
Implement sensing_dayssince2000 block
1 parent 05b0fbd commit 5781a8a

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

src/blocks/sensingblocks.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "sensingblocks.h"
2424
#include "audio/audioinput.h"
2525
#include "audio/iaudioloudness.h"
26+
#include "engine/internal/clock.h"
2627

2728
using namespace libscratchcpp;
2829

@@ -60,6 +61,7 @@ void SensingBlocks::registerBlocks(IEngine *engine)
6061
engine->addCompileFunction(this, "sensing_resettimer", &compileResetTimer);
6162
engine->addCompileFunction(this, "sensing_of", &compileOf);
6263
engine->addCompileFunction(this, "sensing_current", &compileCurrent);
64+
engine->addCompileFunction(this, "sensing_dayssince2000", &compileDaysSince2000);
6365
}
6466

6567
void SensingBlocks::onInit(IEngine *engine)
@@ -367,6 +369,11 @@ CompilerValue *SensingBlocks::compileCurrent(Compiler *compiler)
367369
return compiler->addConstValue(Value());
368370
}
369371

372+
CompilerValue *SensingBlocks::compileDaysSince2000(Compiler *compiler)
373+
{
374+
return compiler->addFunctionCall("sensing_dayssince2000", Compiler::StaticType::Number);
375+
}
376+
370377
void SensingBlocks::onAnswer(const std::string &answer)
371378
{
372379
// https://github.com/scratchfoundation/scratch-vm/blob/6055823f203a696165084b873e661713806583ec/src/blocks/scratch3_sensing.js#L99-L115
@@ -775,3 +782,12 @@ extern "C" double sensing_current_second()
775782
tm *ltm = localtime(&now);
776783
return ltm->tm_sec;
777784
}
785+
786+
extern "C" double sensing_dayssince2000()
787+
{
788+
if (!SensingBlocks::clock)
789+
SensingBlocks::clock = Clock::instance().get();
790+
791+
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(SensingBlocks::clock->currentSystemTime().time_since_epoch()).count();
792+
return ms / 86400000.0 - 10957.0;
793+
}

src/blocks/sensingblocks.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace libscratchcpp
1010
{
1111

1212
class IAudioInput;
13+
class IClock;
1314
class ExecutionContext;
1415
class Thread;
1516

@@ -27,6 +28,7 @@ class SensingBlocks : public IExtension
2728
static void askQuestion(ExecutionContext *ctx, const StringPtr *question);
2829

2930
static inline IAudioInput *audioInput = nullptr;
31+
static inline IClock *clock = nullptr;
3032

3133
private:
3234
struct Question
@@ -62,6 +64,7 @@ class SensingBlocks : public IExtension
6264
static CompilerValue *compileResetTimer(Compiler *compiler);
6365
static CompilerValue *compileOf(Compiler *compiler);
6466
static CompilerValue *compileCurrent(Compiler *compiler);
67+
static CompilerValue *compileDaysSince2000(Compiler *compiler);
6568

6669
static void onAnswer(const std::string &answer);
6770
static void enqueueAsk(const std::string &question, Thread *thread);

test/blocks/sensing_blocks_test.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include <audioinputmock.h>
1414
#include <audioloudnessmock.h>
1515
#include <timermock.h>
16+
#include <clockmock.h>
1617

1718
#include "../common.h"
1819
#include "blocks/sensingblocks.h"
@@ -44,19 +45,23 @@ class SensingBlocksTest : public testing::Test
4445
m_audioLoudness = std::make_shared<AudioLoudnessMock>();
4546
SensingBlocks::audioInput = &m_audioInput;
4647
EXPECT_CALL(m_audioInput, audioLoudness()).WillRepeatedly(Return(m_audioLoudness));
48+
49+
SensingBlocks::clock = &m_clock;
4750
}
4851

4952
void TearDown() override
5053
{
5154
SensingBlocks::clearQuestions();
5255
SensingBlocks::audioInput = nullptr;
56+
SensingBlocks::clock = nullptr;
5357
}
5458

5559
std::unique_ptr<IExtension> m_extension;
5660
Project m_project;
5761
IEngine *m_engine = nullptr;
5862
EngineMock m_engineMock;
5963
std::shared_ptr<AudioLoudnessMock> m_audioLoudness;
64+
ClockMock m_clock;
6065

6166
private:
6267
AudioInputMock m_audioInput;
@@ -2745,3 +2750,25 @@ TEST_F(SensingBlocksTest, Current_Invalid)
27452750
ASSERT_EQ(value_toDouble(&value), 0.0);
27462751
value_free(&value);
27472752
}
2753+
2754+
TEST_F(SensingBlocksTest, DaysSince2000)
2755+
{
2756+
auto targetMock = std::make_shared<TargetMock>();
2757+
2758+
ScriptBuilder builder(m_extension.get(), m_engine, targetMock);
2759+
builder.addBlock("sensing_dayssince2000");
2760+
Block *block = builder.currentBlock();
2761+
2762+
Compiler compiler(&m_engineMock, targetMock.get());
2763+
auto code = compiler.compile(block, Compiler::CodeType::Reporter);
2764+
Script script(targetMock.get(), block, &m_engineMock);
2765+
script.setCode(code);
2766+
Thread thread(targetMock.get(), &m_engineMock, &script);
2767+
2768+
std::chrono::system_clock::time_point time(std::chrono::milliseconds(1011243120562)); // Jan 17 2002 04:52:00
2769+
EXPECT_CALL(m_clock, currentSystemTime()).WillOnce(Return(time));
2770+
2771+
ValueData value = thread.runReporter();
2772+
ASSERT_EQ(value_toDouble(&value), 747.20278428240817);
2773+
value_free(&value);
2774+
}

0 commit comments

Comments
 (0)