Skip to content

Commit a85a4b7

Browse files
authored
Created the gmtime entry for python (#7620)
* Created the gmtime entry for python * Update gmtime.md * minor wording fix * Apply suggestion from @avdhoottt ---------
1 parent e4e9092 commit a85a4b7

File tree

1 file changed

+69
-0
lines changed
  • content/python/concepts/time-module/terms/gmtime

1 file changed

+69
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
---
2+
Title: '.gmtime()'
3+
Description: 'Converts a time expressed in seconds since the epoch to a `struct_time` in UTC.'
4+
Subjects:
5+
- 'Computer Science'
6+
- 'Data Science'
7+
- 'Web Development'
8+
Tags:
9+
- 'Constants'
10+
- 'Methods'
11+
CatalogContent:
12+
- 'learn-python-3'
13+
- 'paths/computer-science'
14+
---
15+
16+
The **`gmtime()`** function converts a time value (in seconds since the epoch, or the current time if not provided) into a `struct_time` representation in Coordinated Universal Time (UTC).
17+
18+
## Syntax
19+
20+
```pseudo
21+
time.gmtime([seconds])
22+
```
23+
24+
**Parameters:**
25+
26+
- `seconds` (optional, float or int): Number of seconds since the epoch (January 1, 1970, 00:00:00 UTC). If omitted, the current time is used.
27+
28+
**Return value:**
29+
30+
Returns a `time.struct_time` object in Coordinated Universal Time (UTC) with the attributes: `(tm_year, tm_mon, tm_mday, tm_hour, tm_min, tm_sec, tm_wday, tm_yday, tm_isdst)`
31+
32+
## Example
33+
34+
In this example, the current UTC time is retrieved using `gmtime()`, and then a specific date (January 1, 2020) is converted into its UTC `struct_time` representation:
35+
36+
```py
37+
import time
38+
import calendar
39+
40+
# Get the current time in UTC
41+
current_time = time.gmtime()
42+
print("Current UTC time:", current_time)
43+
44+
# Convert a specific time (e.g., 1st January 2020) to UTC
45+
specific_time = calendar.timegm((2020, 1, 1, 0, 0, 0, 0, 0, 0))
46+
utc_time = time.gmtime(specific_time)
47+
print("Specific UTC time:", utc_time)
48+
```
49+
50+
The possible output of this code is:
51+
52+
```shell
53+
Current UTC time: time.struct_time(tm_year=2025, tm_mon=9, tm_mday=23, tm_hour=11, tm_min=12, tm_sec=32, tm_wday=1, tm_yday=266, tm_isdst=0)
54+
Specific UTC time: time.struct_time(tm_year=2020, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=1, tm_isdst=0)
55+
```
56+
57+
> **Note:** The exact output for the current time will vary depending on when the code is executed.
58+
59+
## Codebyte Example
60+
61+
In this example, 0 seconds since the epoch is passed to `gmtime()`, which returns the epoch start time in UTC:
62+
63+
```codebyte/python
64+
import time
65+
66+
# Passing the seconds elapsed as an argument to this method
67+
gt = time.gmtime(0)
68+
print("The epoch of the system:", gt)
69+
```

0 commit comments

Comments
 (0)