Giter Club home page Giter Club logo

moment-msdate's Issues

Add TypeScript definitions for moment-msdate methods

Using moment-msdate in place of moment or moment-timezone in a TypeScript project requires type definitions for its methods. Moment + Timezone are already typed.

I'm still pretty new to TypeScript, but I may create a PR we can review and hopefully get this in.

All it may involve is (add anything I'm missing)

  • Create TS def file moment-msdate.d.ts
  • Link to package.json on typings key

Implementation Concerns

I'm ok with the idea of a moment plugin for OADates, but I have a few concerns:

  • I would expect the external API to be moment.fromOADate(x), not moment().fromOADate(x). By attaching it to the moment instance, you're first creating a "now" moment, then throwing it away.
  • The function clearly returns a moment, otherwise it wouldn't be in here at all. So in the docs, I wouldn't show passing the result out as a jsDate and back into the moment constructor again.
  • You might want to mention that an OADate is "unspecified", and therefore based on the local time zone by default. You might also want a clear way to pass in an OADate that is based on UTC.
  • In the implementation itself, you appear to be taking the timezone from a UTC-based conversion and then applying it to the input to determine the local time. In other words Offset(UTC(OADate)) + UTC(OADate). That's not necessarily going to yield the correct value. The offset applies at the destination - not at the source. Instead, you should be starting with a new Date of the OADate base and working from there.
  • There are some implementation quirks about negative numbers that are described in the MSDN reference, but you don't appear to be handling them. You might want to consider the internal .Net implementation code of DateTime.FromOADate which is as follows:
// Creates a DateTime from an OLE Automation Date. 
//
public static DateTime FromOADate(double d) { 
    return new DateTime(DoubleDateToTicks(d), DateTimeKind.Unspecified); 
}

// Converts an OLE Date to a tick count. 
// This function is duplicated in COMDateTime.cpp 
internal static long DoubleDateToTicks(double value) {
    if (value >= OADateMaxAsDouble || value <= OADateMinAsDouble) 
        throw new ArgumentException(Environment.GetResourceString("Arg_OleAutDateInvalid"));
    long millis = (long)(value * MillisPerDay + (value >= 0? 0.5: -0.5));
    // The interesting thing here is when you have a value like 12.5 it all positive 12 days and 12 hours from 01/01/1899
    // However if you a value of -12.25 it is minus 12 days but still positive 6 hours, almost as though you meant -11.75 all negative 
    // This line below fixes up the millis in the negative case
    if (millis < 0) { 
        millis -= (millis % MillisPerDay) * 2; 
    }

    millis += DoubleDateOffset / TicksPerMillisecond;

    if (millis < 0 || millis >= MaxMillis) throw new ArgumentException(Environment.GetResourceString("Arg_OleAutDateScale"));
    return millis * TicksPerMillisecond; 
}

While it's not going to be identical in JavaScript, your code should follow the same logic.

  • You might also want to implement .toOADate()
  • Unit Tests?

not on npm?

This plugin should be installable via npm install moment-msdate like all other plugins...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.