File: timeutil.py

package info (click to toggle)
waagent 2.15.0.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,820 kB
  • sloc: python: 60,164; xml: 4,126; sh: 1,354; makefile: 22
file content (20 lines) | stat: -rw-r--r-- 1,001 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the Apache License.

import datetime

def create_utc_timestamp(dt):
    """
    Formats the given datetime, which must be timezone-aware and in UTC, as "YYYY-MM-DDTHH:MM:SS.ffffffZ".
    This is basically ISO-8601, but using "Z" (Zero offset) to represent the timezone offset (instead of "+00:00").
    The corresponding format for strftime/strptime is "%Y-%m-%dT%H:%M:%S.%fZ".
    """
    if dt.tzinfo is None:
        raise ValueError("The datetime must be timezone-aware")
    if dt.utcoffset() != datetime.timedelta(0):
        raise ValueError("The datetime must be in UTC")

    # We use isoformat() instead of strftime() since the later is limited to years >= 1900 in Python < 3.2.  We remove the
    # timezone information since we are using "Z" to represent UTC, and we force the microseconds to be 000000 when it is 0.
    return dt.replace(tzinfo=None).isoformat() + (".000000Z" if dt.microsecond == 0 else "Z")