@potatoes/date

Version 1 - mainnet - 0xac...0fa729

Published by@potatoes · 7 packages

Readme

Versions

1

Dependencies

0

Dependents

0

Analytics

On the way

Date

This package implements date and time formatting in Move: RFC 7231 (UTC) string implementation as well as ISO 8601. Also supports user-defined formats.

Installing

mvr add @potatoes/date

Manual

To add this library to your project, add this to your Move.toml file under [dependencies] section:

# goes into [dependencies] section
date = { git = "https://github.com/sui-potatoes/app.git", subdir = "packages/date", rev = "date@v1" }

Exported address of this package is:

date = "0x..."

In your code, import and use the package as:

module my::awesome_project;

use date::date;

public fun utc_date(clock: &Clock): String {
    date::from_clock(clock).to_utc_string()
}

Features

  • supports timestamp -> parsed date conversion with the ability to get current minutes, seconds, year and so on
  • supports RFC 7231 (UTC) string printing
  • supports ISO 8601 string printing
  • supports custom date formatting, see Formatting for rules

Formatting

Any unrecognized symbols will be printed as is!

patternalternativedescription
yyyyYYYYThe year number in four digits (e.g. 2005)
yyYYThe last two digits of the year (e.g. 05)
MMMM-Full month name (output only)
MMM-Three letter month name (e.g. Aug)
MM-Two digit month with leading zero
M-Month number 1-12 without leading zero
ddddDDDDFull day name (output only)
dddDDDThree letter day name (e.g. Sat)
ddDDTwo digit day with leading zero
dDDay number 1-31 without leading zero
HH-Two digit 24-hour with leading zero
H-24-hour 0-23 without leading zero
hh-Two digit 12-hour with leading zero
h-12-hour 1-12 without leading zero
mm-Two digit minutes with leading zero
m-Minutes 0-59 without leading zero
ss-Two digit seconds with leading zero
s-Seconds 0-59 without leading zero
SSS-Milliseconds 0-999 padded with zeroes
tt-A.M. or P.M.
z-UTC offset (e.g. +01:00, -05:00)
'***'-Literal strings are escaped with ' single quotes

Examples

Constructing From Clock or RFC 7231 String

use sui::clock::Clock;
use date::date;

public fun date_from_clock(c: &Clock) {
    // Construct from timestamp.
    let date = date::new(c.timestamp_ms());

    // Handy method for passing `Clock`.
    let date = date::from_clock(c);

    // Print the `Date` as an RFC 7231 String.
    let utc_string = date.to_utc_string();

    // Construct the `Date` from an RFC 7231 String.
    let _date = date::from_utc_string(utc_string);
}

UTC and ISO printing

use std::unit_test::assert_eq;

#[test]
fun try_utc_and_iso() {
    assert_eq!(
        date::new(1747409967000).to_utc_string(), // rfc 7231
        b"Fri, 16 May 2025 15:39:27 GMT".to_string(),
    );

    assert_eq!(
        date::new(1747901403000).to_iso_string(), // iso 8601
        b"2025-05-22T08:10:03.000Z".to_string()
    );
}

Custom Formatting

use std::unit_test::assert_eq;

#[test]
fun try_different_formats() {
    let d = date::new(1747901403000).format(b"dd/MM/yyyy HH:mm:ss");
    assert_eq!(d, b"22/05/2025 08:10:03",to_string());

    let d = date::new(2145916899000).format(b"DD-MM-YY - tt - HH::mm::ss");
    assert_eq!(d, b"01-01-38 - AM - 00::01::39".to_string());

    // use single quotes for escapes
    let d = date::new(2145916899000).format(b"MM:YY:DD'@potatoes'");
    assert_eq!(d, b"01:38:01@potatoes".to_string());
}

License

This package is licensed under MIT.

Install

You can install this package in your Move project by calling

mvr add @potatoes/date
Interoperating Transactions

Description

Date and time printing / formatting tool, which supports RFC 7231 (UTC), ISO-8601 and custom formats, as well as constructing the date from string

Contact

https://github.com/sui-potatoes