Hướng dẫn python get utc offset in hours - python được bù đắp utc trong giờ

time.timezone:

Show
    import time
    
    print -time.timezone
    

    Nó in phần bù UTC tính bằng giây (để tính đến thời gian tiết kiệm ánh sáng ban ngày (DST) Xem thời gian.altzone:

    is_dst = time.daylight and time.localtime().tm_isdst > 0
    utc_offset = - (time.altzone if is_dst else time.timezone)
    

    Trong trường hợp bù UTC được xác định thông qua: "Để có được thời gian địa phương, hãy thêm độ lệch UTC vào thời gian UTC."

    Trong Python 3.3+ có thuộc tính tm_gmtoff nếu thư viện cơ bản C hỗ trợ nó:

    utc_offset = time.localtime().tm_gmtoff
    

    Lưu ý: time.daylight có thể cho kết quả sai trong một số trường hợp cạnh.

    tm_gmtoff được sử dụng tự động bởi DateTime nếu nó có sẵn trên Python 3.3+:

    from datetime import datetime, timedelta, timezone
    
    d = datetime.now(timezone.utc).astimezone()
    utc_offset = d.utcoffset() // timedelta(seconds=1)
    

    Để có được sự bù đắp UTC hiện tại theo cách giải quyết vấn đề time.daylight và hoạt động ngay cả khi tm_gmtoff không có sẵn, có thể sử dụng đề xuất của @JTS để tạo ra thời gian tại địa phương và UTC:

    import time
    from datetime import datetime
    
    ts = time.time()
    utc_offset = (datetime.fromtimestamp(ts) -
                  datetime.utcfromtimestamp(ts)).total_seconds()
    

    Để có được sự bù đắp cho UTC cho các ngày trong quá khứ/tương lai, có thể sử dụng thời gian

    is_dst = time.daylight and time.localtime().tm_isdst > 0
    utc_offset = - (time.altzone if is_dst else time.timezone)
    
    1:

    from datetime import datetime
    from tzlocal import get_localzone # $ pip install tzlocal
    
    tz = get_localzone() # local timezone 
    d = datetime.now(tz) # or some other local date 
    utc_offset = d.utcoffset().total_seconds()
    

    Nó hoạt động trong quá trình chuyển đổi DST, nó hoạt động cho các ngày trong quá khứ/tương lai ngay cả khi timezone địa phương có độ lệch UTC khác nhau tại thời điểm, ví dụ: thời gian châu Âu/Moscow trong giai đoạn 2010-2015.

    from datetime import datetime, timedelta, timezone
    
    d = datetime.now(timezone.utc).astimezone()
    utc_offset = d.utcoffset() // timedelta(seconds=1)
    
    2
    is_dst = time.daylight and time.localtime().tm_isdst > 0
    utc_offset = - (time.altzone if is_dst else time.timezone)
    
    9
    from datetime import datetime, timedelta, timezone
    
    d = datetime.now(timezone.utc).astimezone()
    utc_offset = d.utcoffset() // timedelta(seconds=1)
    
    4
    from datetime import datetime, timedelta, timezone
    
    d = datetime.now(timezone.utc).astimezone()
    utc_offset = d.utcoffset() // timedelta(seconds=1)
    
    5
    from datetime import datetime, timedelta, timezone
    
    d = datetime.now(timezone.utc).astimezone()
    utc_offset = d.utcoffset() // timedelta(seconds=1)
    
    6

    from datetime import datetime, timedelta, timezone
    
    d = datetime.now(timezone.utc).astimezone()
    utc_offset = d.utcoffset() // timedelta(seconds=1)
    
    7
    is_dst = time.daylight and time.localtime().tm_isdst > 0
    utc_offset = - (time.altzone if is_dst else time.timezone)
    
    9
    from datetime import datetime, timedelta, timezone
    
    d = datetime.now(timezone.utc).astimezone()
    utc_offset = d.utcoffset() // timedelta(seconds=1)
    
    9

    Thời gian trước UTC bởi: 5:30:00

  • Ví dụ 3: Tìm thời gian bù đắp
  • from datetime import datetime, timedelta, timezone
    
    d = datetime.now(timezone.utc).astimezone()
    utc_offset = d.utcoffset() // timedelta(seconds=1)
    
    2
    is_dst = time.daylight and time.localtime().tm_isdst > 0
    utc_offset = - (time.altzone if is_dst else time.timezone)
    
    9
    from datetime import datetime, timedelta, timezone
    
    d = datetime.now(timezone.utc).astimezone()
    utc_offset = d.utcoffset() // timedelta(seconds=1)
    
    4
    from datetime import datetime
    from tzlocal import get_localzone # $ pip install tzlocal
    
    tz = get_localzone() # local timezone 
    d = datetime.now(tz) # or some other local date 
    utc_offset = d.utcoffset().total_seconds()
    
    6
    from datetime import datetime, timedelta, timezone
    
    d = datetime.now(timezone.utc).astimezone()
    utc_offset = d.utcoffset() // timedelta(seconds=1)
    
    6
  • from datetime import datetime, timedelta, timezone
    
    d = datetime.now(timezone.utc).astimezone()
    utc_offset = d.utcoffset() // timedelta(seconds=1)
    
    2
    is_dst = time.daylight and time.localtime().tm_isdst > 0
    utc_offset = - (time.altzone if is_dst else time.timezone)
    
    9
    from datetime import datetime, timedelta, timezone
    
    d = datetime.now(timezone.utc).astimezone()
    utc_offset = d.utcoffset() // timedelta(seconds=1)
    
    4
    from datetime import datetime, timedelta, timezone
    
    d = datetime.now(timezone.utc).astimezone()
    utc_offset = d.utcoffset() // timedelta(seconds=1)
    
    5
    from datetime import datetime, timedelta, timezone
    
    d = datetime.now(timezone.utc).astimezone()
    utc_offset = d.utcoffset() // timedelta(seconds=1)
    
    6

    from datetime import datetime, timedelta, timezone
    
    d = datetime.now(timezone.utc).astimezone()
    utc_offset = d.utcoffset() // timedelta(seconds=1)
    
    7
    is_dst = time.daylight and time.localtime().tm_isdst > 0
    utc_offset = - (time.altzone if is_dst else time.timezone)
    
    9
    from datetime import datetime, timedelta, timezone
    
    d = datetime.now(timezone.utc).astimezone()
    utc_offset = d.utcoffset() // timedelta(seconds=1)
    
    9

    Thời gian trước UTC bởi: 5:30:00

    Ví dụ 3: Tìm thời gian bù đắputcoffset() function is used to return a timedelta object that represents the difference between the local time and UTC time.

    • from datetime import datetime, timedelta, timezone
      
      d = datetime.now(timezone.utc).astimezone()
      utc_offset = d.utcoffset() // timedelta(seconds=1)
      
      2
      is_dst = time.daylight and time.localtime().tm_isdst > 0
      utc_offset = - (time.altzone if is_dst else time.timezone)
      
      9
      from datetime import datetime, timedelta, timezone
      
      d = datetime.now(timezone.utc).astimezone()
      utc_offset = d.utcoffset() // timedelta(seconds=1)
      
      4
      from datetime import datetime
      from tzlocal import get_localzone # $ pip install tzlocal
      
      tz = get_localzone() # local timezone 
      d = datetime.now(tz) # or some other local date 
      utc_offset = d.utcoffset().total_seconds()
      
      6
      from datetime import datetime, timedelta, timezone
      
      d = datetime.now(timezone.utc).astimezone()
      utc_offset = d.utcoffset() // timedelta(seconds=1)
      
      6
    • Ở đây phạm vi của utcoffset là thời gian (giờ = 24)
    • Nếu phần bù là ở phía đông của UTC, thì nó được coi là tích cực và nếu phần bù là phía tây của UTC, thì nó được coi là âm. Vì có 24 giờ trong một ngày, -Timedelta (24) và Timedelta (24) là những giá trị lớn nhất có thể.

    Cú pháp: utcoffset ()utcoffset()

    Tham số: Hàm này không chấp nhận bất kỳ tham số nào.This function does not accept any parameter.

    Giá trị trả về: Hàm này trả về một đối tượng TimeDelta đại diện cho sự khác biệt giữa thời gian địa phương và thời gian UTC.This function returns a timedelta object representing the difference between the local time and UTC time.

    Ví dụ 1: Ở đây, đầu ra không có vì bây giờ () hàm trả về ngày và giờ ở định dạng UTC, do đó sự khác biệt giữa thời gian cục bộ i.e, được trả lại bởi hàm bây giờ () và thời gian UTC không phải là không có.Here output is None because now() function returns date and time in UTC format, hence the difference between the local time i.e, returned by now() function and UTC time is none.

    Python3

    is_dst = time.daylight and time.localtime().tm_isdst > 0
    utc_offset = - (time.altzone if is_dst else time.timezone)
    
    2
    is_dst = time.daylight and time.localtime().tm_isdst > 0
    utc_offset = - (time.altzone if is_dst else time.timezone)
    
    3
    is_dst = time.daylight and time.localtime().tm_isdst > 0
    utc_offset = - (time.altzone if is_dst else time.timezone)
    
    4
    is_dst = time.daylight and time.localtime().tm_isdst > 0
    utc_offset = - (time.altzone if is_dst else time.timezone)
    
    5

    is_dst = time.daylight and time.localtime().tm_isdst > 0
    utc_offset = - (time.altzone if is_dst else time.timezone)
    
    4
    is_dst = time.daylight and time.localtime().tm_isdst > 0
    utc_offset = - (time.altzone if is_dst else time.timezone)
    
    1

    is_dst = time.daylight and time.localtime().tm_isdst > 0
    utc_offset = - (time.altzone if is_dst else time.timezone)
    
    8
    is_dst = time.daylight and time.localtime().tm_isdst > 0
    utc_offset = - (time.altzone if is_dst else time.timezone)
    
    9
    utc_offset = time.localtime().tm_gmtoff
    
    0

    utc_offset = time.localtime().tm_gmtoff
    
    1
    utc_offset = time.localtime().tm_gmtoff
    
    2

    Output:

    Không có

    Ví dụ 2: Tìm phần bù thời gianFinding the offset of time

    Python3

    is_dst = time.daylight and time.localtime().tm_isdst > 0
    utc_offset = - (time.altzone if is_dst else time.timezone)
    
    2
    is_dst = time.daylight and time.localtime().tm_isdst > 0
    utc_offset = - (time.altzone if is_dst else time.timezone)
    
    3
    is_dst = time.daylight and time.localtime().tm_isdst > 0
    utc_offset = - (time.altzone if is_dst else time.timezone)
    
    4
    is_dst = time.daylight and time.localtime().tm_isdst > 0
    utc_offset = - (time.altzone if is_dst else time.timezone)
    
    5

    is_dst = time.daylight and time.localtime().tm_isdst > 0
    utc_offset = - (time.altzone if is_dst else time.timezone)
    
    4
    is_dst = time.daylight and time.localtime().tm_isdst > 0
    utc_offset = - (time.altzone if is_dst else time.timezone)
    
    1

    is_dst = time.daylight and time.localtime().tm_isdst > 0
    utc_offset = - (time.altzone if is_dst else time.timezone)
    
    8
    is_dst = time.daylight and time.localtime().tm_isdst > 0
    utc_offset = - (time.altzone if is_dst else time.timezone)
    
    9
    utc_offset = time.localtime().tm_gmtoff
    
    0

    Không có

    Ví dụ 2: Tìm phần bù thời gian

    utc_offset = time.localtime().tm_gmtoff
    
    1
    import time
    from datetime import datetime
    
    ts = time.time()
    utc_offset = (datetime.fromtimestamp(ts) -
                  datetime.utcfromtimestamp(ts)).total_seconds()
    
    1
    import time
    from datetime import datetime
    
    ts = time.time()
    utc_offset = (datetime.fromtimestamp(ts) -
                  datetime.utcfromtimestamp(ts)).total_seconds()
    
    2
    import time
    from datetime import datetime
    
    ts = time.time()
    utc_offset = (datetime.fromtimestamp(ts) -
                  datetime.utcfromtimestamp(ts)).total_seconds()
    
    3

    Output:

    utc_offset = time.localtime().tm_gmtoff
    
    9
    is_dst = time.daylight and time.localtime().tm_isdst > 0
    utc_offset = - (time.altzone if is_dst else time.timezone)
    
    9
    utc_offset = time.localtime().tm_gmtoff
    
    0

    from datetime import datetime, timedelta, timezone
    
    d = datetime.now(timezone.utc).astimezone()
    utc_offset = d.utcoffset() // timedelta(seconds=1)
    
    2
    is_dst = time.daylight and time.localtime().tm_isdst > 0
    utc_offset = - (time.altzone if is_dst else time.timezone)
    
    9
    from datetime import datetime, timedelta, timezone
    
    d = datetime.now(timezone.utc).astimezone()
    utc_offset = d.utcoffset() // timedelta(seconds=1)
    
    4
    from datetime import datetime, timedelta, timezone
    
    d = datetime.now(timezone.utc).astimezone()
    utc_offset = d.utcoffset() // timedelta(seconds=1)
    
    5
    from datetime import datetime, timedelta, timezone
    
    d = datetime.now(timezone.utc).astimezone()
    utc_offset = d.utcoffset() // timedelta(seconds=1)
    
    6
    Finding time offset

    Python3

    is_dst = time.daylight and time.localtime().tm_isdst > 0
    utc_offset = - (time.altzone if is_dst else time.timezone)
    
    2
    is_dst = time.daylight and time.localtime().tm_isdst > 0
    utc_offset = - (time.altzone if is_dst else time.timezone)
    
    3
    is_dst = time.daylight and time.localtime().tm_isdst > 0
    utc_offset = - (time.altzone if is_dst else time.timezone)
    
    4
    is_dst = time.daylight and time.localtime().tm_isdst > 0
    utc_offset = - (time.altzone if is_dst else time.timezone)
    
    5

    is_dst = time.daylight and time.localtime().tm_isdst > 0
    utc_offset = - (time.altzone if is_dst else time.timezone)
    
    4
    is_dst = time.daylight and time.localtime().tm_isdst > 0
    utc_offset = - (time.altzone if is_dst else time.timezone)
    
    1

    is_dst = time.daylight and time.localtime().tm_isdst > 0
    utc_offset = - (time.altzone if is_dst else time.timezone)
    
    8
    is_dst = time.daylight and time.localtime().tm_isdst > 0
    utc_offset = - (time.altzone if is_dst else time.timezone)
    
    9
    utc_offset = time.localtime().tm_gmtoff
    
    0

    Không có

    Ví dụ 2: Tìm phần bù thời gian

    utc_offset = time.localtime().tm_gmtoff
    
    1
    import time
    from datetime import datetime
    
    ts = time.time()
    utc_offset = (datetime.fromtimestamp(ts) -
                  datetime.utcfromtimestamp(ts)).total_seconds()
    
    1
    import time
    from datetime import datetime
    
    ts = time.time()
    utc_offset = (datetime.fromtimestamp(ts) -
                  datetime.utcfromtimestamp(ts)).total_seconds()
    
    2
    import time
    from datetime import datetime
    
    ts = time.time()
    utc_offset = (datetime.fromtimestamp(ts) -
                  datetime.utcfromtimestamp(ts)).total_seconds()
    
    3

    Output:

    utc_offset = time.localtime().tm_gmtoff
    
    9
    is_dst = time.daylight and time.localtime().tm_isdst > 0
    utc_offset = - (time.altzone if is_dst else time.timezone)
    
    9
    utc_offset = time.localtime().tm_gmtoff
    
    0