Skip to content

Commit 535570e

Browse files
authored
feat: utc() converter (#29)
1 parent 79d5365 commit 535570e

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

python_carbon/__init__.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,13 @@ def diffInYears(self, carbon: 'Carbon') -> int:
471471

472472
# TODO: Difference for humans
473473

474+
##############
475+
# Converters #
476+
##############
477+
478+
def utc(self) -> 'Carbon':
479+
return Carbon(self._date.astimezone(tzutc()))
480+
474481
#############
475482
# Modifiers #
476483
#############

tests/test_python_carbon.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,20 @@
44

55

66
class test_python_carbon(unittest.TestCase):
7-
def test_carbon_methods(self) -> None:
7+
def test_parse_and_equality(self) -> None:
88
dt = Carbon.parse('2021-08-16 01:02:03')
99
dt2 = Carbon.parse('2021-08-16 01:02:03')
1010

1111
self.assertTrue(dt.equalTo(dt2))
1212

13+
def test_instantiation_now_and_constructor(self) -> None:
1314
dt = Carbon.now()
1415
self.assertIsInstance(dt, Carbon)
1516

1617
dt = Carbon() # equivalent to Carbon.now()
1718
self.assertIsInstance(dt, Carbon)
1819

20+
def test_create_format_and_getters(self) -> None:
1921
dt = Carbon.createFromFormat('%Y-%m-%d', '2021-08-18')
2022
self.assertEqual(dt.format('%Y-%m-%d'), '2021-08-18')
2123

@@ -29,6 +31,7 @@ def test_carbon_methods(self) -> None:
2931
self.assertEqual(dt.micro, 0)
3032
self.assertIsInstance(dt.timestamp, float)
3133

34+
def test_start_and_end_methods(self) -> None:
3235
dt = Carbon.now()
3336

3437
start_of_week = dt.startOfWeek()
@@ -60,6 +63,7 @@ def test_carbon_methods(self) -> None:
6063
self.assertEqual(end_of_hour.second, 59)
6164
self.assertEqual(end_of_hour.microsecond, 999999)
6265

66+
def test_add_sub_and_last_day_of_month(self) -> None:
6367
self.assertTrue(Carbon.parse('2021-08-31').isLastDayOfMonth())
6468

6569
anchor = Carbon.now()
@@ -68,6 +72,7 @@ def test_carbon_methods(self) -> None:
6872

6973
self.assertIsInstance(Carbon.timedelta(days=1), timedelta)
7074

75+
def test_difference_and_diffin_methods(self) -> None:
7176
reference = Carbon.parse('1992-04-17 17:00:00')
7277
now = Carbon.now()
7378

@@ -95,6 +100,7 @@ def test_carbon_methods(self) -> None:
95100
self.assertEqual(diff_seconds, diff_minutes * 60)
96101
self.assertEqual(diff_microseconds, diff_seconds * 1000)
97102

103+
def test_cookie_iso_and_calendar_methods(self) -> None:
98104
cookie_string = Carbon.utcnow().toCookieString()
99105
self.assertIsInstance(cookie_string, str)
100106
self.assertIn('UTC', cookie_string)
@@ -109,6 +115,7 @@ def test_carbon_methods(self) -> None:
109115
week_of_month = Carbon.now().getWeekOfMonth()
110116
self.assertIn(week_of_month, range(0, 7))
111117

118+
def test_start_end_of_month_year_and_quarters(self) -> None:
112119
start_of_month = Carbon.now().startOfMonth()
113120
end_of_month = Carbon.now().endOfMonth()
114121
start_of_year = Carbon.now().startOfYear()
@@ -125,14 +132,24 @@ def test_carbon_methods(self) -> None:
125132
self.assertTrue(all(len(q) == 3 for q in quarters))
126133
self.assertIn(quarter, range(0, 4))
127134

135+
def test_between_included_and_excluded(self) -> None:
128136
current = Carbon.now()
129137
yesterday = Carbon.yesterday()
130138
tomorrow = Carbon.tomorrow()
139+
131140
self.assertTrue(current.betweenIncluded(yesterday, tomorrow))
132141
self.assertTrue(current.betweenExcluded(yesterday, tomorrow))
133142
self.assertTrue(yesterday.betweenIncluded(yesterday, tomorrow))
134143
self.assertFalse(yesterday.betweenExcluded(yesterday, tomorrow))
135144

145+
def test_utc_converter(self) -> None:
146+
local_with_offset = Carbon.parse('2021-08-18T10:00:00+02:00')
147+
converted = local_with_offset.utc()
148+
149+
self.assertIsInstance(converted, Carbon)
150+
self.assertEqual(converted.toDatetime().tzname(), 'UTC')
151+
self.assertEqual(converted.toDateTimeString(), '2021-08-18 08:00:00')
152+
136153

137154
if __name__ == '__main__':
138155
unittest.main()

0 commit comments

Comments
 (0)