Updating a date/time based field is giving a result that's 12 hours off

I am seeing strange behavior when trying to update a time based field in a document. We are trying to use Aspose to update a field in a document, and that field is based on the built-in document property “Last Saved Time”. If the last saved time is an AM time, then the update works correctly; if the last saved time is a PM time, then the update result is the same as if it were an AM time - 8:30 PM gets updated to 8:30 AM.

I’ve attached three documents to this post. The first two are the same sample file, one with a “Last Saved Time” property set to a time in the morning, and one in the evening (sample - am.docx and sample - pm.docx, respectively). I’ve also attached a text file with some code that can be used to demonstrate the problem. The code basically goes over every field in the document, updates it, and then resaves the document. You should be able to see that the update does not work correctly for the sample - pm.docx file.

Can this be fixed? Or is there something I’m doing wrong, maybe?

Thanks,

Michael Whalen

Hi Michael,

Thanks for your inquiry. Please note that Aspose.Words mimics the same behavior as MS Word does.

Aspose.Words uses UTC time for storing CreatedTime, LastPrinted and LastSavedTime properties. So, if you need a local time (like it displayed in MS Word), you should translate UTC time to local time as shown below.

If you still face problem, please share in which Region/culture you have set up MS Word on your Machine.

Dim CreatedTime = doc.BuiltInDocumentProperties.CreatedTime.ToLocalTime()
Dim LastPrinted = doc.BuiltInDocumentProperties.LastPrinted.ToLocalTime()
Dim LastSavedTime = doc.BuiltInDocumentProperties.LastSavedTime.ToLocalTime()

In this case, I can demonstrate that MS Word is doing this correctly and Aspose is not. This is about a field that is getting updated incorrectly - the built-in properties are just fine, but updating a field based on the last saved time property isn’t working. Let me try to explain further.

The two docx files attached to the original post have been created so that one’s current “Last Saved Time” is in the morning, and one’s is in the evening. Other than that, the files are identical.

In the content of the files, the headers contain some fields that will show a couple of the built-in properties, including “Last Saved Time”.

When we use Aspose to update those fields, no matter what, afterwards field contains an AM time. The “Last Saved Time” in the evening document is 8:39 PM, but after updating the field using Aspose, the field reads “8:39 AM”. Note: doing similar steps using MS Word updates the field correctly to “8:39 PM”. Both Aspose and MS Word update the morning document correctly, it’s just the afternoon document that’s failing.

In case it matters, we are using US English as our region/culture, but I haven’t seen any change in behavior when I change the various time related regional settings on my machine.

The vb file (with a .vb.txt extension) has some code you can use to demonstrate the problem.

The sub “RunTest” will open a document, enumerate and update all its fields, and save it to a new location so you can see the results and compare to the original. A little modification (like filling in the “SourceDir” variable with the source directory) and you should be able to run it and see what I’m talking about. Both the morning and evening files should have different time stamps after running through this test code, but they don’t - they’re exactly the same. The update on the evening document fails, exactly 12 hours off.

Note: there’s a little bit of unneeded code in that file - the subs “UnlinkField” and “RemoveSequence” aren’t needed. I forgot to remove them before sending the sample code.

Please give the test code a try and hopefully you’ll see the problem. Your response seems to focus just on the built-in property, but it’s the field update that’s important - in this case, it does not work correctly if the Last Saved Time is a PM time.

Hi Michael,

Thanks for sharing the detail. I have tested the scenario using latest version of Aspose.Words for .NET 15.5.0 and have noticed that Document.UpdateFields and Field.Update does not update LastSavedTime field.

I have faced this issue with ‘sample - am.docx’. The field’s value is ‘June 9, 2015 - 8:39 AM’ in output document. When It is updated by MS Word, it is changed to ‘June 9, 2015 - 8:39 PM’. I have not faced this issue with ‘sample - pm.docx’. I have attached the output documents with this post for your kind reference.

For the sake of correction, I have logged this problem in our issue tracking system as WORDSNET-11992. I have linked this forum thread to the same issue and you will be notified via this forum thread once this issue is resolved.

We apologize for your inconvenience.

Thanks for the info. Note - this is exactly the opposite of what we saw - the AM document did not show the issue, but the PM document did. We are in the Pacific time zone (UTC-8), so perhaps that has something to do with it - maybe the PM file is affected if you’re between Greenwich and the International Date Line, and the AM file is affected otherwise? Just a thought that might help as more data on this issue.

Thanks again,

Michael Whalen

Hi Michael,

Thanks for sharing the detail. I have logged this detail in our issue tracking system. Our product team will check this issue and we will inform you via this forum thread once there is any update available on this issue.

Please let us know if you have any more queries.

Hi Michael,

Thanks
for your patience. Could you please share step by step (possibly with screenshots) how you managed to reproduce the issue?

  1. Open your document in MS Word and press Alt + F. Take screen shot of ‘Related Date’ section and share it here
  2. Share the screen shots of field before and after updating it.

Please check the attached images for detail.

Attached is a zip file with three images in it. The file titled “before updating field.png” shows the Properties page from Word for the document in question before any update has been applied. As you can see, the last modified time is 8:39PM.

To reproduce the issue, I then run the code that’s attached to the OP. It simply opens the document using Aspose.Words.dll, finds all of the fields in the document, updates all of them, and then saves the document to a new location so as to not overwrite the original. We do use a function to collect the fields, rather than just using Document.Range.Fields, as we’ve found there are sometimes problems with delete revision fields and ignoring those works better for our product (this document doesn’t actually have any of those fields, so it’s not really necessary to filter the fields, but that’s the code we’re using in our product, so I included it).

That’s the entire repro, and the result can be seen in the image file “after updating field using Aspose.png” - the time displayed is 8:39 AM. I also used Word to update the field, and the result can be seen in “after updating field using MS Word.png” - the time displayed is 8:39 PM.

I think this might have something to do with which hemisphere you’re in. The original investigator apparently was able to reproduce the issue using the AM file - he applied the same steps above using the document which has 8:39 AM as the last modified time, and the result was the field was updated to 8:39 PM. All of my tests have been done on a machine set to use Pacific Daylight Time (UTC-8). That may be important in trying to reproduce this issue.

Let me know if you need any more info - this is a kind of confusing issue which I had a hard time understanding for the first couple of days that I was working on it myself.

Hi Michael,

Thanks
for sharing the detail. It seems that the shared screenshots are of ‘sample - am.docx’. Please confirm this.

No - those screenshots are of from “sample - pm.docx” and that’s the problem - it’s switching PM to AM when you update the field.

The file “before updating field.png” shows the last modified time of 8:39 PM, which is from the document “sample - pm.docx”. If you are seeing different properties then please switch your computer to use Pacific Daylight Time (UTC-8) and try again.

One more piece of information - based on the images you provided, I’m guessing that you are in a country where the current time zone is UTC+5. When I set my computer to that time zone, the file “sample - am.docx” looks the same as it does in the images you sent, and also displays the problem. Whether you see this issue or not is dependent on the time zone, so:

– If you are in UTC+5, the problem will occur with “sample - am.docx”

– If you are in UTC-8, the problem will occur with “sample - pm.docx”

I haven’t checked any other time zones, so I don’t know where the switch over point will be. The problem in both cases is that updating a field based on “last modified time” switches PM to AM.

Hi Michael,

Thanks for sharing the detail. Our product team has completed the analysis of this issue. It seem that this is a bug in MS Word not in Aspose.Words. I am quoting developer’s comments here for your reference.

It seems to be a Word’s bug, not Aspose.Words. We set the time zone to UTC-8,
culture to en-US and opened the sample - pm.docx document (not processed
by Aspose.Words). And yes, after updating the field there was a difference
between the property (showing PM which was correct) and field’s value
(showing AM which was incorrect and unexpected). At the same time, the
SAVEDATE field displayed the correct value (PM).

We suggest you please use SaveDate field in your document instead of DOCPROPERTY. Hope this helps you.

Sorry for the very slow reply on this - for some reason I never received an email about your most recent response.

I have just tried the steps that you outline in your most recent message, and I am unable to reproduce the problem you see with Word - I am using Word 2013 to try the repro, and it consistently updates the field correctly. It’s only when I try to original steps I outlined in the first few messages and get Aspose.Words involved in the process that I see the timestamp get updated incorrectly in the field.

Is it possible you could try again to see if you can reproduce the problem? I know you’ve already looked at it, but our client who has this particular problem would really like a resolution, and through our testing we’re convinced this isn’t a bug with Word. Our product has two modes - one which uses Aspose where it can and one which exclusively uses Word (it’s much much slower to use Word, but some clients prefer that option), and this problem only occurs when using Aspose - when our product only uses Word it is able to update the field correctly.

Thanks for any more help you can provide on this - it’s a very confusing and difficult to understand issue, but I’m certain there actually is an issue here that needs fixing.

Hi Michael,

Thanks
for your inquiry. I have logged another issue as WORDSNET-12287 to look into this issue further. We will update you via this forum thread once there is any update available on this issue. We suggest you please use SaveDate field in your document instead of DOCPROPERTY. Please use following code example as a workaround. Hope this helps you.

Document doc = new Document(MyDir + "sample+-+am.docx");
DocumentBuilder builder = new DocumentBuilder(doc);
foreach (Field field in doc.Range.Fields)
{
    if (field.Type == FieldType.FieldDocProperty)
    {
        if (field.GetFieldCode().Contains("LastSavedTime"))
        {
            builder.MoveTo(field.Start);
            builder.InsertField(FieldType.FieldSaveDate, true);
            field.Remove();
        }
    }
}
doc.UpdateFields();
doc.Save(MyDir + "Out am.docx");

Unfortunately, our client is creating a legal document that has certain requirements and that workaround is not an option that our client can use. Thanks for the code sample, though, and for taking another look at the issue.

Hi Michael,

Thanks
for sharing the detail. We will update you via this forum thread once there is any update available on WORDSNET-12287.

Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.

The issues you have found earlier (filed as WORDSNET-12287) have been fixed in this .NET update and this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.