xref: /aosp_15_r20/external/fonttools/Tests/misc/timeTools_test.py (revision e1fe3e4ad2793916b15cccdc4a7da52a7e1dd0e9)
1from fontTools.misc.timeTools import (
2    asctime,
3    timestampNow,
4    timestampToString,
5    timestampFromString,
6    epoch_diff,
7)
8import os
9import time
10import locale
11import pytest
12
13
14def test_asctime():
15    assert isinstance(asctime(), str)
16    assert asctime(time.gmtime(0)) == "Thu Jan  1 00:00:00 1970"
17
18
19def test_source_date_epoch():
20    os.environ["SOURCE_DATE_EPOCH"] = "150687315"
21    assert timestampNow() + epoch_diff == 150687315
22
23    # Check that malformed value fail, any better way?
24    os.environ["SOURCE_DATE_EPOCH"] = "ABCDEFGHI"
25    with pytest.raises(ValueError):
26        timestampNow()
27
28    del os.environ["SOURCE_DATE_EPOCH"]
29    assert timestampNow() + epoch_diff != 150687315
30
31
32# test for issue #1838
33def test_date_parsing_with_locale():
34    l = locale.getlocale(locale.LC_TIME)
35    try:
36        locale.setlocale(locale.LC_TIME, "de_DE.utf8")
37    except locale.Error:
38        pytest.skip("Locale de_DE not available")
39
40    try:
41        assert timestampFromString(timestampToString(timestampNow()))
42    finally:
43        locale.setlocale(locale.LC_TIME, l)
44