Skip to content

data.ourtime #

OurTime Module

The OurTime module provides a flexible and user-friendly way to work with time in V. It supports both relative and absolute time formats, making it easy to handle various time-related operations.

Features

  • Create time objects from various string formats
  • Support for relative time expressions
  • Support for absolute time formats
  • Unix timestamp handling
  • Time formatting utilities
  • Time warping capabilities

Usage

Basic Usage

import freeflowuniverse.crystallib.data.ourtime

// Create time object for current time
mut t := ourtime.now()

// Create from string
t2 := ourtime.new('2022-12-05 20:14:35')!

// Get formatted string
println(t2.str()) // Output: 2022-12-05 20:14

// Get unix timestamp
println(t2.unix()) // Output: 1670271275

Time Formats

Relative Time Format

Relative time expressions use the following period indicators:- s: seconds

  • h: hours
  • d: days
  • w: weeks
  • M: months
  • Q: quarters
  • Y: years

Examples:

// Create time object with relative time
mut t := ourtime.new('+1w +2d -4h')! // 1 week forward, 2 days forward, 4 hours back

// Warp existing time object
mut t2 := ourtime.now()
t2.warp('+1h')! // Move 1 hour forward

Absolute Time Format

Supported date formats:- YYYY-MM-DD HH:mm:ss

  • YYYY-MM-DD HH:mm
  • YYYY-MM-DD
  • DD-MM-YYYY (YYYY must be 4 digits)
  • Also supports '/' instead of '-' for dates

Examples:

// Various absolute time formats
t1 := ourtime.new('2022-12-05 20:14:35')!
t2 := ourtime.new('2022-12-05')! // Sets time to 00:00:00
t3 := ourtime.new('05-12-2022')! // DD-MM-YYYY format

Methods

Creation Methods

// Create for current time
now := ourtime.now()

// Create from string
t := ourtime.new('2022-12-05 20:14:35')!

// Create from unix timestamp
t2 := ourtime.new_from_epoch(1670271275)

Formatting Methods

mut t := ourtime.now()

// Get as YYYY-MM-DD HH:mm format
println(t.str())

// Get as YYYY-MM-DD format
println(t.day())

// Get as formatted key (YYYY_MM_DD_HH_mm_ss)
println(t.key())

// Get as markdown formatted string
println(t.md())

Time Operations

mut t := ourtime.now()

// Move time forward or backward
t.warp('+1h')! // 1 hour forward
t.warp('-30m')! // 30 minutes backward
t.warp('+1w +2d -4h')! // Complex time warp

// Get unix timestamp
unix := t.unix()

// Get as integer
i := t.int()

// Check if time is empty (zero)
is_empty := t.empty()

Examples

Working with Relative Time

mut t := ourtime.now()

// Add time periods
t.warp('+1w')! // Add 1 week
t.warp('+2d')! // Add 2 days
t.warp('-4h')! // Subtract 4 hours

// Complex time warping
t.warp('+1Y -2Q +2M +4h -60s')! // Add 1 year, subtract 2 quarters, add 2 months, add 4 hours, subtract 60 seconds

Working with Absolute Time

// Create time from absolute format
t1 := ourtime.new('2022-12-05 20:14:35')!
println(t1.str()) // 2022-12-05 20:14

// Create time from date only
t2 := ourtime.new('2022-12-05')!
println(t2.str()) // 2022-12-05 00:00

// Get different formats
println(t1.day()) // 2022-12-05
println(t1.key()) // 2022_12_05_20_14_35

Time Comparisons and Checks

mut t := ourtime.now()

// Check if time is empty
if t.empty() {
    t.now() // Set to current time if empty
}

// Get unix timestamp for comparisons
unix := t.unix()

Error Handling

All time parsing operations that can fail return a Result type, so they should be called with ! or handled with or blocks:

// Using ! operator (panics on error)
t1 := ourtime.new('2022-12-05')!

// Using or block for error handling
t2 := ourtime.new('invalid-date') or {
    println('Error parsing date: ${err}')
    ourtime.now() // fallback to current time
}

fn get_unix_from_absolute #

fn get_unix_from_absolute(timestr_ string) !i64

fn new #

fn new(txt_ string) !OurTime

Get Expiration object from time string input . input can be either relative or absolute . if input is empty then is now

## Relative time
#### time periods:
- s : second
- h : hour
- d : day
- w : week
- M : month
- Q : quarter
- Y : year
0 means right now
input string example: "+1w +2d -4h"

## Absolute time
inputs must be of the form: "YYYY-MM-DD HH:mm:ss" or "YYYY-MM-DD"
the time can be HH:mm:ss or HH:mm
inputs also supported are: "DD-MM-YYYY" but then the YYYY needs to be 4 chars
for date we also support / in stead of -
input string examples:

'2022-12-5 20:14:35'
'2022-12-5' - sets hours, mins, seconds to 00

fn new_from_epoch #

fn new_from_epoch(ut u64) OurTime

fn now #

fn now() OurTime

struct OurTime #

struct OurTime {
pub mut:
	unixt i64
}

fn (OurTime) check #

fn (mut ot OurTime) check()

will check if the time is empty, if yes then will fill it in

fn (OurTime) md #

fn (ot OurTime) md() string

print the wiki formatting for time

fn (OurTime) key #

fn (ot OurTime) key() string

format as a usable key . "YYYY_MM_DD_HH_mm_ss"

fn (OurTime) str #

fn (ot OurTime) str() string

returns a date-time string in "YYYY-MM-DD HH:mm" format (24h).

fn (OurTime) day #

fn (ot OurTime) day() string

returns a date string in "YYYY-MM-DD" format

fn (OurTime) int #

fn (ot OurTime) int() int

returns as epoch (seconds)

fn (OurTime) now #

fn (mut t OurTime) now()

set ourtime to now

fn (OurTime) time #

fn (t OurTime) time() time.Time

get time from vlang

fn (OurTime) unix #

fn (t OurTime) unix() i64

get time from vlang

fn (OurTime) empty #

fn (t OurTime) empty() bool

fn (OurTime) warp #

fn (mut t OurTime) warp(warp string) !

move the time, e.g. +1h means we go 1 h further## Relative time

time periods:

  • s : second
  • h : hour
  • d : day
  • w : week
  • M : month
  • Q : quarter
  • Y : yearinput string example: "+1w +2d -4h"