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