|
// retoor <retoor@molodetz.nl>
|
|
|
|
import "datetime" for DateTime, Duration
|
|
|
|
System.print("=== DateTime Module Demo ===\n")
|
|
|
|
System.print("--- Current Date/Time ---")
|
|
var now = DateTime.now()
|
|
System.print("Now: %(now)")
|
|
System.print("Timestamp: %(now.timestamp)")
|
|
|
|
System.print("\n--- Date Components ---")
|
|
System.print("Year: %(now.year)")
|
|
System.print("Month: %(now.month)")
|
|
System.print("Day: %(now.day)")
|
|
System.print("Hour: %(now.hour)")
|
|
System.print("Minute: %(now.minute)")
|
|
System.print("Second: %(now.second)")
|
|
System.print("Day of Week: %(now.dayOfWeek) (0=Sunday)")
|
|
System.print("Day of Year: %(now.dayOfYear)")
|
|
System.print("Is DST: %(now.isDst)")
|
|
|
|
System.print("\n--- Creating from Timestamp ---")
|
|
var epoch = DateTime.fromTimestamp(0)
|
|
System.print("Unix Epoch: %(epoch)")
|
|
|
|
var specificTime = DateTime.fromTimestamp(1704067200)
|
|
System.print("Jan 1, 2024 00:00 UTC: %(specificTime)")
|
|
|
|
System.print("\n--- Formatting with strftime ---")
|
|
System.print("ISO 8601: %(now.toIso8601)")
|
|
System.print("Date (Y-m-d): %(now.format("\%Y-\%m-\%d"))")
|
|
System.print("Time (H:M:S): %(now.format("\%H:\%M:\%S"))")
|
|
System.print("Full date: %(now.format("\%A, \%B \%d, \%Y"))")
|
|
System.print("Time with AM/PM: %(now.format("\%I:\%M \%p"))")
|
|
|
|
System.print("\n--- Duration Class ---")
|
|
var oneHour = Duration.fromHours(1)
|
|
var twoMinutes = Duration.fromMinutes(2)
|
|
var thirtySeconds = Duration.fromSeconds(30)
|
|
var oneDay = Duration.fromDays(1)
|
|
|
|
System.print("1 hour in seconds: %(oneHour.seconds)")
|
|
System.print("2 minutes in milliseconds: %(twoMinutes.milliseconds)")
|
|
System.print("30 seconds in minutes: %(thirtySeconds.minutes)")
|
|
System.print("1 day in hours: %(oneDay.hours)")
|
|
|
|
System.print("\n--- Duration Arithmetic ---")
|
|
var combined = oneHour + twoMinutes + thirtySeconds
|
|
System.print("1 hour + 2 minutes + 30 seconds: %(combined)")
|
|
System.print(" in seconds: %(combined.seconds)")
|
|
|
|
var doubled = oneHour * 2
|
|
System.print("1 hour * 2: %(doubled)")
|
|
|
|
var diff = oneHour - twoMinutes
|
|
System.print("1 hour - 2 minutes: %(diff)")
|
|
|
|
System.print("\n--- DateTime Arithmetic ---")
|
|
var tomorrow = now + Duration.fromDays(1)
|
|
System.print("Now: %(now)")
|
|
System.print("Tomorrow: %(tomorrow)")
|
|
|
|
var nextHour = now + Duration.fromHours(1)
|
|
System.print("Next hour: %(nextHour)")
|
|
|
|
var yesterday = now - Duration.fromDays(1)
|
|
System.print("Yesterday: %(yesterday)")
|
|
|
|
System.print("\n--- DateTime Comparison ---")
|
|
System.print("tomorrow > now: %(tomorrow > now)")
|
|
System.print("yesterday < now: %(yesterday < now)")
|
|
System.print("now == now: %(now == now)")
|
|
|
|
System.print("\n--- Difference Between DateTimes ---")
|
|
var timeDiff = tomorrow - now
|
|
System.print("Difference (tomorrow - now): %(timeDiff)")
|
|
|
|
System.print("\n--- Practical Example: Event Timing ---")
|
|
var eventStart = DateTime.now()
|
|
var eventDuration = Duration.fromHours(2) + Duration.fromMinutes(30)
|
|
var eventEnd = eventStart + eventDuration
|
|
|
|
System.print("Event starts: %(eventStart.format("\%H:\%M"))")
|
|
System.print("Event duration: %(eventDuration)")
|
|
System.print("Event ends: %(eventEnd.format("\%H:\%M"))")
|