File: neo_ww_calculator.py

package info (click to toggle)
intel-compute-runtime 25.27.34303.9-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 76,556 kB
  • sloc: cpp: 886,192; lisp: 3,433; sh: 715; makefile: 159; python: 21
file content (41 lines) | stat: -rwxr-xr-x 1,117 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env python3

#
# Copyright (C) 2021 Intel Corporation
#
# SPDX-License-Identifier: MIT
#

import sys

from datetime import datetime, timezone

def convert_ww(epoch):
    dt = datetime.fromtimestamp(epoch, timezone.utc)

    # get some info from epoch
    yr = int(dt.strftime("%y"))
    doy = int(dt.strftime("%j"))
    # and day of week for Jan 1st
    dow1 = int(datetime(dt.year, 1, 1).strftime("%w"))

    # number of days in a year
    _is_leap = yr % 400 == 0 or (yr % 4 == 0 and yr % 100 != 0)
    _y_days = 366 if _is_leap else 365

    _doy = doy - 1 + dow1           # shift day of year to simulate Jan 1st as Sunday
    _ww = int(_doy / 7) + 1         # get workweek
    _wd = int(_doy % 7)             # get days of week
    _y_days = _y_days + dow1        # adjusted number of days in year
    _w_days = _y_days - _doy + _wd  # numer of week days days to end of year

    if _w_days < 7:
        # last week has less than 7 days
        yr = yr + 1
        _ww = 1

    print("{:02d}.{:02d}".format(yr, _ww))
    return 0

if __name__ == '__main__':
    sys.exit(convert_ww(int(sys.argv[1])))