Real Date and Time

version 1 by Ron Newcomb

  • Home page
  • Complete text



  • Real Date and Time (for Glulx only) by Ron Newcomb begins here.

    "Allows the author to get the real-world time and date from the player's computer."


    A weekday is a kind of value. The weekdays are Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday.

    A month is a kind of value. The months are January, February, March, April, May, June, July, August, September, October, November, December.

    A timezone is a kind of value. [ It's just an alternate way of printing a time as a relative measure, not in words. ]

    Represent time locally is a truth state that varies. The represent time locally variable translates into I6 as "UTCvsLocal". [Represent time locally is usually true.]


    To decide if the player's time/date is available: (- (glk($0004, 20, 0) ~= 0) -). [! gestalt_DateTime]

    To decide what number is the player's year: (- GetNthDateTimeComponent(0) -).
    To decide what month is the player's month: (- GetNthDateTimeComponent(1) -).
    To decide what number is the player's day: (- GetNthDateTimeComponent(2) -).
    To decide what weekday is the player's weekday: (- GetNthDateTimeComponent(3) -).
    To decide what time is the player's time: (- GetNthDateTimeComponent(4) -). [ and 5 ]
    To decide what number is the player's seconds: (- GetNthDateTimeComponent(6) -).
    To decide what number is the player's microseconds: (- GetNthDateTimeComponent(7) -).

    To decide what timezone is the player's timezone: (- GetPlayersTimezone() -).

    To while suspending time begin -- end: (- for(suspend_glk_get_time = GetNthDateTimeComponent(-1) : suspend_glk_get_time : --suspend_glk_get_time) -).
    To while suspending time, (ph - a phrase): (- for(suspend_glk_get_time = GetNthDateTimeComponent(-1) : suspend_glk_get_time : --suspend_glk_get_time) {ph}; -).

    To say (tz - a timezone):
        let T be tz as a time;
        if T is less than zero minutes:
            say "-";
            now T is 0 minutes minus T;
        otherwise:
            say "+";
        let H be the hours part of T;
        let M be the minutes part of T;
        say "[if H is less than ten]0[end if][H][unless M is zero]:[M]".

    To decide what time is (t - timezone) as a time: (- {t} -).
    To decide what timezone is (t - time) as a timezone: (- {t} -).

    Include (-
    Array countofseconds --> 3; ! this holds the number of microseconds elapsed since midnight on January 1, 1970, GMT/UTC
    Array datetime --> 8; ! this holds the above broken down into year, month, day, weekday, hour, minute, second, and microseconds
    Global UTCvsLocal = 1; ! truth state: 1 = local time; 0 = GMT otherwise known as UTC
    Global suspend_glk_get_time = 0; ! 0 = not suspended; 1 = suspended

    [ GetNthDateTimeComponent n;
    if (glk($0004, 20, 0) == 0) return 0; ! glk_gestalt_DateTime(20, 0);
    if (~~suspend_glk_get_time)
    {
         glk($0160, countofseconds); ! glk_current_time(timeval);
         if (UTCvsLocal) glk($0169, countofseconds, datetime); ! glk_time_to_date_local(tv, date);
         else glk($0168, countofseconds, datetime); ! glk_time_to_date_utc(tv, date);
    }
    if (n == -1) return 1; ! used by "while suspending time"
    if (n == 3) return datetime-->3 + 1;
    if (n == 4 or 5) return datetime-->4 * 60 + datetime-->5;
    return datetime-->n;
    ];

    Array countofseconds2 --> 3; ! as above, for timezone calculations.
    Array datetime2 --> 8; ! This is so player can use it inside "while suspending time". Otherwise, it corrupts the held time.
    [ GetPlayersTimezone zonesec zonemin zonehour firstsecondcount;
    if (glk($0004, 20, 0) == 0) return 0; ! glk_gestalt_DateTime(20, 0);
    glk($0160, countofseconds2); ! glk_current_time()
    glk($0169, countofseconds2, datetime2); ! glk_time_to_date_local()
    firstsecondcount = countofseconds2-->1;
    glk($016C, datetime2, countofseconds2); ! glk_date_to_time_utc()
    zonesec = (countofseconds2-->1 - firstsecondcount); ! UTC - local
    zonehour = zonesec / 3600;
    zonesec = zonesec % 3600;
    zonemin = zonesec / 60;
    return (zonehour * 60) + zonemin;
    ];
    -) after "Definitions.i6t".



    Real Date and Time ends here.