1

I recently found the following code online that gives FFXIV's in-game time (Eorzea time):

var E_TIME = 20.5714285714;
var global = {
utcTime: null,
eorzeaTime: null
};
window.setInterval(updateClock, Math.floor(1000 * 60 /  E_TIME));

function updateClock() {
    global.utcTime = new Date().getTime();
    var eo_timestamp = Math.floor(global.utcTime * E_TIME);
    global.eorzeaTime = new Date();
    global.eorzeaTime.setTime(eo_timestamp);
    showTime();
}

function showTime() {
    var d = new Date();
    d.setTime(global.eorzeaTime);
    var eTime = document.getElementById('e-time');
    var hours = d.getUTCHours();
    var ampm = hours > 11 ? "PM" : "AM";
    if(hours > 12)
        hours -= 12;
    hours = padLeft(hours);
    var minutes = d.getUTCMinutes();
    minutes = padLeft(minutes);
    eTime.innerHTML = hours + ":" + minutes + " " + ampm;
}

function padLeft(val){
    var str = "" + val;
    var pad = "00";
    return pad.substring(0, pad.length - str.length) + str;
}

updateClock();

NOTE: I take no credit in that code, I am not the original coder and it was found here: http://jsfiddle.net/jryansc/6r85j/

What I would like to do is get something that does the same result in C# (time wise), unfortunately I am new to programming and I know only some C# at the moment. This is why I am asking for help, I tried to manipulate DateTime and TimeSpan in C# but it does not seem as easy as it is in JavaScript (according to the code above).

Can someone help me out to convert the code please?

All the help is greatly appreciated.

10
  • Do you need do this in a WPF, winform oe Asp.Net project ?
    – renefc3
    Jan 14, 2015 at 13:33
  • 3
    This question appears to be off-topic because it is about code translation.
    – Paddy
    Jan 14, 2015 at 13:34
  • It needs to be working in a standard WindowsForm Application. About the off-topic comment, is there anywhere else I could have posted this question please? Thanks again for all the help.
    – Dyr Fenrir
    Jan 14, 2015 at 13:42
  • You should make an attempt to convert the code yourself and then return with specific issues in your implementations. Jan 14, 2015 at 13:47
  • @user3310909 your question is a bit vague. Could you update it to clarify: are you asking how to calculate Eorzea time in C#? Or how to display a realtime clock in a WinForms application? I wouldn't consider either of those to be off topic, but it appears at the moment you're asking for a straight code translation.
    – olitee
    Jan 14, 2015 at 13:52

1 Answer 1

2

Assuming your question is 'How do I calculate Eorzea time in C#':

Javascript's Time class is based around Epoch Time (the amount of time that has elapsed since 00:00 1/1/1970). When you multiply the Time() object, you're multiplying the number of seconds that have elapsed since that date.

.NET's DateTime class doesn't support the simple multiply operator Javascript does, but it's easy to duplicate. You need to calculate how many ticks, seconds, minutes (whichever you like) have elapsed since 1/1/1970, then multiply that number by 20.5714285714, and convert back to a DateTime.

In my example, I'm using ticks instead of seconds.

const double EORZEA_MULTIPLIER = 3600D / 175D; //175 Earth seconds for every 3600 Eorzea seconds

// Calculate how many ticks have elapsed since 1/1/1970
long epochTicks = DateTime.UtcNow.Ticks - (new DateTime(1970, 1, 1).Ticks);

// Multiply that value by 20.5714285714...
long eorzeaTicks = (long)Math.Round(epochTicks * EORZEA_MULTIPLIER);

var eorzeaTime = new DateTime(eorzeaTicks);

To make things even easier, you could created a DateTime extension method:

public static class EorzeaDateTimeExtention
{
    public static DateTime ToEorzeaTime(this DateTime date)
    {
        const double EORZEA_MULTIPLIER = 3600D/175D;

        long epochTicks = date.ToUniversalTime().Ticks - (new DateTime(1970, 1, 1).Ticks);

        long eorzeaTicks = (long)Math.Round(epochTicks * EORZEA_MULTIPLIER);

        return new DateTime(eorzeaTicks);
    }
}

Which will allow you to convert ANY date to Eorzea time by simply:

var eorzeaTimeNow = DateTime.Now.ToEorzeaTime();

or

var eorzeaSpecificTime = new DateTime(2014,5,12,5,0,0).ToEorzeaTime();

TIP: Make sure your PC clock is set accurately ... I found this code was a few minutes out until I realised that my clock was several seconds behind. :)

1
  • Greetings again, I looked back at my code and I was actually trying too much to translate exactly what the JS script was doing and forgot to only go with the logic. Thanks a lot with the information, I also needed to learn to work with time and timers in C# and it helped a great deal. Sorry again to have not been that clear at first, my question sure was answered though and thanks for that. I'll do a credit section on my tool and I'll make sure to put your name in it.
    – Dyr Fenrir
    Jan 15, 2015 at 5:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.