Giter Club home page Giter Club logo

morrow.mojo's Introduction

Morrow.mojo: Human-friendly date & time for Mojo ๐Ÿ”ฅ

Test GitHub release

Morrow is a Mojo library that provides human-friendly method for managing, formatting, and transforming dates, times, and timestamps.

Morrow is heavily inspired by arrow, and thanks for its elegant design.

Features

  • TimeZone-aware and UTC by default.
  • Support format and parse strings.
  • Support for the ISO 8601 standard.

Preparation

You have three ways to reference this library:

  • Download morrow.mojopkg from releases.
  • Clone this project and execute make build to build morrow.mojopkg.
  • Directly copy the morrow directory of this project to your own project.

Usage

from morrow import Morrow, TimeZone

# Get local date and time.
let now = Morrow.now()
print(now.__str__())  # 2023-10-01T20:10:25.188957+08:00

# Get UTC date and time.
let utcnow = Morrow.utcnow()
print(utcnow.__str__())  # 2023-10-01T20:10:25.954638+00:00

# Get local time from POSIX timestamp.
let t = Morrow.fromtimestamp(1696089600)
print(t.__str__())  # 2023-10-01T00:00:00.000000+08:00

# Get UTC time from POSIX timestamp.
let utc_t = Morrow.utcfromtimestamp(1696089600)
print(utc_t.__str__())  # 2023-09-30T16:00:00.000000+00:00

# Get ISO format.
let m = Morrow(2023, 10, 1, 0, 0, 0, 1234)
print(m.isoformat())  # 2023-10-01T00:00:00.001234

# Get ISO format with time zone.
let m_beijing = Morrow(2023, 10, 1, 0, 0, 0, 1234, TimeZone(28800, 'Bejing'))
print(m_beijing.isoformat(timespec="seconds"))  # 2023-10-01T00:00:00+08:00

# Get time zone offset.
print(TimeZone.from_utc('UTC+08:00').offset)  # 28800

# Subtract two dates.
let timedelta = Morrow(2023, 10, 2, 10, 0, 0) - Morrow(2023, 10, 1, 10, 0, 0)
print(timedelta.__str__())  # 1 day, 0:00:00

# Return proleptic Gregorian ordinal for the year, month and day.
let m_10_1 = Morrow(2023, 10, 1)
let ordinal = m_10_1.toordinal()
print(ordinal)  # 738794

# Construct a Morrow from a proleptic Gregorian ordinal.
let m_10_1_ = Morrow.fromordinal(ordinal)
print(m_10_1_.__str__())  # 2023-10-01T00:00:00.000000

# Convert Morrow to python datetime
let py_dt = now.to_py()
print(py_dt.isoformat())  # 2023-10-01T20:10:25.188957

# Convert python datetime to Morrow
let m_from_py = Morrow.from_py(py_dt)
print(m_from_py.__str__())  # 2023-10-01T20:10:25.188957

morrow.mojo's People

Contributors

prodesire avatar shalei6698 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.