Skip to content

Commit

Permalink
Add TIME fingerprint
Browse files Browse the repository at this point in the history
  • Loading branch information
cwesson committed Apr 14, 2024
1 parent 9c23bbb commit 08473a9
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,12 @@ Instructions from dynamic Funges run with the same stack as the IP that called t

`TERM` [Terminal extension](http://www.rcfunge98.com/rcfunge2_manual.html#TERM).

`TIME` [Time and Date functions](http://www.rcfunge98.com/rcfunge2_manual.html#TIME).

`TOYS` [Standard Toys](https://github.com/catseye/Funge-98/blob/master/library/TOYS.markdown).

`TRDS`[IP travel in time and space](http://www.rcfunge98.com/rcfunge2_manual.html#TRDS), backward time travel not supported.

## Debugger
The Funge++ debugger, known as defunge, can be run on any Befunge program by specifying the `-g` command line argument.

Expand Down
2 changes: 2 additions & 0 deletions src/FingerprintStrategy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "FingerprintREFC.h"
#include "FingerprintSTRN.h"
#include "FingerprintSUBR.h"
#include "FingerprintTIME.h"
#include "FingerprintTOYS.h"
#include "FingerprintTRDS.h"
#include "FungeUniverse.h"
Expand Down Expand Up @@ -91,6 +92,7 @@ Fingerprint* FingerprintStrategy::loadBuiltin(uint64_t fingerprint){
case 0x52454643: fing = new FingerprintREFC(runner); break;
case 0x5354524E: fing = new FingerprintSTRN(runner); break;
case 0x53554252: fing = new FingerprintSUBR(runner); break;
case 0x54494D45: fing = new FingerprintTIME(runner); break;
case 0x544F5953: fing = new FingerprintTOYS(runner); break;
case 0x54524453: fing = new FingerprintTRDS(runner); break;
case FingerprintDynamic::ID:
Expand Down
68 changes: 68 additions & 0 deletions src/fingerprint/FingerprintTIME.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* @file FingerprintTIME.cpp
* Time and Date functions.
* @author Conlan Wesson
*/

#include "FingerprintTIME.h"
#include <chrono>

namespace Funge {

FingerprintTIME::FingerprintTIME(FungeRunner& r) :
Fingerprint(r, {'D', 'F', 'G', 'H', 'L', 'M', 'O', 'S', 'W', 'Y'}),
gmt(false)
{}

FungeError FingerprintTIME::execute(inst_t cmd){
const std::tm* t = getTime();
if(t == nullptr){
return ERROR_UNSPEC;
}
switch(cmd){
case 'D':
stack.top().push(t->tm_mday);
break;
case 'F':
stack.top().push(t->tm_yday);
break;
case 'G':
gmt = true;
break;
case 'H':
stack.top().push(t->tm_hour);
break;
case 'L':
gmt = false;
break;
case 'M':
stack.top().push(t->tm_min);
break;
case 'O':
stack.top().push(t->tm_mon+1);
break;
case 'S':
stack.top().push(t->tm_sec);
break;
case 'W':
stack.top().push(t->tm_wday+1);
break;
case 'Y':
stack.top().push(t->tm_year+1900);
break;
default:
return ERROR_UNIMP;
}
return ERROR_NONE;
}

const std::tm* FingerprintTIME::getTime(){
std::time_t t = std::time(nullptr);
if(gmt){
return std::gmtime(&t);
}else{
return std::localtime(&t);
}
}

}
26 changes: 26 additions & 0 deletions src/fingerprint/include/FingerprintTIME.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* @file FingerprintTIME.h
* Time and Date functions.
* @author Conlan Wesson
*/

#pragma once

#include "Fingerprint.h"

namespace Funge {

class FingerprintTIME : public Fingerprint {
public:
explicit FingerprintTIME(FungeRunner& r);
virtual ~FingerprintTIME() = default;

virtual FungeError execute(inst_t cmd) override;

private:
bool gmt;

const std::tm* getTime();
};

}

0 comments on commit 08473a9

Please sign in to comment.