How do you convert epoch time in C#?

UPDATE 2020 You can do this with DateTimeOffset DateTimeOffset dateTimeOffset = DateTimeOffset.FromUnixTimeSeconds(epochSeconds); DateTimeOffset dateTimeOffset2 = DateTimeOffset.FromUnixTimeMilliseconds(epochMilliseconds); And if you need the DateTime object instead of DateTimeOffset, then you can call the DateTime property DateTime dateTime = dateTimeOffset.DateTime; Original answer I presume that you mean Unix time, which is defined as the number of seconds since …

Read more

How to find the last field using ‘cut’

You could try something like this: echo ‘’ | rev | cut -d’.’ -f 1 | rev Explanation rev reverses “” to be moc.elgoog.spam cut uses dot (ie ‘.’) as the delimiter, and chooses the first field, which is moc lastly, we reverse it again to get com

How to allocate aligned memory only using the standard library?

Original answer { void *mem = malloc(1024+16); void *ptr = ((char *)mem+16) & ~ 0x0F; memset_16aligned(ptr, 0, 1024); free(mem); } Fixed answer { void *mem = malloc(1024+15); void *ptr = ((uintptr_t)mem+15) & ~ (uintptr_t)0x0F; memset_16aligned(ptr, 0, 1024); free(mem); } Explanation as requested The first step is to allocate enough spare space, just in case. Since …

Read more

Convert date to another timezone in JavaScript

Here is the one-liner: function convertTZ(date, tzString) { return new Date((typeof date === “string” ? new Date(date) : date).toLocaleString(“en-US”, {timeZone: tzString})); } // usage: Asia/Jakarta is GMT+7 convertTZ(“2012/04/20 10:10:30 +0000”, “Asia/Jakarta”) // Tue Apr 20 2012 17:10:30 GMT+0700 (Western Indonesia Time) // Resulting value is regular Date() object const convertedDate = convertTZ(“2012/04/20 10:10:30 +0000”, “Asia/Jakarta”) …

Read more

How can I convert the “arguments” object to an array in JavaScript?

ES6 using rest parameters If you are able to use ES6 you can use: Rest Parameters function sortArgs(…args) { return args.sort(function (a, b) { return a – b; }); } document.body.innerHTML = sortArgs(12, 4, 6, 8).toString(); As you can read in the link The rest parameter syntax allows us to represent an indefinite number of …

Read more

How can I call controller/view helper methods from the console in Ruby on Rails?

To call helpers, use the helper object: $ ./script/console >> helper.number_to_currency(‘123.45′) => “R$ 123,45” If you want to use a helper that’s not included by default (say, because you removed helper :all from ApplicationController), just include the helper. >> include BogusHelper >> helper.bogus => “bogus output” As for dealing with controllers, I quote Nick’s answer: …

Read more