Charychap
Charychap

Reputation: 69

Creating a log file name based on the hour during day light saving

I am working on a project. The application follows day time saving.

The application creates a log file every hour when running,e.g.

2015042206_someName.log

Here 20150422 is the date and 06 is the hour of logging.

When time is changing in winter, there will two time 2 am – you know what I mean.

I want to make two different files for these two different hours by assigning suffix, e.g.

2015042202A_someName.log – File before time change
2015042202B_someName.log – File after time change

One solution I thought of:

Somehow (I don’t know how) get ‘PREVIOUS HOUR’ from the Calendar and see the different between the ‘HOUR_OF_DAY’ and ‘PREVIOUS HOUR’. If the difference is zero then time has changed so put suffix.

In this case, I don’t know how to get the previous hour – especially if the application was not running during the previous hour.

Any other solution anyone can think of???

Any help is appreciated greatly. Thanks in advance.

Upvotes: 1

Views: 210

Answers (4)

Component 10
Component 10

Reputation: 10497

IMHO You're complicating the matter too much. The best way to approach this is to avoid the daylight savings time issue by making the timestamp UTC. That way you will never have the DST issue, and you won't have to make complicated and error prone changes.

True, it may get confusing if someone reading the file-names doesn't realise that it's not in local time, but you can always append "UTC" in the file-name if it's really an issue, like:

2015042202_UTC_someName.log

Upvotes: 1

llogiq
llogiq

Reputation: 14521

The easiest way to do this (if you are happy with specifying the time zone) would be to use a SimpleDateFormat("yyyyMMddhhX")

Upvotes: 0

Loki
Loki

Reputation: 4130

A quite trivial way would be to save the last file name in a .txt File. When setting up the new name you can always look at this specific file for the last name. If your algorithm gives you the same name, you can add a suffix.

Upvotes: 0

mbsingh
mbsingh

Reputation: 489

You can simply put a check in place to see if the file with that name already exists or not. In case it does, you can add "A" or "B" as per your wish.

Upvotes: 2

Related Questions