0 votes
by (160 points)

I'm using Twine v2.2.1 with SugarCube v2.28.2.

I've implemented the widely-shared "Mad Exile's Gregorian Date & Time Widgets" and it does everything I want it to do except I'm having a weird problem that I can't figure out for the life of me.

When I advance time (using the widget command <<addhours 1>>, etc.) the date doesn't switch over to the next day at midnight. It switches over to the next day at 05:00am.

So the date goes: 22:00 Wednesday, 23:00 Wednesday, 00:00 Wednesday ..... 04:00 Wednesday, 05:00  Thursday, 06:00 Thursday... etc.

How can I force it to roll over to the next day at midnight?

Here's the widget code:

/*
	Date & Time Widget Setup
*/
<<set
	/* params: year , month(0-based) , day , hour(24H) , minute [, second ] */
	$gameDate to new Date(2018, 5, 23, 6, 0); /* e.g. May 23, 2018 6:00 */
	
>>
<<set
	window.GameDays to [
		"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
	];
	window.GameMonths to [
		"Jan", "Feb", "Mar", "Apr", "May", "Jun",
		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
	];
>>


/*
	Date & Time Advancement Widget Definitions
*/
/* Adds the specified number of minutes. */
<<widget "addmins">>\
<<run $gameDate.setUTCMinutes($gameDate.getUTCMinutes() + $args[0])>>\
<</widget>>

/* Adds the specified number of hours. */
<<widget "addhours">>\
<<run $gameDate.setUTCHours($gameDate.getUTCHours() + $args[0])>>\
<</widget>>

/* Adds the specified number of days. */
<<widget "adddays">>\
<<run $gameDate.setUTCHours($gameDate.getUTCHours() + $args[0] * 24)>>\
<</widget>>


/*
	Date & Time Printing Widget Definitions
*/
/* Prints the current date ("{weekday} {month} {day}, {year}"). */
<<widget "date">>\
<<print String.format("{0} {1} {2}, {3}",
	GameDays[$gameDate.getDay()],
	GameMonths[$gameDate.getMonth()],
	$gameDate.getDate(),
	$gameDate.getFullYear()
)>>\
<</widget>>

/* Prints the current time (12H). */
<<widget "time12h">>\
<<if $gameDate.getUTCHours() eq 0>>\
12\
<<elseif $gameDate.getUTCHours() gt 12>>\
<<print $gameDate.getUTCHours() - 12>>\
<<else>>\
<<print $gameDate.getUTCHours()>>\
<</if>>:\
<<if $gameDate.getUTCMinutes() lt 10>>0<</if>><<print $gameDate.getUTCMinutes()>> \
<<if $gameDate.getUTCHours() gte 12>>PM<<else>>AM<</if>>\
<</widget>>

/* Prints the current time (24H). */
<<widget "time24h">>\
<<if $gameDate.getUTCHours() lt 10>>0<</if>><<print $gameDate.getUTCHours()>>:\
<<if $gameDate.getUTCMinutes() lt 10>>0<</if>><<print $gameDate.getUTCMinutes()>>\
<</widget>>

/* Prints the current date and time (12H). */
<<widget "datetime">><<date>> <<time12h>><</widget>>

/* Sets Weekends and weekdays

<<set $isWeekend = (GameDays[$gameDate.getDay()] is "Saturday") or (GameDays[$gameDate.getDay()] is "Sunday")>>

<<set $isWeekday = (GameDays[$gameDate.getDay()] is "Monday") or (GameDays[$gameDate.getDay()] is "Tuesday") or (GameDays[$gameDate.getDay()] is "Wednesday") or (GameDays[$gameDate.getDay()] is "Thursday") or (GameDays[$gameDate.getDay()] is "Friday")>>

/* Sets day

<<widget "day">><<print GameDays[$gameDate.getDay()]>><</widget>>

Any help is appreciated.

1 Answer

+1 vote
by (68.6k points)
selected by
 
Best answer

The reason is that you're using an older, incorrect, version of the widgets, which assuming you got them from the original thread on the old forums is clearly spelled out at the top of the post you pulled them from.

See the correct version of the: Gregorian Date & Time Widgets widgets.

EDIT / PS: Your $isWeekend and $isWeekday story variables aren't going to work the way you likely intend as you're storing the state of the expression at the time the widgets are processed.  What you need to be doing is to perform the assignments after each time you update the clock.  You're probably also better off using temporary variables, but that's a smaller issue.

by (160 points)

Thanks.

I did use the newer version of the widgets, but when I updated it from the older version I had initially used I missed the change from:

$gameDate.getUTCHours()

to

$gameDate.getHours()

etc.

And thanks for the heads-up on the $isWeekend stuff. I just threw that in there while I was working on the clock and hadn't really worked with it yet. Probably saved me some time and frustration.

...