Giter Club home page Giter Club logo

moment-msdate's Introduction

moment-msdate Build Status npm version

A moment.js and moment-timezone.js plugin for parsing OLE Automation dates.

Visit http://markitondemand.github.io/moment-msdate/ for more information and examples.

About OLE Automation Dates

An OLE Automation date, or "MSDate" as we call it, is implemented as a floating-point number whose integral component is the number of days before or after midnight, 30 December 1899, and whose fractional component represents the time on that day divided by 24. For example, midnight, 31 December 1899 is represented by 1.0; 6 A.M., 1 January 1900 is represented by 2.25; midnight, 29 December 1899 is represented by -1.0; and 6 A.M., 29 December 1899 is represented by -1.25.

Read more about OLE Automation on MSDN (including DateTime.ToOADate and DateTime.FromOADate).

Note: OLE Automation dates are unspecified and they’re based on the local timezone by default. The moment library normalizes all time to UTC and as a result this library will return all values based on UTC time.

Usage

fromOADate(oaDate, [offset])

Convert an OA date to a moment:

moment.fromOADate(42298.6868055556) returns 2015-10-21T16:29:00.000Z

Convert an OA date with a known offset to UTC to a moment in UTC time

moment.fromOADate(42298.6868055556, 240) returns 2015-10-21T20:29:00.000Z
moment.fromOADate(42298.6868055556, 'America/New_York') returns `2015-10-21T20:29:00.000Z

toOADate()

Convert a moment to a floating point OA date in UTC:

const momentDate = moment('2015-10-21T16:29:00.000-07:00')
momentDate.toOADate() returns 42298.978472222225

Example Moment Formatting:

Convert OA date into Moment (OA Date is assumed to be in UTC)

const momentDate = moment.fromOADate(42298.6868055556);

If OA date is not in UTC and the offset to UTC is known it can be specified during the moment creation in minutes

const momentDate = moment.fromOADate(42298.6868055556, 240)
momentDate.toISOString() returns '2015-10-21T20:29:00.000Z' (UTC)
momentDate.format('LLLL') returns 'Wednesday, October 21, 2015 8:29 PM' (UTC)

If OA date is not in UTC and the offset to UTC is known it can be specified during the moment creation as a timezone

const momentDate = moment.fromOADate(42298.6868055556, 'America/New_York')
momentDate.toISOString() returns '2015-10-21T20:29:00.000Z' (UTC)
momentDate.format('LLLL') returns 'Wednesday, October 21, 2015 8:29 PM' (UTC)

Once the date is in UTC it can than easily be converted to any other timezone using moment-timezone.js

const momentDate = moment.fromOADate(42298.6868055556, 240)
momentDate.tz('America/New_York')
momentDate.toISOString() returns '2015-10-21T20:29:00.000Z' (UTC)
momentDate.format('LLLL') returns 'Wednesday, October 21, 2015 4:29 PM' (ET)

License

Copyright © 2014 Markit On Demand, Inc.

The "moment-msdate" Moment.js plugin is licensed under the Apache License, Version 2.0.

moment-msdate's People

Contributors

brianbaker avatar camjjack avatar dzmitrychuradze avatar jjustus avatar mark-lewis-markit avatar markhealey avatar michael-lechner avatar zlatkovsky avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

moment-msdate's Issues

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?

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

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.