Basic Time Usage

This example demonstrates the basic usage of the True-Core time module.

Example Code

Basic Time Usage Example
 1"""
 2Basic usage examples of the Time class demonstrating core functionality.
 3"""
 4
 5from true.time import Time, TimeFormat
 6
 7
 8def demo_basic_time_creation():
 9    """Demonstrate different ways to create Time objects."""
10    print("=== Basic Time Creation ===")
11
12    # Create time with current timestamp
13    current_time = Time.now()
14    print(f"Current time: {current_time}")
15
16    # Create time with specific timezone
17    ny_time = Time.now("America/New_York")
18    print(f"New York time: {ny_time}")
19
20    # Create time from timestamp
21    timestamp_time = Time(1234567890.0)
22    print(f"Time from timestamp: {timestamp_time}")
23
24    # Create time from string
25    string_time = Time("2024-02-20 15:30:00")
26    print(f"Time from string: {string_time}")
27
28
29def demo_time_formatting():
30    """Demonstrate different time formatting options."""
31    print("\n=== Time Formatting ===")
32
33    time_obj = Time.now()
34
35    # Different format types
36    print(f"24-hour format: {time_obj.format(TimeFormat.HOUR_24)}")
37    print(f"12-hour format: {time_obj.format(TimeFormat.HOUR_12)}")
38    print(f"ISO format: {time_obj.format(TimeFormat.ISO)}")
39
40    # Custom format
41    custom_format = "%Y-%m-%d %I:%M %p"
42    print(f"Custom format: {time_obj.format(custom_format=custom_format)}")
43
44    # Different locales
45    print(f"US locale: {time_obj.format(locale_name='en_US')}")
46    print(f"French locale: {time_obj.format(locale_name='fr_FR')}")
47
48
49def demo_time_components():
50    """Demonstrate accessing time components."""
51    print("\n=== Time Components ===")
52
53    time_obj = Time.now()
54    time_dict = time_obj.to_dict()
55
56    print("Time components:")
57    for key, value in time_dict.items():
58        print(f"  {key}: {value}")
59
60    print(f"\nQuarter of year: {time_obj.quarter}")
61    print(f"Is DST: {time_obj.is_dst()}")
62
63
64if __name__ == "__main__":
65    demo_basic_time_creation()
66    demo_time_formatting()
67    demo_time_components()

Key Features

  1. Time Creation - Current time - Time from string - Time from timestamp

  2. Time Formats - Format customization - Timezone handling - Format conversion

  3. Basic Operations - Time comparison - Time arithmetic - Duration calculation