简体   繁体   English

将mySQL数据库的日志文件日期时间格式从12小时时钟批量转换为24小时时钟

[英]Bulk convert log file date time format from 12 hour clock to 24 hour clock for mySQL database

I am creating a web app to analyze data from a client's custom database. 我正在创建一个Web应用程序,以分析来自客户的自定义数据库的数据。 I am having difficulty finding a way how to convert the client's log file entries from 12 hour clock to 24 hour clock. 我很难找到一种方法将客户端的日志文件条目从12小时制转换为24小时制。 The Database that I am setting this up with cannot read 12 hour time format, so is displaying the time wrong. 我为此设置的数据库无法读取12小时时间格式,因此显示时间错误。

Log files I am using look like this: 我正在使用的日志文件如下所示:

Site_Name,number_of_clicks,DD/MM/YYYY_2:00PM,Interaction_Type

I need to convert the log files to look like this: 我需要将日志文件转换为如下形式:

Site_Name,number_of_clicks,DD/MM/YYYY_14:00,Interaction_Type

There are tens of thousands of enteries per log file, so there is no way this can be done per entry. 每个日志文件有成千上万次输入,因此,无法对每个条目执行此操作。 I need to figure out a way to bulk convert the entries to 24-hour clock for each of the log files. 我需要找出一种将每个日志文件的条目批量转换为24小时制的方法。

Any help would be greatly appreciated! 任何帮助将不胜感激!

Thanks! 谢谢!

Aaron 亚伦

If you want to do that on db side and you use MySql you can read it with STR_TO_DATE() and either use it as a datetime value 如果要在数据库端执行此操作,并且使用MySql,则可以使用STR_TO_DATE()读取它, STR_TO_DATE()其用作日期时间值

INSERT INTO log (`Site_Name`, `number_of_clicks`, `date`, `Interaction_Type`)
SELECT 'site1', 10, STR_TO_DATE('10/05/2013_2:00PM', '%d/%m/%Y_%l:%i%p'), 1;

or 要么

UPDATE log
   SET date = STR_TO_DATE(str_date, '%d/%m/%Y_%l:%i%p');

assuming that date column is of type datetime, and str_date column contains string values in 12h format. 假设date列的类型为datetime,而str_date列包含12h格式的字符串值。

or if for some reason you store it as VARCHAR and really need to format it as DD/MM/YYYY_14:00 then you can use DATE_FORMAT() 或者如果由于某种原因将其存储为VARCHAR且确实需要将其格式化为DD/MM/YYYY_14:00则可以使用DATE_FORMAT()

DATE_FORMAT(STR_TO_DATE('10/05/2013_2:00PM', '%d/%m/%Y_%l:%i%p'), '%d/%m/%Y_%k:%i')

which will produce 会产生

|          NEWDATE |
--------------------
| 10/05/2013_14:00 |

To update in-place 就地更新

UPDATE log
   SET str_date = DATE_FORMAT(STR_TO_DATE(str_date, '%d/%m/%Y_%l:%i%p'), '%d/%m/%Y_%k:%i');

Here is SQLFiddle demo. 这是SQLFiddle演示。

Why don't you write a simple app that will search through all log entries and update whenever it finds date in PM format. 为什么不编写一个简单的应用程序,它将搜索所有日志条目并在找到PM格式的日期时进行更新。 You basically need to write a piece of code that will add 12 hrs if there is PM in the time. 基本上,您需要编写一段代码,如果有时间的话,这将增加12个小时。

Here is a rough code in C#... not fully tested but you can build on this… 这是C#中的粗略代码...尚未经过全面测试,但是您可以在此基础上构建…

protected string ConvertToPM(string logEntry)

{
    string result = string.Empty;

//DD/MM/YYYY_2:00PM
if (logEntry.Contains("PM"))
{
   string temp = logEntry.Substring(logEntry.IndexOf("_"), logEntry.IndexOf(":") - logEntry.IndexOf("_"));

    result = logEntry.Replace(temp, (Convert.ToInt32(temp) + 12).ToString());
}

return result;
}

Just add a piece of code that will a) read log file line by line b) extract date part c) use function similar to the one above to alter the data… 只需添加一段代码即可:a)逐行读取日志文件b)提取日期部分c)使用与上面类似的功能来更改数据…

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM