Skip to content

Commit

Permalink
Operators between power, energy and time_duration
Browse files Browse the repository at this point in the history
  • Loading branch information
irwiss committed Nov 14, 2022
1 parent 2c4d051 commit 2bd7f28
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/units.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "units.h"

#include "calendar.h"
#include "json.h"
#include "string_formatter.h"

Expand Down Expand Up @@ -152,4 +153,30 @@ std::string display( const units::power v )
return std::to_string( mw ) + ' ' + pgettext( "energy unit: milliwatt", "mW" );
}

units::energy operator*( const units::power &power, const time_duration &time )
{
const int64_t mW = units::to_milliwatt<int64_t>( power );
const int64_t seconds = to_seconds<int64_t>( time );
return units::from_millijoule( mW * seconds );
}

units::energy operator*( const time_duration &time, const units::power &power )
{
return power * time;
}

units::power operator/( const units::energy &energy, const time_duration &time )
{
const int64_t mj = to_millijoule( energy );
const int64_t seconds = to_seconds<int64_t>( time );
return from_milliwatt( mj / seconds );
}

time_duration operator/( const units::energy &energy, const units::power &power )
{
const int64_t mj = to_millijoule( energy );
const int64_t mw = to_milliwatt( power );
return time_duration::from_seconds( mj / mw );
}

} // namespace units
9 changes: 9 additions & 0 deletions src/units.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include "translations.h"
#include "units_fwd.h" // IWYU pragma: export

class time_duration;

namespace units
{

Expand Down Expand Up @@ -1075,6 +1077,13 @@ static const std::vector<std::pair<std::string, temperature>> temperature_units
{ "K", 1_K }
}
};

units::energy operator*( const units::power &power, const time_duration &time );
units::energy operator*( const time_duration &time, const units::power &power );

units::power operator/( const units::energy &energy, const time_duration &time );
time_duration operator/( const units::energy &energy, const units::power &power );

} // namespace units

namespace detail
Expand Down
10 changes: 10 additions & 0 deletions tests/units_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,16 @@ TEST_CASE( "units_have_correct_ratios", "[units]" )
CHECK( 1_mW == units::from_milliwatt( 1 ) );
CHECK( 1_W == units::from_watt( 1 ) );
CHECK( 1_kW == units::from_kilowatt( 1 ) );

CHECK( 1_W * 1_seconds == 1_J );
CHECK( 1_seconds * 1_W == 1_J );
CHECK( 1_J / 1_seconds == 1_W );
CHECK( 1_J / 1_W == 1_seconds );
CHECK( 5_W * 5_minutes == 1.5_kJ );
CHECK( -5_W * 5_minutes == -1.5_kJ );
CHECK( ( 5_kJ / 5_W ) == ( 16_minutes + 40_seconds ) );
CHECK( ( -5_kJ / 5_W ) == -( 16_minutes + 40_seconds ) );
CHECK( ( 5_kJ / -5_W ) == -( 16_minutes + 40_seconds ) );
}

static units::energy parse_energy_quantity( const std::string &json )
Expand Down

0 comments on commit 2bd7f28

Please sign in to comment.