[Macros] Complete Date/Time macro set relased

I’m finally happy to announce the official release of my custom time system macros for Sugarcube, which I have creatively called DATESYSTEM The Datesystem macros are intended to provide an entire drop-in date/time system, capable of imitating a Gregorian calendar, or supporting an entirely custom date/time system. It offers a variety of macros, and corresponding JS functions.

These macros are intended for anyone who’d like a complex date/time system in their Twine/Sugarcube game, but don’t want to write their own. Need more features? Let me know and I’ll be happy to look at adding them.

Features

* Fully customisable date system (min/hour/day/month/year lengths) - defaults to Gregorian
* Custom month names
* Custom day names
* Custom time unit names
* Leap year support
* Set to real-world time
* Sends custom event when time changes
* Supports multiple time systems in one game
* Time tracked in a single story variable for each time system

Macros

* <<date>> outputs a formatted date or time, with full format control
* <<dateset>> sets the date/time
* <<dateto>> sets to an absolute time
* <<dateadd>> moves the time forward by some unit(s)
* <<datesubtract>> moves the time backwards by some unit(s)
* <<datenext>> moves the time forward to the next minute/hour/day/year (i.e discards fractions of that time unit)
* <<datereset>> sets the new base time and resets the time counter
* <<dateperiod>> outputs a time as a period/duration
* <<dateticker>> creates a ticking clock with 1s precision
* <<datesetup>> handles the setup of the date system

Functions

* Functional versions of every macro
* dateCompare() function to compare parts of dates
6 Likes

Thanks! This looks useful.

I’ve never wanted to use a real-time like time system in my games, but people ask for it all the time, so I thought that instead of always explaining roughly how it works, I could just make a library …

2 Likes

I’ve never wanted to use a real-time like time system in my games, but people ask for it all the time

I was considering making a tamagotchi-style pumpkin-raising game for Ectocomp one year.

I didn’t have to do it anyway at that point, but I was considering whether the best way to do it was to use real time (I think you can get the computer’s date and time with js) or with a turn-based time system (like yours).

Maybe this year…

Edit: I see at the end of the readme and the points above that you can get the real date? That’s the best of both worlds.

Yes, you can set the system to initialise or update to the real date when you want, with the getRealDate() function.

1 Like

I’ve added a new macro to this set, <<at>>

Syntax <<at "date string or number" [system-id]>>[code to run]<</at>>

The <<at>> macro allows you to run code dynamically when the game time reaches a certain value. You could use this to put a notification on screen, trigger an event, or interrupt gameplay to go to a new passage. The event is triggered by the :dateupdated event (see below), which means that any of the date changing macros (including the clock created by <<dateticker>>) can trigger it.

The first argument to the macro should either be a date string (like “2000y 10mo 1d”) or a number representing the time (like you might get from $time + 60). As soon as the current $time reaches or passes that value, the code inside the <<at>> will be run.

Example

<<at "2000y 1mo 1d 4h">>
    <<run alert("Time up!")>>
<</at>>

The code inside the <<at>> will use the value of story and temporary variables at the time it runs, rather than at the time it was created. If you want it to use the values when it was created, wrap it in a <<capture>> macro, as you would do for a <<link>>.

Example

:: PassageOne
<<dateset "2000y 1mo 1d">>
<<set _name = "Hituro">>
<<capture _name>>
    <<at "2000y 1mo 1d 4h">>
        <<run alert(`Hello ${_name}`)>>
    <</at>>
<</capture>>
<<link [[Go to 4am|PassageTwo]]>><<dateadd "3h">><</link>>

:: PassageTwo
<<set _name = "Bob">>

When you go to PassageTwo the alert will show “Hello Hituro” because _name was captured when the <<at>> was run. Without the <<capture>> it would show “Hello Bob” instead.

Hi Hituro,

Firstly, love this - very helpful for what i’m working on.

I’m having some issues with my date - i’ve been working through the docs and I’m hoping it’s something I’m missing.

I’m setting the date like this:

<<datesetup"2000y 1mo 1d">>

But what i’m getting is Thursday 0th of January when I then print it out like

<<date "[D] [d][day_ordinal] of [M]">>

I’m not sure if this is a local bug i’ve got - or if there’s something in the JS - i’ve been going through it and trying to work out where the index is set to 0. Just need to know if that’s something it should be doing or if there’s something i can configure to check it.

I’ve also noticed that the PM bit is
out.day_half = out.h > 12 ? "pm" : "am";

I’ve changed that locally to be =>, as that makes more sense to me, do you mind if i suggest that as a fork on github?

Thanks

Just to check, are you actually leaving out the space in <<datesetup>> because that might be an issue?

I tried taking the latest version of the Date macros, putting it into a new story, putting <<datesetup "2000y 1mo 1d">> into StoryInit and <<date "[D] [d][day_ordinal] of [M]">> into the start passage, and I get “Saturday 1st of January”, so I am not sure why you are getting “Thursday 0th of January” unless your <<datesetup>> is not working.

As for the AM/PM split, I remember reading up to see if 12 noon was correctly AM or PM, and the consensus answer was AM. Feel free to change your own version if it makes more sense to you the other way though.

Hi Hiruto,

I previously posted that i’d solved it with calling the script, but that was incorrect. It turns out that I was manipulating it with setup.datesystems.default.getDate(State.variables.timestamp, ‘’) immediately after - my error.

On the AM/PM bit - appreciate different cultures do it differently. It’s hammered into my head that 12am is midnight - so I’ll make some adjustments locally. I’ll probably give users the option to disable the day_half.

All the best.