File tree Expand file tree Collapse file tree 2 files changed +50
-0
lines changed
Expand file tree Collapse file tree 2 files changed +50
-0
lines changed Original file line number Diff line number Diff line change 1+ """
2+ Define functions to handle dates nad times in climada
3+ """
4+ import datetime as dt
5+
6+ def date_to_str (date ):
7+ """ Compute date string in ISO format from input datetime ordinal int.
8+
9+ Parameters:
10+ date (int): input datetime ordinal
11+
12+ Returns:
13+ str
14+ """
15+ return dt .date .fromordinal (date ).isoformat ()
16+
17+ def str_to_date (date ):
18+ """ Compute datetime ordinal int from input date string in ISO format.
19+
20+ Parameters:
21+ date (str): idate string in ISO format, e.g. '2018-04-06'
22+
23+ Returns:
24+ int
25+ """
26+ year , mounth , day = (int (val ) for val in date .split ('-' ))
27+ return dt .date (year , mounth , day ).toordinal ()
Original file line number Diff line number Diff line change 1+ """
2+ Test of dates_times module
3+ """
4+ import datetime as dt
5+ import unittest
6+
7+ import climada .util .dates_times as u_dt
8+
9+ class TestDateString (unittest .TestCase ):
10+ """Test date functions """
11+ def test_date_to_str_pass (self ):
12+ """ Test _date_to_str function"""
13+ ordinal_date = dt .datetime .toordinal (dt .datetime (2018 , 4 , 6 ))
14+ self .assertEqual ('2018-04-06' , u_dt .date_to_str (ordinal_date ))
15+
16+ def test_str_to_date_pass (self ):
17+ """ Test _date_to_str function"""
18+ date = 730000
19+ self .assertEqual (u_dt .str_to_date (u_dt .date_to_str (date )), date )
20+
21+ # Execute Tests
22+ TESTS = unittest .TestLoader ().loadTestsFromTestCase (TestDateString )
23+ unittest .TextTestRunner (verbosity = 2 ).run (TESTS )
You can’t perform that action at this time.
0 commit comments