Skip to content

Commit

Permalink
extract to_time() converter
Browse files Browse the repository at this point in the history
  • Loading branch information
o3bvv committed Jan 3, 2016
1 parent 4102105 commit caabd8f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
9 changes: 9 additions & 0 deletions il2fb/parsers/mission/converters.py
@@ -1,5 +1,8 @@
# -*- coding: utf-8 -*-

import datetime
import math

from il2fb.commons import Skills, UnitTypes
from il2fb.commons.organization import AirForces, Belligerents

Expand Down Expand Up @@ -46,3 +49,9 @@ def to_air_force(value):
return AirForces.vvs_rkka
elif value:
return AirForces.get_by_value(value)


def to_time(value):
time = float(value)
minutes, hours = math.modf(time)
return datetime.time(int(hours), int(minutes * 60))
12 changes: 2 additions & 10 deletions il2fb/parsers/mission/sections/main.py
@@ -1,11 +1,8 @@
# -*- coding: utf-8 -*-

import datetime
import math

from il2fb.commons.weather import Conditions

from ..converters import to_belligerent
from ..converters import to_belligerent, to_time
from . import ValuesParser


Expand All @@ -31,7 +28,7 @@ def clean(self):
return {
'location_loader': self.data['MAP'],
'time': {
'value': self._to_time(self.data['TIME']),
'value': to_time(self.data['TIME']),
'is_fixed': 'TIMECONSTANT' in self.data,
},
'weather_conditions': Conditions.get_by_value(weather_conditions),
Expand All @@ -43,8 +40,3 @@ def clean(self):
'fixed_weapons': 'WEAPONSCONSTANT' in self.data,
},
}

def _to_time(self, value):
time = float(value)
minutes, hours = math.modf(time)
return datetime.time(int(hours), int(minutes * 60))
5 changes: 5 additions & 0 deletions tests/test_converters.py
@@ -1,5 +1,6 @@
# -*- coding: utf-8 -*-

import datetime
import unittest

from il2fb.commons import Skills, UnitTypes
Expand All @@ -8,6 +9,7 @@
from il2fb.parsers.mission.constants import NULL
from il2fb.parsers.mission.converters import (
to_bool, to_belligerent, to_skill, to_unit_type, to_air_force,
to_time,
)


Expand All @@ -33,3 +35,6 @@ def test_to_air_force(self):
self.assertIsNone(to_air_force(""))
self.assertEqual(to_air_force(NULL), AirForces.vvs_rkka)
self.assertEqual(to_air_force("nn"), AirForces.none)

def test_to_time(self):
self.assertEqual(to_time("11.75"), datetime.time(11, 45))

0 comments on commit caabd8f

Please sign in to comment.