Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
dream
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
1
Issues
1
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
dream
Commits
4ebfa3e9
Commit
4ebfa3e9
authored
Feb 16, 2015
by
Jérome Perrin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Introduce a mixin plugin with time unit handling helper functions
parent
d80adf94
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
49 additions
and
0 deletions
+49
-0
dream/plugins/TimeSupport.py
dream/plugins/TimeSupport.py
+49
-0
No files found.
dream/plugins/TimeSupport.py
0 → 100644
View file @
4ebfa3e9
from
datetime
import
datetime
from
datetime
import
timedelta
import
random
from
pprint
import
pformat
from
dream.plugins
import
plugin
class
TimeSupportMixin
(
object
):
"""Helper methods to handle time units.
"""
initialized
=
False
def
initializeTimeSupport
(
self
,
data
):
self
.
timeUnitPerDay
=
data
[
'general'
][
'timeUnitPerDay'
]
self
.
dateFormat
=
data
[
'general'
].
get
(
'dateFormat'
,
'%Y/%m/%d %H:%M:%S'
)
# Convert simulation 0 time to real world time
self
.
now
=
datetime
.
strptime
(
data
[
'general'
][
'currentDate'
],
self
.
dateFormat
)
self
.
initialized
=
True
def
convertToRealWorldTime
(
self
,
simulation_time
):
"""Convert simulation clock time to real world time, as python datetime object.
"""
assert
self
.
initialized
,
"initializeTimeSupport has not been called"
return
self
.
now
+
timedelta
(
days
=
simulation_time
/
self
.
timeUnitPerDay
)
def
convertToSimulationTime
(
self
,
real_world_time
):
"""Convert real world time (as python datetime object) to simulation clock time.
"""
assert
self
.
initialized
,
"initializeTimeSupport has not been called"
raise
NotImplementedError
def
getTimeUnitText
(
self
):
"""Return the time unit as text.
"""
assert
self
.
initialized
,
"initializeTimeSupport has not been called"
if
self
.
timeUnitPerDay
==
24
*
60
:
return
'minute'
if
self
.
timeUnitPerDay
==
24
:
return
'hour'
if
self
.
timeUnitPerDay
==
1
:
return
'day'
if
self
.
timeUnitPerDay
==
1
/
7.
:
return
'week'
if
self
.
timeUnitPerDay
==
1
/
30.
:
return
'month'
if
timeUnitPerDay
==
1
/
360.
:
return
'year'
raise
ValueError
(
"Unsupported time unit %s"
%
self
.
timeUnitPerDay
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment