-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path12_datetime.ring
More file actions
73 lines (62 loc) · 1.64 KB
/
12_datetime.ring
File metadata and controls
73 lines (62 loc) · 1.64 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
12 - Date & Time
Use Python's datetime module from Ring.
*/
load "python.ring"
py_init()
# Current date and time
py_exec("
from datetime import datetime, timedelta
now = datetime.now()
_now_str = now.strftime('%Y-%m-%d %H:%M:%S')
_date = now.strftime('%A, %B %d, %Y')
_time = now.strftime('%I:%M %p')
")
? "=== Current Date & Time ==="
? "Timestamp: " + py_get("_now_str")
? "Date: " + py_get("_date")
? "Time: " + py_get("_time")
# Date arithmetic
? ""
? "=== Date Arithmetic ==="
py_exec("
today = datetime.now()
future = today + timedelta(days=30)
past = today - timedelta(days=90)
_future = future.strftime('%Y-%m-%d')
_past = past.strftime('%Y-%m-%d')
# Days until new year
ny = datetime(today.year + 1, 1, 1)
_days_to_ny = (ny - today).days
")
? "30 days from now: " + py_get("_future")
? "90 days ago: " + py_get("_past")
? "Days to new year: " + py_get("_days_to_ny")
# Parsing dates
? ""
? "=== Parsing Dates ==="
py_exec("
dates = ['2024-01-15', '2023-06-30', '2025-12-25']
parsed = []
for d in dates:
dt = datetime.strptime(d, '%Y-%m-%d')
parsed.append({
'input': d,
'day_name': dt.strftime('%A'),
'day_of_year': dt.timetuple().tm_yday,
})
")
parsed = py_get("parsed")
for row in parsed
if islist(row)
input = "" day = "" doy = ""
for pair in row
if islist(pair) and len(pair) = 2
if pair[1] = "input" input = pair[2] ok
if pair[1] = "day_name" day = pair[2] ok
if pair[1] = "day_of_year" doy = pair[2] ok
ok
next
? input + " → " + day + " (day " + doy + ")"
ok
next